Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
22#include <ogdf/basic/Graph.h>
36#include <iostream>
37#include <string>
38
39namespace ogdf {
40class InitialPlacer;
42} // namespace ogdf
43
44using namespace ogdf;
45
46template<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.
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.
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.
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
109int 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.
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}
Places nodes at the barycenter of his neighbors.
Splits and packs the components of a Graph.
Merges nodes with neighbour to get a Multilevel Graph.
Declaration of Fast-Multipole-Embedder layout algorithm.
Includes declaration of graph class.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Declares class GraphIO which provides access to all graph read and write functionality.
Decralation of GraphElement and GraphList classes.
Merges nodes with neighbour to get a Multilevel Graph.
MMM is a Multilevel Graph drawing Algorithm that can use different modules.
MLG is the main data structure for ModularMultilevelMixer.
Preprocessor Layout simplifies Graphs for use in other Algorithms.
ScalingLayout scales and calls a secondary layout.
Merges nodes with solar system rules.
Places Nodes with solar system rules.
Declaration of class TileToRowsCCPacker.
The barycenter placer for multilevel layout.
void weightedPositionPriority(bool on)
void setLayoutModule(LayoutModule *layout)
void setPacker(CCLayoutPackModule *packer)
The fast multipole embedder approach for force-directed layout.
void setNumIterations(uint32_t numIterations)
sets the maximum number of iterations
void setRandomize(bool b)
if true, layout algorithm will randomize the layout in the beginning
Stores additional attributes of a graph (like layout information).
double height(node v) const
Returns the height of the bounding box of node v.
double width(node v) const
Returns the width of the bounding box of node v.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition Graph_d.h:929
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.
static bool writeGML(const Graph &G, std::ostream &os)
Writes graph G in GML format to output stream os.
static bool drawSVG(const GraphAttributes &A, std::ostream &os, const SVGSettings &settings)
static bool readGML(Graph &G, std::istream &is)
Reads graph G in GML format from input stream is.
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.
Base class for placer modules.
Modular multilevel graph layout.
void setMultilevelBuilder(MultilevelBuilder *levelBuilder)
Sets the multilevel builder module to levelBuilder.
void setLayoutRepeats(int times=1)
Determines how many times the one-level layout will be called.
void setInitialPlacer(InitialPlacer *placement)
Sets the initial placer module to placement.
void setLevelLayoutModule(LayoutModule *levelLayout)
Sets the one-level layout module to levelLayout.
Base class for merger modules.
void exportAttributes(GraphAttributes &GA) const
Class for the representation of nodes.
Definition Graph_d.h:241
The PreprocessorLayout removes multi-edges and self-loops.
void setRandomizePositions(bool on)
Defines whether the positions of the node are randomized before the secondary layout call.
void call(Graph &G, MultilevelGraph &MLG)
void setLayoutModule(LayoutModule *layout)
Sets the secondary layout.
Scales a graph layout and calls a secondary layout algorithm.
void setScaling(double min, double max)
Sets the minimum and the maximum scaling factor.
void setSecondaryLayout(LayoutModule *layout)
Sets a LayoutModule that should be applied after scaling.
void setExtraScalingSteps(unsigned int steps)
Sets how often the scaling should be repeated.
void setLayoutRepeats(unsigned int repeats)
Sets how often the LayoutModule should be applied.
void setScalingType(ScalingType type)
Sets a ScalingType wich sets the relative scale for the Graph.
@ RelativeToDrawing
Scales by a factor relative to the drawing.
@ RelativeToDesiredLength
Scales by a factor relative to the desired Edgelength m_desEdgeLength.
The solar merger for multilevel layout.
Definition SolarMerger.h:48
The solar placer for multilevel layout.
Definition SolarPlacer.h:44
The tile-to-rows algorithm for packing drawings of connected components.
int main()
static void configureFastLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
static void configureNiceLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
static void configureNoTwistLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
static InitialPlacer * getBarycenterPlacer()
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
The namespace for all OGDF objects.