Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

PlanarSubgraphTree.h
Go to the documentation of this file.
1 
32 #pragma once
33 
38 
39 namespace ogdf {
40 
43 template<typename TCost>
45 {
46 public:
47  virtual PlanarSubgraphTree *clone() const override {
48  return new PlanarSubgraphTree();
49  }
50 
51 protected:
53  const Graph &graph,
54  const List<edge> &preferredEdges,
55  List<edge> &delEdges,
56  const EdgeArray<TCost> *pCost,
57  bool preferedImplyPlanar) override {
58  delEdges.clear();
59 
60  if (pCost) {
61  GraphCopy copy(graph);
62  EdgeArray<TCost> weight(copy);
63  TCost maxCost = std::numeric_limits<TCost>::min();
64 
65  for (edge e : graph.edges) {
66  maxCost = max(maxCost, (*pCost)[e]);
67  }
68 
69  for (edge e : copy.edges) {
70  weight[e] = maxCost - (*pCost)[copy.original(e)];
71  }
72 
73  makeMinimumSpanningTree(copy, weight);
74 
75  for (edge e : graph.edges) {
76  if (copy.copy(e) == nullptr) {
77  delEdges.pushBack(e);
78  }
79  }
80  } else if (!graph.empty()) {
81  // Will contain the parent of each node in the computed forest.
82  // parent[v] == v iff v is a root node.
83  // parent[v] == nullptr iff v wasn't visited yet.
84  NodeArray<node> parent(graph, nullptr);
85  ArrayBuffer<node> nodes(graph.numberOfNodes());
86 
87  for (node v : graph.nodes) {
88  if (parent[v] == nullptr) {
89  parent[v] = v;
90  nodes.push(v);
91 
92  while (!nodes.empty()) {
93  node u = nodes.popRet();
94 
95  for (adjEntry adj : u->adjEntries) {
96  node w = adj->twinNode();
97 
98  if (parent[w] == nullptr) {
99  parent[w] = u;
100  nodes.push(w);
101  }
102  }
103  }
104  }
105  }
106 
107  for (edge e : graph.edges) {
108  node v = e->source();
109  node w = e->target();
110 
111  bool vIsParent = v == parent[w];
112  bool wIsParent = w == parent[v];
113 
114  // Delete edges that are not in the tree.
115  // In particular, do not pick parallel edges or self-loops.
116  if (e->isSelfLoop() || (!vIsParent && !wIsParent)) {
117  delEdges.pushBack(e);
118  } else if (vIsParent) {
119  parent[w] = nullptr;
120  } else if (wIsParent) {
121  parent[v] = nullptr;
122  }
123  }
124  }
125 
126 #ifdef OGDF_DEBUG
127  NodeArray<int> tmp(graph);
128  int numberOfComponents = connectedComponents(graph, tmp);
129  int numberOfEdgesInForest = graph.numberOfEdges() - delEdges.size();
130  // Euler characteristic for forests
131  OGDF_ASSERT(numberOfEdgesInForest == graph.numberOfNodes() - numberOfComponents);
132 #endif
133 
135  }
136 };
137 
138 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
void clear()
Removes all elements from the list.
Definition: List.h:1394
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:336
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:44
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal&#39;s algorithm.
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:548
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:581
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
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:105
Class for the representation of edges.
Definition: Graph_d.h:291
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:250
Class for the representation of nodes.
Definition: Graph_d.h:167
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:287
bool isSelfLoop() const
Definition: Graph_d.h:337
int size() const
Returns the number of elements in the list.
Definition: List.h:1255
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:325
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:578
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
int connectedComponents(const Graph &G, NodeArray< int > &component)
Computes the connected components of G.
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition: Graph_d.h:575
Class for adjacency list elements.
Definition: Graph_d.h:78
#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:198