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 
32 
33 using namespace ogdf;
34 
35 template<class T>
37 {
38  T *merger = new T();
39  merger->setFactor(2.0);
40  merger->setEdgeLengthAdjustment(0);
41  return merger;
42 }
43 
45 {
46  BarycenterPlacer *placer = new BarycenterPlacer();
47  placer->weightedPositionPriority(true);
48  return placer;
49 }
50 
52 {
53  // The SolarMerger is used for the coarsening phase.
54  merger = new SolarMerger(false, false);
55  // The SolarPlacer is used for the placement.
56  placer = new SolarPlacer();
57 
58  // Postprocessing is applied at each level after the single level layout.
59  // It is turned off in this example.
60  sl->setExtraScalingSteps(0);
61  // In this example it is used to scale with fixed factor 2 relative to the graph drawing.
63  sl->setScaling(2.0, 2.0);
64 }
65 
67 {
68  // The EdgeCoverMerger is used for the coarsening phase.
69  merger = getDoubleFactoredZeroAdjustedMerger<EdgeCoverMerger>();
70  // The BarycenterPlacer is used for the placement.
71  placer = getBarycenterPlacer();
72 
73  // Postprocessing is applied at each level after the single level layout.
74  // In this example a FastMultipoleEmbedder with zero iterations is used for postprocessing.
75  sl->setExtraScalingSteps(0);
76  // No scaling is done. It is fixed to factor 1.
78  sl->setScaling(1.0, 1.0);
79 }
80 
82 {
83  // The LocalBiconnectedMerger is used for the coarsening phase.
84  // It tries to keep biconnectivity to avoid twisted graph layouts.
85  merger = getDoubleFactoredZeroAdjustedMerger<LocalBiconnectedMerger>();
86  // The BarycenterPlacer is used for the placement.
87  placer = getBarycenterPlacer();
88 
89  // Postprocessing is applied at each level after the single level layout.
90  // It is turned off in this example.
91  sl->setExtraScalingSteps(1);
92  // The ScalingLayout is used to scale with a factor between 5 and 10
93  // relative to the edge length.
95  sl->setScaling(5.0, 10.0);
96 }
97 
98 int main(int argc, const char *argv[])
99 {
100  if (argc != 2) {
101  std::cout << "Usage: " << argv[0] << " (0|1|2)" << std::endl;
102  return 255;
103  }
104 
105  // We first declare a Graph G with GraphAttributes GA and load it from
106  // the GML file sierpinski_04.gml.
107  Graph g;
108  GraphAttributes ga(g);
109  if (!GraphIO::read(ga, g, "uk_Pack_Bary_EC_FRENC.gml", GraphIO::readGML)) {
110  std::cerr << "Could not load Graph" << std::endl;
111  return 1;
112  }
113 
114  // We assign a width and height of 10.0 to each node.
115  for (node v : g.nodes) {
116  ga.width(v) = ga.height(v) = 10.0;
117  }
118 
119  // Then we create a MultilevelGraph from the GraphAttributes.
120  MultilevelGraph mlg(ga);
121 
122  // The FastMultipoleEmbedder is used for the single level layout.
124  // It will use 1000 iterations at each level.
125  fme->setNumIterations(1000);
126  fme->setRandomize(false);
127 
128  // To minimize dispersion of the graph when more nodes are added, a
129  // ScalingLayout can be used to scale up the graph on each level.
130  ScalingLayout *sl = new ScalingLayout();
131  sl->setLayoutRepeats(1);
132  // The FastMultipoleEmbedder is nested into this ScalingLayout.
133  sl->setSecondaryLayout(fme);
134 
135  // Set the merger and placer according to the wanted configuration.
136  MultilevelBuilder *merger;
137  InitialPlacer *placer;
138  switch (argv[1][0]) {
139  case 2:
140  configureFastLayout(sl, merger, placer);
141  break;
142  case 1:
143  configureNiceLayout(sl, merger, placer);
144  break;
145  default:
146  configureNoTwistLayout(sl, merger, placer);
147  break;
148  }
149 
150  // Then the ModularMultilevelMixer is created.
152  mmm->setLayoutRepeats(1);
153  // The single level layout, the placer and the merger are set.
154  mmm->setLevelLayoutModule(sl);
155  mmm->setInitialPlacer(placer);
156  mmm->setMultilevelBuilder(merger);
157 
158  // Since energybased algorithms are not doing well for disconnected
159  // graphs, the ComponentSplitterLayout is used to split the graph and
160  // computation is done separately for each connected component.
162  // The TileToRowsPacker merges these connected components after computation.
164  csl->setPacker(ttrccp);
165  csl->setLayoutModule(mmm);
166 
167  // At last the PreprocessorLayout removes double edges and loops.
168  PreprocessorLayout ppl;
169  ppl.setLayoutModule(csl);
170  ppl.setRandomizePositions(true);
171 
172  ppl.call(mlg);
173 
174  // After the computation the MultilevelGraph is exported to the
175  // GraphAttributes and written to disk.
176  mlg.exportAttributes(ga);
177  GraphIO::write(ga, "output-multilevelmixer-" + std::string(argv[1]) + ".gml", GraphIO::writeGML);
178  GraphIO::write(ga, "output-multilevelmixer-" + std::string(argv[1]) + ".svg", GraphIO::drawSVG);
179 
180  return 0;
181 }
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: AugmentationModule.h:36
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:66
ogdf::ModularMultilevelMixer::setInitialPlacer
void setInitialPlacer(InitialPlacer *placement)
Sets the initial placer module to placement.
Definition: ModularMultilevelMixer.h:134
ogdf::MultilevelGraph::exportAttributes
void exportAttributes(GraphAttributes &GA) const
getBarycenterPlacer
static InitialPlacer * getBarycenterPlacer()
Definition: multilevelmixer.cpp:44
ogdf::BarycenterPlacer
The barycenter placer for multilevel layout.
Definition: BarycenterPlacer.h:42
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:63
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:78
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:46
ogdf::PreprocessorLayout
The PreprocessorLayout removes multi-edges and self-loops.
Definition: PreprocessorLayout.h:51
configureFastLayout
static void configureFastLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
Definition: multilevelmixer.cpp:51
ogdf::SolarMerger
The solar merger for multilevel layout.
Definition: SolarMerger.h:42
configureNiceLayout
static void configureNiceLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
Definition: multilevelmixer.cpp:66
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:66
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:93
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:98
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:924
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:65
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:137
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:862
ogdf::GraphAttributes::height
double height(node v) const
Returns the height of the bounding box of node v.
Definition: GraphAttributes.h:387
ogdf::PreprocessorLayout::setLayoutModule
void setLayoutModule(LayoutModule *layout)
Sets the secondary layout.
Definition: PreprocessorLayout.h:90
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.
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:45
ogdf::FastMultipoleEmbedder::setNumIterations
void setNumIterations(uint32_t numIterations)
sets the maximum number of iterations
Definition: FastMultipoleEmbedder.h:72
configureNoTwistLayout
static void configureNoTwistLayout(ScalingLayout *sl, MultilevelBuilder *&merger, InitialPlacer *&placer)
Definition: multilevelmixer.cpp:81
ogdf::ScalingLayout
Scales a graph layout and calls a secondary layout algorithm.
Definition: ScalingLayout.h:47
ogdf::ModularMultilevelMixer
Modular multilevel graph layout.
Definition: ModularMultilevelMixer.h:73
ogdf::SolarPlacer
The solar placer for multilevel layout.
Definition: SolarPlacer.h:42
getDoubleFactoredZeroAdjustedMerger
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
Definition: multilevelmixer.cpp:36
ogdf::TileToRowsCCPacker
The tile-to-rows algorithm for packing drawings of connected components.
Definition: TileToRowsCCPacker.h:40
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::ModularMultilevelMixer::setMultilevelBuilder
void setMultilevelBuilder(MultilevelBuilder *levelBuilder)
Sets the multilevel builder module to levelBuilder.
Definition: ModularMultilevelMixer.h:129
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:121
ogdf::GraphAttributes::width
double width(node v) const
Returns the width of the bounding box of node v.
Definition: GraphAttributes.h:351