Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
PlanarSubgraphTree.h
Go to the documentation of this file.
1
32#pragma once
33
35#include <ogdf/basic/Graph.h>
38#include <ogdf/basic/List.h>
39#include <ogdf/basic/Math.h>
40#include <ogdf/basic/Module.h>
41#include <ogdf/basic/basic.h>
45
46#include <limits>
47
48namespace ogdf {
49
52template<typename TCost>
54public:
55 virtual PlanarSubgraphTree* clone() const override { return new PlanarSubgraphTree(); }
56
57protected:
58 virtual Module::ReturnType doCall(const Graph& graph, const List<edge>& preferredEdges,
59 List<edge>& delEdges, const EdgeArray<TCost>* pCost, bool preferedImplyPlanar) override {
60 delEdges.clear();
61
62 if (pCost) {
63 GraphCopy copy(graph);
64 EdgeArray<TCost> weight(copy);
65 TCost maxCost = std::numeric_limits<TCost>::min();
66
67 for (edge e : graph.edges) {
68 Math::updateMax(maxCost, (*pCost)[e]);
69 }
70
71 for (edge e : copy.edges) {
72 weight[e] = maxCost - (*pCost)[copy.original(e)];
73 }
74
75 makeMinimumSpanningTree(copy, weight);
76
77 for (edge e : graph.edges) {
78 if (copy.copy(e) == nullptr) {
79 delEdges.pushBack(e);
80 }
81 }
82 } else if (!graph.empty()) {
83 // Will contain the parent of each node in the computed forest.
84 // parent[v] == v iff v is a root node.
85 // parent[v] == nullptr iff v wasn't visited yet.
86 NodeArray<node> parent(graph, nullptr);
87 ArrayBuffer<node> nodes(graph.numberOfNodes());
88
89 for (node v : graph.nodes) {
90 if (parent[v] == nullptr) {
91 parent[v] = v;
92 nodes.push(v);
93
94 while (!nodes.empty()) {
95 node u = nodes.popRet();
96
97 for (adjEntry adj : u->adjEntries) {
98 node w = adj->twinNode();
99
100 if (parent[w] == nullptr) {
101 parent[w] = u;
102 nodes.push(w);
103 }
104 }
105 }
106 }
107 }
108
109 for (edge e : graph.edges) {
110 node v = e->source();
111 node w = e->target();
112
113 bool vIsParent = v == parent[w];
114 bool wIsParent = w == parent[v];
115
116 // Delete edges that are not in the tree.
117 // In particular, do not pick parallel edges or self-loops.
118 if (e->isSelfLoop() || (!vIsParent && !wIsParent)) {
119 delEdges.pushBack(e);
120 } else if (vIsParent) {
121 parent[w] = nullptr;
122 } else if (wIsParent) {
123 parent[v] = nullptr;
124 }
125 }
126 }
127
128#ifdef OGDF_DEBUG
129 NodeArray<int> tmp(graph);
130 int numberOfComponents = connectedComponents(graph, tmp);
131 int numberOfEdgesInForest = graph.numberOfEdges() - delEdges.size();
132 // Euler characteristic for forests
133 OGDF_ASSERT(numberOfEdgesInForest == graph.numberOfNodes() - numberOfComponents);
134#endif
135
137 }
138};
139
140}
Declaration and implementation of ArrayBuffer class.
Includes declaration of graph class.
Declaration of graph copy classes.
Decralation of GraphElement and GraphList classes.
Declaration of doubly linked lists and iterators.
Declares base class for all module types.
Declaration of interface for planar subgraph algorithms.
Mathematical Helpers.
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition ArrayBuffer.h:64
E popRet()
Removes the newest element from the buffer and returns it.
void push(E e)
Puts a new element in the buffer.
bool empty() const
Returns true if the buffer is empty, false otherwise.
Class for the representation of edges.
Definition Graph_d.h:364
const Graph & original() const
Returns a reference to the original graph.
Definition GraphCopy.h:104
Copies of graphs supporting edge splitting.
Definition GraphCopy.h:390
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition GraphCopy.h:463
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition Graph_d.h:979
int numberOfEdges() const
Returns the number of edges in the graph.
Definition Graph_d.h:982
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition Graph_d.h:929
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition Graph_d.h:976
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition Graph_d.h:932
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
int size() const
Returns the number of elements in the list.
Definition List.h:1488
void clear()
Removes all elements from the list.
Definition List.h:1626
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition List.h:1547
ReturnType
The return type of a module.
Definition Module.h:52
@ Feasible
The solution is feasible.
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
Interface for planar subgraph algorithms.
Maximum planar subgraph heuristic that yields a spanning tree.
virtual PlanarSubgraphTree * clone() const override
Returns a new instance of the planar subgraph module with the same option settings.
virtual Module::ReturnType doCall(const Graph &graph, const List< edge > &preferredEdges, List< edge > &delEdges, const EdgeArray< TCost > *pCost, bool preferedImplyPlanar) override
Computes the set of edges delEdges which have to be deleted to obtain the planar subgraph.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
Declaration of extended graph algorithms.
int connectedComponents(const Graph &G, NodeArray< int > &component, List< node > *isolated=nullptr, ArrayBuffer< node > *reprs=nullptr)
Computes the connected components of G and optionally generates a list of isolated nodes.
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal's algorithm.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition Math.h:94
The namespace for all OGDF objects.
Declaration of simple graph algorithms.