Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MaximumPlanarSubgraph.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Module.h>
35 #include <ogdf/basic/Timeouter.h>
38 
42 
43 #include <ogdf/external/abacus.h>
44 
45 namespace ogdf {
46 
48 
51 template<typename TCost>
53 {
54 public:
55  // Construction
57  // Destruction
59 
60  virtual MaximumPlanarSubgraph *clone() const override { return new MaximumPlanarSubgraph(); }
61 
62 protected:
63  // Implements the Planar Subgraph interface.
64  // For the given graph \p G, a clustered graph with only
65  // a single root cluster is generated.
66  // Computes set of edges delEdges, which have to be deleted
67  // in order to get a planar subgraph; edges in preferredEdges
68  // should be contained in planar subgraph.
69  // Status: pCost and preferredEdges are ignored in current implementation.
71  const Graph &G,
72  const List<edge> &preferredEdges,
73  List<edge> &delEdges,
74  const EdgeArray<TCost> *pCost,
75  bool preferredImplyPlanar) override
76  {
77  if (G.numberOfEdges() < 9)
79 
80  //if the graph is planar, we don't have to do anything
81  if (isPlanar(G))
83 
84  //Exact ILP
86 
87  delEdges.clear();
88 
89  NodeArray<node> tableNodes(G,nullptr);
90  EdgeArray<edge> tableEdges(G,nullptr);
91  NodeArray<bool> mark(G,0);
92 
93  EdgeArray<int> componentID(G);
94 
95  // Determine biconnected components
96  int bcCount = biconnectedComponents(G,componentID);
97  OGDF_ASSERT(bcCount >= 1);
98 
99  // Determine edges per biconnected component
100  Array<SList<edge> > blockEdges(0,bcCount-1);
101  for(edge e : G.edges)
102  {
103  if (!e->isSelfLoop())
104  blockEdges[componentID[e]].pushFront(e);
105  }
106 
107  // Determine nodes per biconnected component.
108  Array<SList<node> > blockNodes(0,bcCount-1);
109  int i;
110  for (i = 0; i < bcCount; i++)
111  {
112  for (edge e : blockEdges[i])
113  {
114  if (!mark[e->source()])
115  {
116  blockNodes[i].pushBack(e->source());
117  mark[e->source()] = true;
118  }
119  if (!mark[e->target()])
120  {
121  blockNodes[i].pushBack(e->target());
122  mark[e->target()] = true;
123  }
124  }
125  for (node v : blockNodes[i])
126  mark[v] = false;
127  }
128 
129 
130  // Perform computation for every biconnected component
132  if (bcCount == 1) {
133  mr = callWithDouble(mc, G, pCost, delEdges);
134  }
135  else
136  {
137  for (i = 0; i < bcCount; i++)
138  {
139  Graph C;
140 
141  for (node v : blockNodes[i])
142  {
143  node w = C.newNode();
144  tableNodes[v] = w;
145  }
146 
147  EdgeArray<double> cost(C);
148 
149  for (edge e : blockEdges[i])
150  {
151  edge f = C.newEdge(tableNodes[e->source()],tableNodes[e->target()]);
152  tableEdges[e] = f;
153  if(pCost != nullptr) {
154  cost[tableEdges[e]] = (*pCost)[e];
155  }
156  }
157 
158  // Construct a translation table for the edges.
159  // Necessary, since edges are deleted in a new graph
160  // that represents the current biconnected component
161  // of the original graph.
162  EdgeArray<edge> backTableEdges(C,nullptr);
163  for (edge e : blockEdges[i])
164  backTableEdges[tableEdges[e]] = e;
165 
166  // The deleted edges of the biconnected component
167  List<edge> delEdgesOfBC;
168 
169  ClusterGraph CG(C);
170  mr = mc.call(CG, pCost == nullptr ? nullptr : &cost, delEdgesOfBC);
171 
172  // Abort if no optimal solution found, i.e., feasible is also not allowed
173  if (mr != Module::ReturnType::Optimal)
174  break;
175 
176  // Get the original edges that are deleted and
177  // put them on the list delEdges.
178  while (!delEdgesOfBC.empty())
179  delEdges.pushBack(backTableEdges[delEdgesOfBC.popFrontRet()]);
180 
181  }
182  }
183  return mr;
184  }
185 
186 
187 private:
188  // Call algorithm with costs as double. All underlying algorithms cast the costs to double on use eventually.
189  // However, only convert the costs if they are not given as double already.
190  template<typename U = TCost>
192  if (pCost == nullptr) {
193  return callWithDouble(mc, G, nullptr, delEdges);
194  }
195  else {
196  EdgeArray<double> dCost(G);
197  for (auto it = pCost->begin(); it != pCost->end(); ++it) {
198  dCost[it.key()] = it.value();
199  }
200  return callWithDouble(mc, G, &dCost, delEdges);
201  }
202  }
204  ClusterGraph CG(G);
205  return mc.call(CG, pCost, delEdges);
206  }
207 };
208 
209 }
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
iterator begin()
Returns an iterator to the first entry in the edge array.
Definition: EdgeArray.h:243
The solution is optimal.
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
Derived class of GraphObserver providing additional functionality to handle clustered graphs...
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
Declares base class for all module types.
iterator end()
Returns an iterator to one-past-last entry in the edge array.
Definition: EdgeArray.h:261
Exact computation of a maximum c-planar subgraph.
ReturnType
The return type of a module.
Definition: Module.h:51
Declaration of an exact c-planar subgraph algorithm, i.e., a maximum c-planar subgraph is computed us...
Representation of clustered graphs.
Definition: ClusterGraph.h:291
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
Interface for planar subgraph algorithms.
edge newEdge(node v, node w)
Creates a new edge (v,w) and returns it.
Includes Abacus.
Module::ReturnType callWithDouble(MaximumCPlanarSubgraph &mc, const Graph &G, const EdgeArray< U > *pCost, List< edge > &delEdges)
virtual MaximumPlanarSubgraph * clone() const override
Returns a new instance of the planar subgraph module with the same option settings.
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
virtual Module::ReturnType doCall(const Graph &G, const List< edge > &preferredEdges, List< edge > &delEdges, const EdgeArray< TCost > *pCost, bool preferredImplyPlanar) override
Computes the set of edges delEdges which have to be deleted to obtain the planar subgraph.
Class for the representation of edges.
Definition: Graph_d.h:292
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1417
Class for the representation of nodes.
Definition: Graph_d.h:169
ReturnType call(const ClusterGraph &G, List< edge > &delEdges)
Computes set of edges delEdges, which have to be deleted in order to get a c-planar subgraph...
Module::ReturnType callWithDouble(MaximumCPlanarSubgraph &mc, const Graph &G, const EdgeArray< double > *pCost, List< edge > &delEdges)
bool isSelfLoop() const
Returns true iff the edge is a self-loop (source node = target node).
Definition: Graph_d.h:341
Declaration of simple graph algorithms.
Declares base class for modules with timeout functionality.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
int biconnectedComponents(const Graph &G, EdgeArray< int > &component, int &nonEmptyComponents)
Computes the biconnected components of G.
The namespace for all OGDF objects.
Declaration of extended graph algorithms.
Declaration of interface for planar subgraph algorithms.
Exact computation of a maximum planar subgraph.
node newNode()
Creates a new node and returns it.
bool empty() const
Returns true iff the list is empty.
Definition: List.h:256
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41