Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

DTreeMultilevelEmbedder.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <ogdf/basic/Graph.h>
34 #include <ogdf/basic/basic.h>
39 
40 #include <algorithm>
41 
42 namespace ogdf {
43 
44 template<int Dim>
46 public:
47  struct NodeCoords {
48  double coords[Dim];
49  };
50 
54  m_levelMaxNumNodes = 10;
55 
57  m_scaleFactorPerLevel = 1.71;
58  } else {
59  m_scaleFactorPerLevel = 3.71; //3.71;//3.71;
60  }
61 
64 #if 0
66 #endif
69 
70  m_thresholdFinestLevel = 0.0002;
72 
75  }
76 
78  void call(const Graph& graph, NodeArray<NodeCoords>& coords);
79 
80 private:
90 
93 };
94 
96 public:
97  void call(GraphAttributes& GA) override {
98  // the graph
99  const Graph& G = GA.constGraph();
100 
101  // the input for the general d dim class
103 
104  // call it
106 
107  // copy the coords back to graph attributes
108  for (node v = G.firstNode(); v; v = v->succ()) {
109  GA.x(v) = coords[v].coords[0];
110  GA.y(v) = coords[v].coords[1];
111  }
112  }
113 };
114 
116 public:
117  void call(GraphAttributes& GA) override {
118  // assert 3d
120 
121  // the graph
122  const Graph& G = GA.constGraph();
123 
124  // the input for the general d dim class
126 
127  // call it
129 
130  // copy the coords back to graph attributes
131  for (node v = G.firstNode(); v; v = v->succ()) {
132  GA.x(v) = coords[v].coords[0];
133  GA.y(v) = coords[v].coords[1];
134  GA.z(v) = coords[v].coords[2];
135  }
136  }
137 };
138 
139 template<int Dim>
141  using namespace energybased::dtree;
142  using Embedder = DTreeEmbedder<Dim>;
143 
144  // we call this just in case
145  resultCoords.init(graph);
146 
147  // make sure the graph is connected
148  OGDF_ASSERT(isConnected(graph));
149 
150  // setup the multilevel step
151  GalaxyLevel levelBegin(graph);
152 
153  // this is the coarsest level with at most m_levelMaxNumNodes
154  GalaxyLevel* pLevelEnd = levelBegin.buildLevelsUntil(m_levelMaxNumNodes);
155 
156  // this array will hold the layout information of the parent node in the coarser level.
157  // furthermore this will keep the final result.
158  NodeArray<NodeCoords>& parentPosition = resultCoords;
159 
160  double currNumIterations = m_numIterationsFinestLevel;
161  double currThreshold = m_thresholdFinestLevel;
162 
163  // loop from the coarsest to the finest level
164  for (GalaxyLevel* pCurrLevel = &levelBegin; pCurrLevel != nullptr;
165  pCurrLevel = pCurrLevel->nextCoarser()) {
166  currNumIterations *= m_numIterationsFactorPerLevel;
167  currThreshold *= m_thresholdFactorPerLevel;
168  }
169 
170  // now we loop from the coarsest to the finest level
171  for (GalaxyLevel* pCurrLevel = pLevelEnd; pCurrLevel; pCurrLevel = pCurrLevel->nextFiner()) {
172  currNumIterations /= m_numIterationsFactorPerLevel;
173  currThreshold /= m_thresholdFactorPerLevel;
174 
175  // new embedder instance for the current level
176  Embedder embedder(pCurrLevel->graph());
177 
178  // if this is coarsest one
179  if (pCurrLevel->isCoarsestLevel()) {
180  // we cannot init from the parent level, do it random
181  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
182  // for all dims
183  for (int d = 0; d < Dim; d++) {
184  // set the position to some random value
185  embedder.setPosition(v, d, randomDouble(-1.0, 1.0));
186  }
187  }
188  } else { // if we have a parent level
189  // iterate over all nodes
190  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
191  // this is a node on the coarser level we already processed
192  node v_parent = pCurrLevel->parent(v);
193 
194  // now init the position from the parent.
195  for (int d = 0; d < Dim; d++) {
196  // get the position of the parent
197  double offset = parentPosition[v_parent].coords[d] * m_scaleFactorPerLevel;
198 
199  // set v's position to the parents pos with some random
200  embedder.setPosition(v, d, offset + randomDouble(-1.0, 1.0));
201  }
202  }
203  }
204  // we have some proper initial coordinates for the nodes
205 
206  if (m_useMultilevelWeights) {
207  // we cannot init from the parent level, do it random
208  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
209  embedder.setMass(v, pCurrLevel->weight(v));
210  }
211 
212  for (edge e = pCurrLevel->graph().firstEdge(); e; e = e->succ()) {
213  embedder.setEdgeWeight(e, pCurrLevel->edgeWeight(e));
214  }
215  }
216 
217  const int numIterationsMaybe =
218  pCurrLevel->isCoarsestLevel() ? m_numIterationsCoarsestLevel : currNumIterations;
219  const int numIterations = std::min(std::max(m_minIterationsPerLevel, numIterationsMaybe),
220  m_maxIterationsPerLevel);
221 
222  embedder.scaleNodes(3.0);
223  embedder.doIterationsNewton(numIterations, currThreshold, RepForceFunctionNewton<Dim, 1>,
224  AttrForceFunctionPow<Dim, 2>);
225  embedder.scaleNodes(1.0 / 3.0);
226  embedder.doIterationsNewton(numIterations, currThreshold, RepForceFunctionNewton<Dim, 2>,
227  AttrForceFunctionPow<Dim, 2>);
228  // run the layout
229 
230  // we now have to backup the positions before getting rid of the embedder
231  parentPosition.init(pCurrLevel->graph());
232 
233  // iterate over all nodes
234  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
235  // for all coords
236  for (int d = 0; d < Dim; d++) {
237  parentPosition[v].coords[d] = embedder.position(v, d);
238  }
239  }
240  }
241 
242  // we are done with the layout. It is saved now in the parentposition nodearray.
243  // which is a reference to the result array
244 }
245 
246 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
DTreeForceTypes.h
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:72
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Graph.h
Includes declaration of graph class.
ogdf::DTreeMultilevelEmbedder::NodeCoords
Definition: DTreeMultilevelEmbedder.h:47
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::GraphAttributes::z
double z(node v) const
Returns the z-coordinate of node v.
Definition: GraphAttributes.h:283
ogdf::GraphAttributes::x
double x(node v) const
Returns the x-coordinate of node v.
Definition: GraphAttributes.h:247
ogdf::DTreeMultilevelEmbedder::m_numIterationsCoarsestLevel
int m_numIterationsCoarsestLevel
Definition: DTreeMultilevelEmbedder.h:88
ogdf::DTreeMultilevelEmbedder::m_thresholdFinestLevel
double m_thresholdFinestLevel
Definition: DTreeMultilevelEmbedder.h:86
ogdf::DTreeMultilevelEmbedder::m_numIterationsFactorPerLevel
double m_numIterationsFactorPerLevel
Definition: DTreeMultilevelEmbedder.h:85
LayoutModule.h
Declaration of interface for layout algorithms (class LayoutModule)
ogdf::DTreeMultilevelEmbedder::m_thresholdCoarsestLevel
double m_thresholdCoarsestLevel
Definition: DTreeMultilevelEmbedder.h:89
ogdf::isConnected
bool isConnected(const Graph &G)
Returns true iff G is connected.
ogdf::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:223
ogdf::DTreeMultilevelEmbedder::m_thresholdFactorPerLevel
double m_thresholdFactorPerLevel
Definition: DTreeMultilevelEmbedder.h:87
ogdf::DTreeMultilevelEmbedder2D::call
void call(GraphAttributes &GA) override
Computes a layout of graph GA.
Definition: DTreeMultilevelEmbedder.h:97
ogdf::GraphAttributes::y
double y(node v) const
Returns the y-coordinate of node v.
Definition: GraphAttributes.h:265
ogdf::DTreeMultilevelEmbedder3D
Definition: DTreeMultilevelEmbedder.h:115
ogdf::DTreeMultilevelEmbedder2D
Definition: DTreeMultilevelEmbedder.h:95
ogdf::DTreeMultilevelEmbedder::m_numIterationsFinestLevel
int m_numIterationsFinestLevel
Definition: DTreeMultilevelEmbedder.h:84
ogdf::DTreeMultilevelEmbedder::m_levelMaxNumNodes
int m_levelMaxNumNodes
Definition: DTreeMultilevelEmbedder.h:91
ogdf::DTreeMultilevelEmbedder::m_scaleFactorPerLevel
double m_scaleFactorPerLevel
Definition: DTreeMultilevelEmbedder.h:92
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::DTreeMultilevelEmbedder::call
void call(const Graph &graph, NodeArray< NodeCoords > &coords)
call the multilevel embedder layout for graph, the result is stored in coords
Definition: DTreeMultilevelEmbedder.h:140
ogdf::DTreeMultilevelEmbedder
Definition: DTreeMultilevelEmbedder.h:45
basic.h
Basic declarations, included by all source files.
ogdf::NodeElement::succ
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:292
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
ogdf::DTreeMultilevelEmbedder::m_useMultilevelWeights
bool m_useMultilevelWeights
Definition: DTreeMultilevelEmbedder.h:83
GalaxyLevel.h
ogdf::EdgeElement::succ
edge succ() const
Returns the successor in the list of all edges.
Definition: Graph_d.h:433
ogdf::DTreeMultilevelEmbedder::m_minIterationsPerLevel
int m_minIterationsPerLevel
Definition: DTreeMultilevelEmbedder.h:82
ogdf::RegisteredArray< GraphRegistry< Key >, Value, WithDefault, Graph >::init
void init(const Graph *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
Definition: RegisteredArray.h:858
ogdf::DTreeMultilevelEmbedder::m_maxIterationsPerLevel
int m_maxIterationsPerLevel
Definition: DTreeMultilevelEmbedder.h:81
DTreeEmbedder.h
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::DTreeMultilevelEmbedder::DTreeMultilevelEmbedder
DTreeMultilevelEmbedder()
constructor with a given graph, allocates memory and does initialization
Definition: DTreeMultilevelEmbedder.h:52
ogdf::DTreeMultilevelEmbedder3D::call
void call(GraphAttributes &GA) override
Computes a layout of graph GA.
Definition: DTreeMultilevelEmbedder.h:117
simple_graph_alg.h
Declaration of simple graph algorithms.
ogdf::GraphAttributes::threeD
static const long threeD
Corresponds to node attribute z(node). Note that all methods work on 2D coordinates only.
Definition: GraphAttributes.h:166
ogdf::DTreeMultilevelEmbedder::NodeCoords::coords
double coords[Dim]
Definition: DTreeMultilevelEmbedder.h:48
ogdf::randomDouble
double randomDouble(double low, double high)
Returns a random double value from the interval [low, high).
ogdf::GraphAttributes::has
bool has(long attr) const
Returns true iff all attributes in attr are available.
Definition: GraphAttributes.h:200
ogdf::LayoutModule
Interface of general layout algorithms.
Definition: LayoutModule.h:45