Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

multilevelmixer.cpp
Go to the documentation of this file.
1 // Introduction for Multilevelmixer:
2 //
3 // Multilevel layout computation is an iterative process that can
4 // be roughly divided in three phases: coarsening, placement, and
5 // single level layout. Starting with the smallest graph, the final
6 // layout for the input graph is obtained by successively computing
7 // layouts for the graph sequence computed by the coarsening phase.
8 // At each level, the additional vertices need to be placed into the
9 // layout of the preceding level, optionally after a scaling to provide
10 // the necessary space.
11 // It helps to overcome some problems of single level energybased graph
12 // layouts (such as finding a local optimal solution) and it speeds up
13 // the computation.
14 //
15 // The Modular Multilevel Mixer is an abstract class that can be used
16 // to build energybased multilevel layouts. Since it is modular you can
17 // easily assemble different layouts by using different coarsening
18 // techniques (merger), placer and single level layouts.
19 
21 #include <ogdf/basic/GraphList.h>
22 #include <ogdf/basic/Graph.h>
36 #include <iostream>
37 #include <string>
38 
39 namespace ogdf {
40 class InitialPlacer;
41 class MultilevelBuilder;
42 } // namespace ogdf
43 
44 using namespace ogdf;
45 
46 template<class T>
48 {
49  T *merger = new T();
50  merger->setFactor(2.0);
51  merger->setEdgeLengthAdjustment(0);
52  return merger;
53 }
54 
56 {
57  BarycenterPlacer *placer = new BarycenterPlacer();
58  placer->weightedPositionPriority(true);
59  return placer;
60 }
61 
63 {
64  // The SolarMerger is used for the coarsening phase.
65  merger = new SolarMerger(false, false);
66  // The SolarPlacer is used for the placement.
67  placer = new SolarPlacer();
68 
69  // Postprocessing is applied at each level after the single level layout.
70  // It is turned off in this example.
71  sl->setExtraScalingSteps(0);
72  // In this example it is used to scale with fixed factor 2 relative to the graph drawing.
74  sl->setScaling(2.0, 2.0);
75 }
76 
78 {
79  // The EdgeCoverMerger is used for the coarsening phase.
80  merger = getDoubleFactoredZeroAdjustedMerger<EdgeCoverMerger>();
81  // The BarycenterPlacer is used for the placement.
82  placer = getBarycenterPlacer();
83 
84  // Postprocessing is applied at each level after the single level layout.
85  // In this example a FastMultipoleEmbedder with zero iterations is used for postprocessing.
86  sl->setExtraScalingSteps(0);
87  // No scaling is done. It is fixed to factor 1.
89  sl->setScaling(1.0, 1.0);
90 }
91 
93 {
94  // The LocalBiconnectedMerger is used for the coarsening phase.
95  // It tries to keep biconnectivity to avoid twisted graph layouts.
96  merger = getDoubleFactoredZeroAdjustedMerger<LocalBiconnectedMerger>();
97  // The BarycenterPlacer is used for the placement.
98  placer = getBarycenterPlacer();
99 
100  // Postprocessing is applied at each level after the single level layout.
101  // It is turned off in this example.
102  sl->setExtraScalingSteps(1);
103  // The ScalingLayout is used to scale with a factor between 5 and 10
104  // relative to the edge length.
106  sl->setScaling(5.0, 10.0);
107 }
108 
109 int main(int argc, const char *argv[])
110 {
111  if (argc != 2) {
112  std::cout << "Usage: " << argv[0] << " (0|1|2)" << std::endl;
113  return 255;
114  }
115 
116  // We first declare a Graph G with GraphAttributes GA and load it from
117  // the GML file sierpinski_04.gml.
118  Graph g;
119  GraphAttributes ga(g);
120  if (!GraphIO::read(ga, g, "uk_Pack_Bary_EC_FRENC.gml", GraphIO::readGML)) {
121  std::cerr << "Could not load Graph" << std::endl;
122  return 1;
123  }
124 
125  // We assign a width and height of 10.0 to each node.
126  for (node v : g.nodes) {
127  ga.width(v) = ga.height(v) = 10.0;
128  }
129 
130  // Then we create a MultilevelGraph from the GraphAttributes.
131  MultilevelGraph mlg(ga);
132 
133  // The FastMultipoleEmbedder is used for the single level layout.
135  // It will use 1000 iterations at each level.
136  fme->setNumIterations(1000);
137  fme->setRandomize(false);
138 
139  // To minimize dispersion of the graph when more nodes are added, a
140  // ScalingLayout can be used to scale up the graph on each level.
141  ScalingLayout *sl = new ScalingLayout();
142  sl->setLayoutRepeats(1);
143  // The FastMultipoleEmbedder is nested into this ScalingLayout.
144  sl->setSecondaryLayout(fme);
145 
146  // Set the merger and placer according to the wanted configuration.
147  MultilevelBuilder *merger;
148  InitialPlacer *placer;
149  switch (argv[1][0]) {
150  case 2:
151  configureFastLayout(sl, merger, placer);
152  break;
153  case 1:
154  configureNiceLayout(sl, merger, placer);
155  break;
156  default:
157  configureNoTwistLayout(sl, merger, placer);
158  break;
159  }
160 
161  // Then the ModularMultilevelMixer is created.
163  mmm->setLayoutRepeats(1);
164  // The single level layout, the placer and the merger are set.
165  mmm->setLevelLayoutModule(sl);
166  mmm->setInitialPlacer(placer);
167  mmm->setMultilevelBuilder(merger);
168 
169  // Since energybased algorithms are not doing well for disconnected
170  // graphs, the ComponentSplitterLayout is used to split the graph and
171  // computation is done separately for each connected component.
173  // The TileToRowsPacker merges these connected components after computation.
175  csl->setPacker(ttrccp);
176  csl->setLayoutModule(mmm);
177 
178  // At last the PreprocessorLayout removes double edges and loops.
179  PreprocessorLayout ppl;
180  ppl.setLayoutModule(csl);
181  ppl.setRandomizePositions(true);
182 
183  ppl.call(mlg);
184 
185  // After the computation the MultilevelGraph is exported to the
186  // GraphAttributes and written to disk.
187  mlg.exportAttributes(ga);
188  GraphIO::write(ga, "output-multilevelmixer-" + std::string(argv[1]) + ".gml", GraphIO::writeGML);
189  GraphIO::write(ga, "output-multilevelmixer-" + std::string(argv[1]) + ".svg", GraphIO::drawSVG);
190 
191  return 0;
192 }
ogdf::ScalingLayout::setScalingType
void setScalingType(ScalingType type)
Sets a ScalingType wich sets the relative scale for the Graph.
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
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.
ogdf::ModularMultilevelMixer::setInitialPlacer
void setInitialPlacer(InitialPlacer *placement)
Sets the initial placer module to placement.
Definition: ModularMultilevelMixer.h:136
ogdf::MultilevelGraph::exportAttributes
void exportAttributes(GraphAttributes &GA) const
getBarycenterPlacer
static InitialPlacer * getBarycenterPlacer()
Definition: multilevelmixer.cpp:55
ogdf::BarycenterPlacer
The barycenter placer for multilevel layout.
Definition: BarycenterPlacer.h:44
Graph.h
Includes declaration of graph class.
ogdf::GraphIO::write
static bool write(const Graph &G, const string &filename, WriterFunc writer=nullptr)
Writes graph G to a file with name filename and infers the format to use from the file's extension.
ogdf::ComponentSplitterLayout::setLayoutModule
void setLayoutModule(LayoutModule *layout)
Definition: ComponentSplitterLayout.h:62
ModularMultilevelMixer.h
MMM is a Multilevel Graph drawing Algorithm that can use different modules.
ogdf::FastMultipoleEmbedder::setRandomize
void setRandomize(bool b)
if true, layout algorithm will randomize the layout in the beginning
Definition: FastMultipoleEmbedder.h:84
ogdf::GraphIO::writeGML
static bool writeGML(const Graph &G, std::ostream &os)
Writes graph G in GML format to output stream os.
ogdf::GraphIO::drawSVG
static bool drawSVG(const GraphAttributes &A, std::ostream &os, const SVGSettings &settings)
ogdf::PreprocessorLayout::call
void call(Graph &G, MultilevelGraph &MLG)
ogdf::ScalingLayout::ScalingType::RelativeToDesiredLength
@ RelativeToDesiredLength
Scales by a factor relative to the desired Edgelength m_desEdgeLength.
ogdf::FastMultipoleEmbedder
The fast multipole embedder approach for force-directed layout.
Definition: FastMultipoleEmbedder.h:52
ogdf::PreprocessorLayout
The PreprocessorLayout removes multi-edges and self-loops.
Definition: PreprocessorLayout.h:57
configureFastLayout
static void configureFastLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
Definition: multilevelmixer.cpp:62
ogdf::SolarMerger
The solar merger for multilevel layout.
Definition: SolarMerger.h:48
configureNiceLayout
static void configureNiceLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
Definition: multilevelmixer.cpp:77
ogdf::GraphIO::readGML
static bool readGML(Graph &G, std::istream &is)
Reads graph G in GML format from input stream is.
ogdf::MultilevelGraph
Definition: MultilevelGraph.h:69
ogdf::BarycenterPlacer::weightedPositionPriority
void weightedPositionPriority(bool on)
ogdf::PreprocessorLayout::setRandomizePositions
void setRandomizePositions(bool on)
Defines whether the positions of the node are randomized before the secondary layout call.
Definition: PreprocessorLayout.h:99
BarycenterPlacer.h
Places nodes at the barycenter of his neighbors.
ogdf::InitialPlacer
Base class for placer modules.
Definition: InitialPlacer.h:43
PreprocessorLayout.h
Preprocessor Layout simplifies Graphs for use in other Algorithms.
ogdf::ScalingLayout::setScaling
void setScaling(double min, double max)
Sets the minimum and the maximum scaling factor.
ogdf::ScalingLayout::setExtraScalingSteps
void setExtraScalingSteps(unsigned int steps)
Sets how often the scaling should be repeated.
main
int main(int argc, const char *argv[])
Definition: multilevelmixer.cpp:109
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:932
ogdf::ScalingLayout::ScalingType::RelativeToDrawing
@ RelativeToDrawing
Scales by a factor relative to the drawing.
EdgeCoverMerger.h
Merges nodes with neighbour to get a Multilevel Graph.
ogdf::ComponentSplitterLayout::setPacker
void setPacker(CCLayoutPackModule *packer)
Definition: ComponentSplitterLayout.h:64
GraphList.h
Decralation of GraphElement and GraphList classes.
GraphIO.h
Declares class GraphIO which provides access to all graph read and write functionality.
SolarPlacer.h
Places Nodes with solar system rules.
ogdf::ModularMultilevelMixer::setLayoutRepeats
void setLayoutRepeats(int times=1)
Determines how many times the one-level layout will be called.
Definition: ModularMultilevelMixer.h:139
ComponentSplitterLayout.h
Splits and packs the components of a Graph.
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::GraphAttributes::height
double height(node v) const
Returns the height of the bounding box of node v.
Definition: GraphAttributes.h:393
ogdf::PreprocessorLayout::setLayoutModule
void setLayoutModule(LayoutModule *layout)
Sets the secondary layout.
Definition: PreprocessorLayout.h:96
LocalBiconnectedMerger.h
Merges nodes with neighbour to get a Multilevel Graph.
ogdf::MultilevelBuilder
Base class for merger modules.
Definition: MultilevelBuilder.h:43
TileToRowsCCPacker.h
Declaration of class TileToRowsCCPacker.
MultilevelGraph.h
MLG is the main data structure for ModularMultilevelMixer.
ogdf::ScalingLayout::setLayoutRepeats
void setLayoutRepeats(unsigned int repeats)
Sets how often the LayoutModule should be applied.
ogdf::ScalingLayout::setSecondaryLayout
void setSecondaryLayout(LayoutModule *layout)
Sets a LayoutModule that should be applied after scaling.
FastMultipoleEmbedder.h
Declaration of Fast-Multipole-Embedder layout algorithm.
ScalingLayout.h
ScalingLayout scales and calls a secondary layout.
ogdf::GraphIO::read
static bool read(Graph &G, const string &filename, ReaderFunc reader=nullptr)
Reads graph G from a file with name filename and infers the used format from the file's extension.
ogdf::ComponentSplitterLayout
Definition: ComponentSplitterLayout.h:44
ogdf::FastMultipoleEmbedder::setNumIterations
void setNumIterations(uint32_t numIterations)
sets the maximum number of iterations
Definition: FastMultipoleEmbedder.h:78
configureNoTwistLayout
static void configureNoTwistLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
Definition: multilevelmixer.cpp:92
ogdf::ScalingLayout
Scales a graph layout and calls a secondary layout algorithm.
Definition: ScalingLayout.h:50
ogdf::ModularMultilevelMixer
Modular multilevel graph layout.
Definition: ModularMultilevelMixer.h:75
ogdf::SolarPlacer
The solar placer for multilevel layout.
Definition: SolarPlacer.h:44
getDoubleFactoredZeroAdjustedMerger
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
Definition: multilevelmixer.cpp:47
ogdf::TileToRowsCCPacker
The tile-to-rows algorithm for packing drawings of connected components.
Definition: TileToRowsCCPacker.h:43
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::ModularMultilevelMixer::setMultilevelBuilder
void setMultilevelBuilder(MultilevelBuilder *levelBuilder)
Sets the multilevel builder module to levelBuilder.
Definition: ModularMultilevelMixer.h:131
SolarMerger.h
Merges nodes with solar system rules.
ogdf::ModularMultilevelMixer::setLevelLayoutModule
void setLevelLayoutModule(LayoutModule *levelLayout)
Sets the one-level layout module to levelLayout.
Definition: ModularMultilevelMixer.h:123
ogdf::GraphAttributes::width
double width(node v) const
Returns the width of the bounding box of node v.
Definition: GraphAttributes.h:357