Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 <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",
});
return 0;
}

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.
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::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::GraphAttributes::edgeStyle
static const long edgeStyle
Corresponds to edge attributes strokeColor(edge), strokeType(edge), and strokeWidth(edge).
Definition: GraphAttributes.h:143
ogdf::GraphAttributes::nodeStyle
static const long nodeStyle
Corresponds to node attributes strokeColor(node), strokeType(node), strokeWidth(node),...
Definition: GraphAttributes.h:147
ogdf::GraphIO::drawSVG
static bool drawSVG(const GraphAttributes &A, std::ostream &os, const SVGSettings &settings)
ogdf::NodeElement::index
int index() const
Returns the (unique) node index.
Definition: Graph_d.h:267
PlanarStraightLayout.h
Declaration of class PlanarStraightLayout which represents a planar straight-line drawing algorithm.
ogdf::FourBlockTree::g
std::unique_ptr< Graph > g
The 4-connected component.
Definition: FourBlockTree.h:74
ogdf::PlanarGridLayoutModule::callFixEmbed
void callFixEmbed(GraphAttributes &AG, adjEntry adjExternal=nullptr)
Calls the grid layout algorithm with a fixed planar embedding (general call).
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
ogdf::Color::Name::White
@ White
randomized.h
Declaration of randomized graph generators.
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:924
ogdf::FourBlockTree::construct
static std::unique_ptr< FourBlockTree > construct(const Graph &g, adjEntry externalFace)
Construct a 4-block tree of the given graph.
ogdf::FourBlockTree::originalNodes
NodeArray< node > originalNodes
The nodes in the original graph corresponding to the nodes in g.
Definition: FourBlockTree.h:82
GraphIO.h
Declares class GraphIO which provides access to all graph read and write functionality.
FourBlockTree.h
Declaration of FourBlockTree.
ogdf::Graph::firstNode
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:989
main
int main()
Definition: gen-acyclic-graph.cpp:7
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::GraphAttributes::nodeLabel
static const long nodeLabel
Corresponds to node attribute label(node).
Definition: GraphAttributes.h:128
ogdf::AdjElement::cyclicSucc
adjEntry cyclicSucc() const
Returns the cyclic successor in the adjacency list.
Definition: Graph_d.h:347
ogdf::FourBlockTree
A node in a 4-block tree.
Definition: FourBlockTree.h:59
ogdf::GraphAttributes::edgeGraphics
static const long edgeGraphics
Corresponds to edge attribute bends(edge).
Definition: GraphAttributes.h:116
ogdf::NodeElement::firstAdj
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:279
ogdf::FourBlockTree::externalFace
adjEntry externalFace
A half-edge in g such that the external face of g is to its right.
Definition: FourBlockTree.h:87
ogdf::PlanarStraightLayout
Implementation of the Planar-Straight layout algorithm.
Definition: PlanarStraightLayout.h:113
ogdf::sync_plan::internal::to_string
std::string to_string(const std::function< std::ostream &(std::ostream &)> &func)
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
ogdf::GraphAttributes::nodeGraphics
static const long nodeGraphics
Corresponds to node attributes x(node), y(node), width(node), height(node), and shape(node).
Definition: GraphAttributes.h:113
simple_graph_alg.h
Declaration of simple graph algorithms.
ogdf::randomPlanarConnectedGraph
void randomPlanarConnectedGraph(Graph &G, int n, int m)
Creates a random connected (simple) planar (embedded) graph.