Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

PlanarSubgraphTree.h
Go to the documentation of this file.
1 
32 #pragma once
33 
38 #include <ogdf/basic/Math.h>
39 
40 namespace ogdf {
41 
44 template<typename TCost>
46 {
47 public:
48  virtual PlanarSubgraphTree *clone() const override {
49  return new PlanarSubgraphTree();
50  }
51 
52 protected:
54  const Graph &graph,
55  const List<edge> &preferredEdges,
56  List<edge> &delEdges,
57  const EdgeArray<TCost> *pCost,
58  bool preferedImplyPlanar) override {
59  delEdges.clear();
60 
61  if (pCost) {
62  GraphCopy copy(graph);
63  EdgeArray<TCost> weight(copy);
64  TCost maxCost = std::numeric_limits<TCost>::min();
65 
66  for (edge e : graph.edges) {
67  Math::updateMax(maxCost, (*pCost)[e]);
68  }
69 
70  for (edge e : copy.edges) {
71  weight[e] = maxCost - (*pCost)[copy.original(e)];
72  }
73 
74  makeMinimumSpanningTree(copy, weight);
75 
76  for (edge e : graph.edges) {
77  if (copy.copy(e) == nullptr) {
78  delEdges.pushBack(e);
79  }
80  }
81  } else if (!graph.empty()) {
82  // Will contain the parent of each node in the computed forest.
83  // parent[v] == v iff v is a root node.
84  // parent[v] == nullptr iff v wasn't visited yet.
85  NodeArray<node> parent(graph, nullptr);
86  ArrayBuffer<node> nodes(graph.numberOfNodes());
87 
88  for (node v : graph.nodes) {
89  if (parent[v] == nullptr) {
90  parent[v] = v;
91  nodes.push(v);
92 
93  while (!nodes.empty()) {
94  node u = nodes.popRet();
95 
96  for (adjEntry adj : u->adjEntries) {
97  node w = adj->twinNode();
98 
99  if (parent[w] == nullptr) {
100  parent[w] = u;
101  nodes.push(w);
102  }
103  }
104  }
105  }
106  }
107 
108  for (edge e : graph.edges) {
109  node v = e->source();
110  node w = e->target();
111 
112  bool vIsParent = v == parent[w];
113  bool wIsParent = w == parent[v];
114 
115  // Delete edges that are not in the tree.
116  // In particular, do not pick parallel edges or self-loops.
117  if (e->isSelfLoop() || (!vIsParent && !wIsParent)) {
118  delEdges.pushBack(e);
119  } else if (vIsParent) {
120  parent[w] = nullptr;
121  } else if (wIsParent) {
122  parent[v] = nullptr;
123  }
124  }
125  }
126 
127 #ifdef OGDF_DEBUG
128  NodeArray<int> tmp(graph);
129  int numberOfComponents = connectedComponents(graph, tmp);
130  int numberOfEdgesInForest = graph.numberOfEdges() - delEdges.size();
131  // Euler characteristic for forests
132  OGDF_ASSERT(numberOfEdgesInForest == graph.numberOfNodes() - numberOfComponents);
133 #endif
134 
136  }
137 };
138 
139 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
void clear()
Removes all elements from the list.
Definition: List.h:1451
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:335
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal&#39;s algorithm.
int connectedComponents(const Graph &G, NodeArray< int > &component, List< node > *isolated=nullptr)
Computes the connected components of G and optionally generates a list of isolated nodes...
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.
Maximum planar subgraph heuristic that yields a spanning tree.
ReturnType
The return type of a module.
Definition: Module.h:51
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:601
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:438
Mathematical Helpers.
Interface for planar subgraph algorithms.
static void copy(const T &from, T &to)
Definition: Alg.h:61
The solution is feasible.
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class for the representation of edges.
Definition: Graph_d.h:292
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:249
Class for the representation of nodes.
Definition: Graph_d.h:169
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:286
bool isSelfLoop() const
Returns true iff the edge is a self-loop (source node = target node).
Definition: Graph_d.h:341
int size() const
Returns the number of elements in the list.
Definition: List.h:1312
virtual PlanarSubgraphTree * clone() const override
Returns a new instance of the planar subgraph module with the same option settings.
Declaration of simple graph algorithms.
Implementation of disjoint sets data structures (union-find functionality).
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
The namespace for all OGDF objects.
Declaration of extended graph algorithms.
Declaration of interface for planar subgraph algorithms.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:93
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition: Graph_d.h:595
Class for adjacency list elements.
Definition: Graph_d.h:79
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200