Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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