Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

PlanarSubgraphTree.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 #include <ogdf/basic/Math.h>
39 
40 namespace ogdf {
41 
44 template<typename TCost>
46 public:
47  virtual PlanarSubgraphTree* clone() const override { return new PlanarSubgraphTree(); }
48 
49 protected:
50  virtual Module::ReturnType doCall(const Graph& graph, const List<edge>& preferredEdges,
51  List<edge>& delEdges, const EdgeArray<TCost>* pCost, bool preferedImplyPlanar) override {
52  delEdges.clear();
53 
54  if (pCost) {
55  GraphCopy copy(graph);
56  EdgeArray<TCost> weight(copy);
57  TCost maxCost = std::numeric_limits<TCost>::min();
58 
59  for (edge e : graph.edges) {
60  Math::updateMax(maxCost, (*pCost)[e]);
61  }
62 
63  for (edge e : copy.edges) {
64  weight[e] = maxCost - (*pCost)[copy.original(e)];
65  }
66 
68 
69  for (edge e : graph.edges) {
70  if (copy.copy(e) == nullptr) {
71  delEdges.pushBack(e);
72  }
73  }
74  } else if (!graph.empty()) {
75  // Will contain the parent of each node in the computed forest.
76  // parent[v] == v iff v is a root node.
77  // parent[v] == nullptr iff v wasn't visited yet.
78  NodeArray<node> parent(graph, nullptr);
79  ArrayBuffer<node> nodes(graph.numberOfNodes());
80 
81  for (node v : graph.nodes) {
82  if (parent[v] == nullptr) {
83  parent[v] = v;
84  nodes.push(v);
85 
86  while (!nodes.empty()) {
87  node u = nodes.popRet();
88 
89  for (adjEntry adj : u->adjEntries) {
90  node w = adj->twinNode();
91 
92  if (parent[w] == nullptr) {
93  parent[w] = u;
94  nodes.push(w);
95  }
96  }
97  }
98  }
99  }
100 
101  for (edge e : graph.edges) {
102  node v = e->source();
103  node w = e->target();
104 
105  bool vIsParent = v == parent[w];
106  bool wIsParent = w == parent[v];
107 
108  // Delete edges that are not in the tree.
109  // In particular, do not pick parallel edges or self-loops.
110  if (e->isSelfLoop() || (!vIsParent && !wIsParent)) {
111  delEdges.pushBack(e);
112  } else if (vIsParent) {
113  parent[w] = nullptr;
114  } else if (wIsParent) {
115  parent[v] = nullptr;
116  }
117  }
118  }
119 
120 #ifdef OGDF_DEBUG
121  NodeArray<int> tmp(graph);
122  int numberOfComponents = connectedComponents(graph, tmp);
123  int numberOfEdgesInForest = graph.numberOfEdges() - delEdges.size();
124  // Euler characteristic for forests
125  OGDF_ASSERT(numberOfEdgesInForest == graph.numberOfNodes() - numberOfComponents);
126 #endif
127 
129  }
130 };
131 
132 }
ogdf::ArrayBuffer
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition: Array.h:46
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::PlanarSubgraphModule
Interface for planar subgraph algorithms.
Definition: PlanarSubgraphModule.h:48
ogdf::Graph::empty
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition: Graph_d.h:971
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:50
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::ArrayBuffer::empty
bool empty() const
Returns true if the buffer is empty, false otherwise.
Definition: ArrayBuffer.h:230
extended_graph_alg.h
Declaration of extended graph algorithms.
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:384
ogdf::ArrayBuffer::popRet
E popRet()
Removes the newest element from the buffer and returns it.
Definition: ArrayBuffer.h:224
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:135
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:924
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:412
ogdf::Graph::numberOfEdges
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:977
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:974
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:264
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:47
ogdf::ArrayBuffer::push
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:209
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:391
ogdf::List< edge >
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:862
ogdf::PlanarSubgraphTree
Maximum planar subgraph heuristic that yields a spanning tree.
Definition: PlanarSubgraphTree.h:45
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:168
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:927
ogdf::Math::updateMax
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:90
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:233
DisjointSets.h
Implementation of disjoint sets data structures (union-find functionality).
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
ogdf::List::size
int size() const
Returns the number of elements in the list.
Definition: List.h:1478
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:394
ogdf::Module::ReturnType
ReturnType
The return type of a module.
Definition: Module.h:50
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
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:1537
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709
ogdf::List::clear
void clear()
Removes all elements from the list.
Definition: List.h:1616