Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Graph Decomposition

Generate an acyclic random graph

This example shows how to generate a random plane triangulation, decompose it into its 4-connected components, and draw each of them from the outside in.

#include <memory>
#include <string>
using namespace ogdf;
int main(void) {
Graph g;
constexpr int n = 16;
randomPlanarConnectedGraph(g, n, 3 * n - 6);
const adjEntry externalFace = g.firstNode()->firstAdj()->cyclicSucc();
const auto fbt = FourBlockTree::construct(g, externalFace);
{
ga.directed() = false;
layout.callFixEmbed(ga, externalFace);
for (const node v : g.nodes) {
ga.label(v) = std::to_string(v->index());
ga.fillColor(v) = Color::Name::White;
}
GraphIO::write(ga, "output-g.svg", GraphIO::drawSVG);
}
int i = 0;
fbt->preorder([&](const FourBlockTree& treeNode) -> void {
GraphAttributes ga(*treeNode.g,
ga.directed() = false;
layout.callFixEmbed(ga, treeNode.externalFace);
for (const node v : treeNode.g->nodes) {
ga.label(v) = std::to_string(treeNode.originalNodes[v]->index());
ga.fillColor(v) = Color::Name::White;
}
GraphIO::write(ga, std::string("output-node-") + std::to_string(i++) + ".svg",
GraphIO::drawSVG);
});
return 0;
}
Declaration of FourBlockTree.
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.
Declaration of class PlanarStraightLayout which represents a planar straight-line drawing algorithm.
Class for adjacency list elements.
Definition Graph_d.h:143
adjEntry cyclicSucc() const
Returns the cyclic successor in the adjacency list.
Definition Graph_d.h:355
Stores additional attributes of a graph (like layout information).
static const long edgeStyle
Corresponds to edge attributes strokeColor(edge), strokeType(edge), and strokeWidth(edge).
static const long nodeLabel
Corresponds to node attribute label(node).
static const long nodeStyle
Corresponds to node attributes strokeColor(node), strokeType(node), strokeWidth(node),...
static const long edgeGraphics
Corresponds to edge attribute bends(edge).
static const long nodeGraphics
Corresponds to node attributes x(node), y(node), width(node), height(node), and shape(node).
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
node firstNode() const
Returns the first node in the list of all nodes.
Definition Graph_d.h:994
Class for the representation of nodes.
Definition Graph_d.h:241
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition Graph_d.h:287
void callFixEmbed(GraphAttributes &AG, adjEntry adjExternal=nullptr)
Calls the grid layout algorithm with a fixed planar embedding (general call).
Implementation of the Planar-Straight layout algorithm.
int main()
Declaration of basic types for graphics.
The namespace for all OGDF objects.
Declaration of randomized graph generators.
A node in a 4-block tree.
NodeArray< node > originalNodes
The nodes in the original graph corresponding to the nodes in g.
adjEntry externalFace
A half-edge in g such that the external face of g is to its right.
std::unique_ptr< Graph > g
The 4-connected component.

Step-by-step explanation

  1. The class FourBlockTree is declared in ogdf/decomposition/FourBlockTree.h
  2. We create a random plane graph. Because of its high number of edges, it must be maximally planar, i.e., triangulated.
  3. We use the function FourBlockTree::construct to build its 4-block tree. The 4-block tree is represented by its root, which has members
    • g, the 4-connected component
    • originalNodes, a map from nodes in g to the corresponding nodes in the original graph
    • externalFace, an adjEntry to the right of which the externalFace of the 4-connected component lies
    • parent, a raw pointer to its parent node (or nullptr, if this is the root)
    • parentFace, the adjEntry in parent corresponding to externalFace (or nullptr, if this is the root)
    • children, a vector of unique_ptrs to the child nodes
  4. For our simple application of traversing the 4-block tree bottom-up, we can use the method preorder, which calls its argument on each node of the 4-block tree in preorder. A similar method named postorder exists as well. For each node of the 4-block tree, we draw it and save the drawing under a unique filename. To this end we use PlanarStraightLayout. We use originalNodes to assign multiple occurrences of the same node in the 4-block tree the same label.