Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MaximalPlanarSubgraphSimple.h
Go to the documentation of this file.
1 
32 #pragma once
33 
38 #include <ogdf/basic/Math.h>
39 #include <type_traits>
40 
41 namespace ogdf {
42 
44 
45 template<typename TCost, class Enable = void>
46 class MaximalPlanarSubgraphSimple {};
47 
49 
51 
58 template<typename TCost>
59 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_integral<TCost>::value>::type> : public PlanarSubgraphModule<TCost> {
60 
61 public:
64  : m_heuristic(*(new PlanarSubgraphEmpty<TCost>))
65  , m_deleteHeuristic(true) {}
66 
72  : m_heuristic(heuristic)
73  , m_deleteHeuristic(false) { }
74 
77  if(m_deleteHeuristic) {
78  delete &m_heuristic;
79  }
80  }
81 
83  virtual MaximalPlanarSubgraphSimple *clone() const override {
84  auto result = new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()));
85  result->m_deleteHeuristic = true; // normally a given heuristic is not deleted by the destructor
86  return result;
87  }
88 
89 protected:
90 
100  virtual Module::ReturnType
102  const Graph &graph,
103  const List<edge> &preferredEdges,
104  List<edge> &delEdges,
105  const EdgeArray<TCost> *pCost,
106  bool preferredImplyPlanar) override
107  {
108  Module::ReturnType result;
109  delEdges.clear();
110  List<edge> heuDelEdges;
111  if(pCost == nullptr) {
112  result = m_heuristic.call(graph, preferredEdges, heuDelEdges, preferredImplyPlanar);
113  } else {
114  result = m_heuristic.call(graph, *pCost, preferredEdges, heuDelEdges, preferredImplyPlanar);
115  heuDelEdges.quicksort(GenericComparer<edge, TCost>(*pCost));
116  }
117  if(Module::isSolution(result)) {
118  GraphCopy copy(graph);
119  for (edge e : heuDelEdges) {
120  copy.delEdge(copy.copy(e));
121  }
122  for (edge e : heuDelEdges) {
123  edge f = copy.newEdge(e);
124 
125  if (!isPlanar(copy)) {
126  delEdges.pushBack(e);
127  copy.delEdge(f);
128  }
129  }
130  }
131  return result;
132  }
133 
134 
135 private:
138 };
139 
140 
141 
142 template<typename TCost>
143 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_floating_point<TCost>::value>::type> : public PlanarSubgraphModule<TCost> {
144 
145 public:
153  : m_heuristic(*(new PlanarSubgraphEmpty<TCost>))
154  , m_deleteHeuristic(true)
155  , m_randomness(0.0)
156  , m_randomGenerator()
157  , m_runs(1) { }
158 
165  explicit MaximalPlanarSubgraphSimple(PlanarSubgraphModule<TCost> &heuristic, double randomness = 0.0, unsigned int runs = 1)
166  : m_heuristic(heuristic)
167  , m_deleteHeuristic(false)
168  , m_randomness(randomness)
169  , m_randomGenerator()
170  , m_runs(runs)
171  {
172  OGDF_ASSERT( runs > 0 );
173  }
174 
177  if(m_deleteHeuristic) {
178  delete &m_heuristic;
179  }
180  }
181 
183  virtual MaximalPlanarSubgraphSimple *clone() const override {
184  auto result = new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()), m_randomness, m_runs);
185  result->m_deleteHeuristic = true; // normally a given heuristic is not deleted by the destructor
186  return result;
187  }
188 
193  void setSeed(unsigned seed) {
194  m_randomGenerator.seed(seed);
195  }
196 
197 
198 protected:
199 
209  virtual Module::ReturnType
211  const Graph &graph,
212  const List<edge> &preferredEdges,
213  List<edge> &delEdges,
214  const EdgeArray<TCost> *pCost,
215  bool preferredImplyPlanar) override
216  {
218  delEdges.clear();
219 
220  // scale the costs and do multiple runs (if needed)
221  List<edge> delEdgesCurrentBest;
222 
223  // normalize costs to [0,1] and apply randomness
224  EdgeArray<TCost> normalizedCost(graph);
225 
226  for (auto i=0u; i < m_runs; i++) {
227 
228  List<edge> heuDelEdges;
229 
230  if(pCost == nullptr) {
231  result = m_heuristic.call(graph, preferredEdges, heuDelEdges, preferredImplyPlanar);
232  } else {
233  std::uniform_real_distribution<TCost> distribution (0.0,1.0);
234  edge firstEdge = graph.firstEdge();
235  TCost maxCost = firstEdge == nullptr ? 0 : (*pCost)[firstEdge];
236  TCost minCost = firstEdge == nullptr ? 0 : (*pCost)[firstEdge];
237  for (edge e: graph.edges) {
238  Math::updateMax(maxCost, (*pCost)[e]);
239  Math::updateMin(minCost, (*pCost)[e]);
240  }
241  for (edge e: graph.edges) {
242  // do not merge with first FOR !
243  // normalized = pCost transformed to [0,1]
244  TCost normalized = 1;
245  if (maxCost > minCost) {
246  normalized = ((*pCost)[e] - minCost) / (maxCost - minCost);
247  }
248  // now use randomness
249  normalizedCost[e] = (1.0 - m_randomness)*normalized + m_randomness*distribution(m_randomGenerator);
250  }
251  result = m_heuristic.call(graph, normalizedCost, preferredEdges, heuDelEdges, preferredImplyPlanar);
252  }
253 
254  if(Module::isSolution(result)) {
255  GraphCopy copy(graph);
256 
257  if (pCost != nullptr) {
258  GenericComparer<edge, TCost> cmp(normalizedCost);
259  heuDelEdges.quicksort(cmp);
260  }
261 
262  for (edge e : heuDelEdges) {
263  copy.delEdge(copy.copy(e));
264  }
265 
266  delEdgesCurrentBest.clear();
267  for (edge e : heuDelEdges) {
268  edge f = copy.newEdge(e);
269 
270  if (!isPlanar(copy)) {
271  delEdgesCurrentBest.pushBack(e);
272  copy.delEdge(f);
273  }
274  }
275 
276  if (pCost == nullptr) {
277  if (i == 0 || delEdgesCurrentBest.size() < delEdges.size()) {
278  // better solution: copy to delEdges
279  delEdges.clear();
280  for (auto e: delEdgesCurrentBest) { delEdges.pushBack(e); };
281  }
282  } else {
283  if (i == 0 || weightOfList(delEdgesCurrentBest,normalizedCost) < weightOfList(delEdges,normalizedCost)) {
284  // better solution: copy to delEdges
285  delEdges.clear();
286  for (auto e: delEdgesCurrentBest) { delEdges.pushBack(e); };
287  }
288  }
289  }
290  }
291 
292  return result;
293  }
294 
295 private:
298  double m_randomness;
299  std::default_random_engine m_randomGenerator;
300  unsigned int m_runs;
301 
307  TCost weightOfList(const List<edge>& list, const EdgeArray<TCost>& weights) {
308  TCost result = 0.0;
309  for (auto e: list) {
310  result += weights[e];
311  }
312  return result;
313  }
314 };
315 
316 }
void clear()
Removes all elements from the list.
Definition: List.h:1451
Computation was aborted due to an error.
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
virtual Module::ReturnType doCall(const Graph &graph, 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: GML.h:110
ReturnType
The return type of a module.
Definition: Module.h:51
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
virtual void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e&#39;s original edge.
MaximalPlanarSubgraphSimple(PlanarSubgraphModule< TCost > &heuristic, double randomness=0.0, unsigned int runs=1)
Constructor.
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition: Math.h:101
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
void quicksort()
Sorts the list using Quicksort.
Definition: List.h:1152
Mathematical Helpers.
Interface for planar subgraph algorithms.
edge firstEdge() const
Returns the first edge in the list of all edges.
Definition: Graph_d.h:623
static void copy(const T &from, T &to)
Definition: Alg.h:61
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
Class for the representation of edges.
Definition: Graph_d.h:292
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:249
std::default_random_engine m_randomGenerator
random generator to use with std::uniform_real_distribution
void setSeed(unsigned seed)
set seed of std::default_random_engine generator to use when randomness > 0
Declaration and Implementation of class PlanarSubgraphEmpty.
double m_randomness
randomness of the process: use 0 to compute everything based on the costs, use 1 for completely rando...
int size() const
Returns the number of elements in the list.
Definition: List.h:1312
Declaration of simple graph algorithms.
Implementation of disjoint sets data structures (union-find functionality).
static bool isSolution(ReturnType ret)
Returns true iff ret indicates that the module returned a feasible solution.
Definition: Module.h:66
The namespace for all OGDF objects.
Declaration of extended graph algorithms.
virtual Module::ReturnType doCall(const Graph &graph, 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.
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:93
MaximalPlanarSubgraphSimple(PlanarSubgraphModule< TCost > &heuristic)
Constructor with user given heuristic that is executed before extending the solution to maximality...
Compare elements based on a single comparable attribute.
Definition: comparer.h:377
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
Dummy implementation for maximum planar subgraph that returns an empty graph.