Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MaximalPlanarSubgraphSimple.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
35 #include <ogdf/basic/GraphCopy.h>
36 #include <ogdf/basic/GraphList.h>
37 #include <ogdf/basic/List.h>
38 #include <ogdf/basic/Math.h>
39 #include <ogdf/basic/Module.h>
40 #include <ogdf/basic/basic.h>
41 #include <ogdf/basic/comparer.h>
45 
46 #include <random>
47 #include <type_traits>
48 
49 namespace ogdf {
50 
52 
53 template<typename TCost, class Enable = void>
54 class MaximalPlanarSubgraphSimple { };
55 
57 
59 
66 template<typename TCost>
67 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_integral<TCost>::value>::type>
68  : public PlanarSubgraphModule<TCost> {
69 public:
72  : m_heuristic(*(new PlanarSubgraphEmpty<TCost>)), m_deleteHeuristic(true) { }
73 
79  : m_heuristic(heuristic), m_deleteHeuristic(false) { }
80 
83  if (m_deleteHeuristic) {
84  delete &m_heuristic;
85  }
86  }
87 
89  virtual MaximalPlanarSubgraphSimple* clone() const override {
90  auto result = new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()));
91  result->m_deleteHeuristic =
92  true; // normally a given heuristic is not deleted by the destructor
93  return result;
94  }
95 
96 protected:
106  virtual Module::ReturnType doCall(const Graph& graph, const List<edge>& preferredEdges,
107  List<edge>& delEdges, const EdgeArray<TCost>* pCost, bool preferredImplyPlanar) override {
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,
115  preferredImplyPlanar);
116  heuDelEdges.quicksort(GenericComparer<edge, TCost>(*pCost));
117  }
118  if (Module::isSolution(result)) {
119  GraphCopy copy(graph);
120  for (edge e : heuDelEdges) {
121  copy.delEdge(copy.copy(e));
122  }
123  for (edge e : heuDelEdges) {
124  edge f = copy.newEdge(e);
125 
126  if (!isPlanar(copy)) {
127  delEdges.pushBack(e);
128  copy.delEdge(f);
129  }
130  }
131  }
132  return result;
133  }
134 
135 
136 private:
139 };
140 
141 template<typename TCost>
142 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_floating_point<TCost>::value>::type>
143  : public PlanarSubgraphModule<TCost> {
144 public:
152  : m_heuristic(*(new PlanarSubgraphEmpty<TCost>))
153  , m_deleteHeuristic(true)
154  , m_randomness(0.0)
155  , m_randomGenerator()
156  , m_runs(1) { }
157 
165  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  OGDF_ASSERT(runs > 0);
172  }
173 
176  if (m_deleteHeuristic) {
177  delete &m_heuristic;
178  }
179  }
180 
182  virtual MaximalPlanarSubgraphSimple* clone() const override {
183  auto result = new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()), m_randomness, m_runs);
184  result->m_deleteHeuristic =
185  true; // normally a given heuristic is not deleted by the destructor
186  return result;
187  }
188 
193  void setSeed(unsigned seed) { m_randomGenerator.seed(seed); }
194 
195 
196 protected:
206  virtual Module::ReturnType doCall(const Graph& graph, const List<edge>& preferredEdges,
207  List<edge>& delEdges, const EdgeArray<TCost>* pCost, bool preferredImplyPlanar) override {
209  delEdges.clear();
210 
211  // scale the costs and do multiple runs (if needed)
212  List<edge> delEdgesCurrentBest;
213 
214  // normalize costs to [0,1] and apply randomness
215  EdgeArray<TCost> normalizedCost(graph);
216 
217  for (auto i = 0u; i < m_runs; i++) {
218  List<edge> heuDelEdges;
219 
220  if (pCost == nullptr) {
221  result = m_heuristic.call(graph, preferredEdges, heuDelEdges, preferredImplyPlanar);
222  } else {
223  std::uniform_real_distribution<TCost> distribution(0.0, 1.0);
224  edge firstEdge = graph.firstEdge();
225  TCost maxCost = firstEdge == nullptr ? 0 : (*pCost)[firstEdge];
226  TCost minCost = firstEdge == nullptr ? 0 : (*pCost)[firstEdge];
227  for (edge e : graph.edges) {
228  Math::updateMax(maxCost, (*pCost)[e]);
229  Math::updateMin(minCost, (*pCost)[e]);
230  }
231  for (edge e : graph.edges) {
232  // do not merge with first FOR !
233  // normalized = pCost transformed to [0,1]
234  TCost normalized = 1;
235  if (maxCost > minCost) {
236  normalized = ((*pCost)[e] - minCost) / (maxCost - minCost);
237  }
238  // now use randomness
239  normalizedCost[e] = (1.0 - m_randomness) * normalized
240  + m_randomness * distribution(m_randomGenerator);
241  }
242  result = m_heuristic.call(graph, normalizedCost, preferredEdges, heuDelEdges,
243  preferredImplyPlanar);
244  }
245 
246  if (Module::isSolution(result)) {
247  GraphCopy copy(graph);
248 
249  if (pCost != nullptr) {
250  GenericComparer<edge, TCost> cmp(normalizedCost);
251  heuDelEdges.quicksort(cmp);
252  }
253 
254  for (edge e : heuDelEdges) {
255  copy.delEdge(copy.copy(e));
256  }
257 
258  delEdgesCurrentBest.clear();
259  for (edge e : heuDelEdges) {
260  edge f = copy.newEdge(e);
261 
262  if (!isPlanar(copy)) {
263  delEdgesCurrentBest.pushBack(e);
264  copy.delEdge(f);
265  }
266  }
267 
268  if (pCost == nullptr) {
269  if (i == 0 || delEdgesCurrentBest.size() < delEdges.size()) {
270  // better solution: copy to delEdges
271  delEdges.clear();
272  for (auto e : delEdgesCurrentBest) {
273  delEdges.pushBack(e);
274  };
275  }
276  } else {
277  if (i == 0
278  || weightOfList(delEdgesCurrentBest, normalizedCost)
279  < weightOfList(delEdges, normalizedCost)) {
280  // better solution: copy to delEdges
281  delEdges.clear();
282  for (auto e : delEdgesCurrentBest) {
283  delEdges.pushBack(e);
284  };
285  }
286  }
287  }
288  }
289 
290  return result;
291  }
292 
293 private:
296  double m_randomness;
297  std::default_random_engine m_randomGenerator;
298  unsigned int m_runs;
299 
305  TCost weightOfList(const List<edge>& list, const EdgeArray<TCost>& weights) {
306  TCost result = 0.0;
307  for (auto e : list) {
308  result += weights[e];
309  }
310  return result;
311  }
312 };
313 
314 }
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::m_deleteHeuristic
bool m_deleteHeuristic
flag to store we have to delete a self created heuristic
Definition: MaximalPlanarSubgraphSimple.h:295
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::Module::isSolution
static bool isSolution(ReturnType ret)
Returns true iff ret indicates that the module returned a feasible solution.
Definition: Module.h:67
ogdf::PlanarSubgraphModule
Interface for planar subgraph algorithms.
Definition: PlanarSubgraphModule.h:53
Graph.h
Includes declaration of graph class.
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_integral< TCost >::value >::type >::MaximalPlanarSubgraphSimple
MaximalPlanarSubgraphSimple(PlanarSubgraphModule< TCost > &heuristic)
Constructor with user given heuristic that is executed before extending the solution to maximality.
Definition: MaximalPlanarSubgraphSimple.h:78
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_integral< TCost >::value >::type >::m_heuristic
PlanarSubgraphModule< TCost > & m_heuristic
user given heuristic
Definition: MaximalPlanarSubgraphSimple.h:137
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::MaximalPlanarSubgraphSimple
MaximalPlanarSubgraphSimple(PlanarSubgraphModule< TCost > &heuristic, double randomness=0.0, unsigned int runs=1)
Constructor.
Definition: MaximalPlanarSubgraphSimple.h:164
extended_graph_alg.h
Declaration of extended graph algorithms.
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::MaximalPlanarSubgraphSimple
MaximalPlanarSubgraphSimple()
Constructor.
Definition: MaximalPlanarSubgraphSimple.h:151
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::~MaximalPlanarSubgraphSimple
virtual ~MaximalPlanarSubgraphSimple()
Destructor.
Definition: MaximalPlanarSubgraphSimple.h:175
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:391
ogdf::isPlanar
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
Definition: extended_graph_alg.h:284
PlanarSubgraphEmpty.h
Declaration and Implementation of class PlanarSubgraphEmpty. Heuristic: We obtain a planar subgraph b...
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_integral< TCost >::value >::type >::clone
virtual MaximalPlanarSubgraphSimple * clone() const override
Clone method.
Definition: MaximalPlanarSubgraphSimple.h:89
Minisat::Internal::copy
static void copy(const T &from, T &to)
Definition: Alg.h:61
ogdf::Module::ReturnType::Error
@ Error
Computation was aborted due to an error.
PlanarSubgraphModule.h
Declaration of interface for planar subgraph algorithms.
backward::Color::type
type
Definition: backward.hpp:1716
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_integral< TCost >::value >::type >::MaximalPlanarSubgraphSimple
MaximalPlanarSubgraphSimple()
Constructor.
Definition: MaximalPlanarSubgraphSimple.h:71
ogdf::Math::updateMin
void updateMin(T &min, const T &newValue)
Stores the minimum of min and newValue in min.
Definition: Math.h:102
GraphCopy.h
Declaration of graph copy classes.
ogdf::List< edge >
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
Math.h
Mathematical Helpers.
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::m_randomness
double m_randomness
randomness of the process: use 0 to compute everything based on the costs, use 1 for completely rando...
Definition: MaximalPlanarSubgraphSimple.h:296
ogdf::Graph::firstEdge
edge firstEdge() const
Returns the first edge in the list of all edges.
Definition: Graph_d.h:1003
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:935
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::m_runs
unsigned int m_runs
number of runs when algorithms is randomized
Definition: MaximalPlanarSubgraphSimple.h:298
ogdf::Math::updateMax
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:94
basic.h
Basic declarations, included by all source files.
std
Definition: GML.h:111
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_integral< TCost >::value >::type >::~MaximalPlanarSubgraphSimple
virtual ~MaximalPlanarSubgraphSimple()
Desctructor.
Definition: MaximalPlanarSubgraphSimple.h:82
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::weightOfList
TCost weightOfList(const List< edge > &list, const EdgeArray< TCost > &weights)
Definition: MaximalPlanarSubgraphSimple.h:305
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::doCall
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: MaximalPlanarSubgraphSimple.h:206
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::m_heuristic
PlanarSubgraphModule< TCost > & m_heuristic
user given heuristic
Definition: MaximalPlanarSubgraphSimple.h:294
ogdf::PlanarSubgraphEmpty
Dummy implementation for maximum planar subgraph that returns an empty graph.
Definition: PlanarSubgraphEmpty.h:48
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
List.h
Declaration of doubly linked lists and iterators.
ogdf::List::size
int size() const
Returns the number of elements in the list.
Definition: List.h:1488
Module.h
Declares base class for all module types.
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_integral< TCost >::value >::type >::doCall
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: MaximalPlanarSubgraphSimple.h:106
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::m_randomGenerator
std::default_random_engine m_randomGenerator
random generator to use with std::uniform_real_distribution
Definition: MaximalPlanarSubgraphSimple.h:297
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::clone
virtual MaximalPlanarSubgraphSimple * clone() const override
Clone method.
Definition: MaximalPlanarSubgraphSimple.h:182
ogdf::Module::ReturnType
ReturnType
The return type of a module.
Definition: Module.h:52
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_integral< TCost >::value >::type >::m_deleteHeuristic
bool m_deleteHeuristic
flag to store we have to delete a self created heuristic
Definition: MaximalPlanarSubgraphSimple.h:138
comparer.h
Declarations for Comparer objects.
ogdf::GenericComparer
Compare elements based on a single comparable attribute.
Definition: comparer.h:42
ogdf::MaximalPlanarSubgraphSimple< TCost, typename std::enable_if< std::is_floating_point< TCost >::value >::type >::setSeed
void setSeed(unsigned seed)
set seed of std::default_random_engine generator to use when randomness > 0
Definition: MaximalPlanarSubgraphSimple.h:193
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1547
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::List::clear
void clear()
Removes all elements from the list.
Definition: List.h:1626