Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

PlanarSubgraphTree.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/ArrayBuffer.h>
35 #include <ogdf/basic/Graph.h>
36 #include <ogdf/basic/GraphCopy.h>
37 #include <ogdf/basic/GraphList.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 
48 namespace ogdf {
49 
52 template<typename TCost>
54 public:
55  virtual PlanarSubgraphTree* clone() const override { return new PlanarSubgraphTree(); }
56 
57 protected:
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 
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 }
ogdf::ArrayBuffer
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition: Array.h:53
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ArrayBuffer.h
Declaration and implementation of ArrayBuffer class.
ogdf::PlanarSubgraphModule
Interface for planar subgraph algorithms.
Definition: PlanarSubgraphModule.h:53
ogdf::Graph::empty
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition: Graph_d.h:979
Graph.h
Includes declaration of graph class.
ogdf::Module::ReturnType::Feasible
@ Feasible
The solution is feasible.
ogdf::connectedComponents
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.
ogdf::PlanarSubgraphTree::doCall
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.
Definition: PlanarSubgraphTree.h:58
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::ArrayBuffer::empty
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:238
extended_graph_alg.h
Declaration of extended graph algorithms.
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:391
ogdf::ArrayBuffer::popRet
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:232
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:932
Minisat::Internal::copy
static void copy(const T &from, T &to)
Definition: Alg.h:61
ogdf::EdgeElement::isSelfLoop
bool isSelfLoop() const
Returns true iff the edge is a self-loop (source node = target node).
Definition: Graph_d.h:419
ogdf::Graph::numberOfEdges
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:985
PlanarSubgraphModule.h
Declaration of interface for planar subgraph algorithms.
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:982
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
ogdf::PlanarSubgraphTree::clone
virtual PlanarSubgraphTree * clone() const override
Returns a new instance of the planar subgraph module with the same option settings.
Definition: PlanarSubgraphTree.h:55
ogdf::ArrayBuffer::push
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:217
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
GraphCopy.h
Declaration of graph copy classes.
ogdf::List< edge >
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::PlanarSubgraphTree
Maximum planar subgraph heuristic that yields a spanning tree.
Definition: PlanarSubgraphTree.h:53
Math.h
Mathematical Helpers.
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:935
ogdf::Math::updateMax
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:94
basic.h
Basic declarations, included by all source files.
ogdf::makeMinimumSpanningTree
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal's algorithm.
Definition: extended_graph_alg.h:243
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
List.h
Declaration of doubly linked lists and iterators.
ogdf::List::size
int size() const
Returns the number of elements in the list.
Definition: List.h:1488
Module.h
Declares base class for all module types.
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
ogdf::Module::ReturnType
ReturnType
The return type of a module.
Definition: Module.h:52
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
simple_graph_alg.h
Declaration of simple graph algorithms.
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1547
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::List::clear
void clear()
Removes all elements from the list.
Definition: List.h:1626