Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MaximalPlanarSubgraphSimple.h
Go to the documentation of this file.
1 
32 #pragma once
33 
38 #include <type_traits>
39 
40 namespace ogdf {
41 
43 
44 namespace MaximalPlanarSubgraphSimpleInternal {
45 template<typename TCost>
46 class EdgeComparer {
47 private:
48  const EdgeArray<TCost> &m_weight;
49 public:
50  explicit EdgeComparer(const EdgeArray<TCost> &weight) : m_weight(weight) { }
51 
52  bool less(const edge &x, const edge &y) const {
53  return m_weight[x] < m_weight[y];
54  }
55 };
56 }
57 
58 
59 template<typename TCost, class Enable = void>
60 class MaximalPlanarSubgraphSimple {};
61 
63 
65 
72 template<typename TCost>
73 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_integral<TCost>::value>::type> : public PlanarSubgraphModule<TCost> {
74 
75 public:
78  : m_heuristic(*(new PlanarSubgraphEmpty<TCost>))
79  , m_deleteHeuristic(true) {}
80 
86  : m_heuristic(heuristic)
87  , m_deleteHeuristic(false) { }
88 
91  if(m_deleteHeuristic) {
92  delete &m_heuristic;
93  }
94  }
95 
97  virtual MaximalPlanarSubgraphSimple *clone() const override {
98  auto result = new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()));
99  result->m_deleteHeuristic = true; // normally a given heuristic is not deleted by the destructor
100  return result;
101  }
102 
103 protected:
104 
114  virtual Module::ReturnType
116  const Graph &graph,
117  const List<edge> &preferredEdges,
118  List<edge> &delEdges,
119  const EdgeArray<TCost> *pCost,
120  bool preferredImplyPlanar) override
121  {
122  Module::ReturnType result;
123  delEdges.clear();
124  List<edge> heuDelEdges;
125  if(pCost == nullptr) {
126  result = m_heuristic.call(graph, preferredEdges, heuDelEdges, preferredImplyPlanar);
127  } else {
128  result = m_heuristic.call(graph, *pCost, preferredEdges, heuDelEdges, preferredImplyPlanar);
129  MaximalPlanarSubgraphSimpleInternal::EdgeComparer<TCost> cmp(*pCost);
130  heuDelEdges.quicksort(cmp);
131  }
132  if(Module::isSolution(result)) {
133  GraphCopy copy(graph);
134  for (edge e : heuDelEdges) {
135  copy.delEdge(copy.copy(e));
136  }
137  for (edge e : heuDelEdges) {
138  edge f = copy.newEdge(e);
139 
140  if (!isPlanar(copy)) {
141  delEdges.pushBack(e);
142  copy.delEdge(f);
143  }
144  }
145  }
146  return result;
147  }
148 
149 
150 private:
153 };
154 
155 
156 
157 template<typename TCost>
158 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_floating_point<TCost>::value>::type> : public PlanarSubgraphModule<TCost> {
159 
160 public:
168  : m_heuristic(*(new PlanarSubgraphEmpty<TCost>))
169  , m_deleteHeuristic(true)
170  , m_randomness(0.0)
171  , m_randomGenerator()
172  , m_runs(1) { }
173 
180  explicit MaximalPlanarSubgraphSimple(PlanarSubgraphModule<TCost> &heuristic, double randomness = 0.0, unsigned int runs = 1)
181  : m_heuristic(heuristic)
182  , m_deleteHeuristic(false)
183  , m_randomness(randomness)
184  , m_randomGenerator()
185  , m_runs(runs) { }
186 
189  if(m_deleteHeuristic) {
190  delete &m_heuristic;
191  }
192  }
193 
195  virtual MaximalPlanarSubgraphSimple *clone() const override {
196  auto result = new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()), m_randomness, m_runs);
197  result->m_deleteHeuristic = true; // normally a given heuristic is not deleted by the destructor
198  return result;
199  }
200 
205  void setSeed(unsigned seed) {
206  m_randomGenerator.seed(seed);
207  }
208 
209 
210 protected:
211 
221  virtual Module::ReturnType
223  const Graph &graph,
224  const List<edge> &preferredEdges,
225  List<edge> &delEdges,
226  const EdgeArray<TCost> *pCost,
227  bool preferredImplyPlanar) override
228  {
229  Module::ReturnType result;
230  delEdges.clear();
231 
232  // scale the costs and do multiple runs (if needed)
233  List<edge> delEdgesCurrentBest;
234 
235  // normalize costs to [0,1] and apply randomness
236  EdgeArray<TCost> normalizedCost(graph);
237 
238  for (auto i=0u; i < m_runs; i++) {
239 
240  List<edge> heuDelEdges;
241 
242  if(pCost == nullptr) {
243  result = m_heuristic.call(graph, preferredEdges, heuDelEdges, preferredImplyPlanar);
244  } else {
245  std::uniform_real_distribution<TCost> distribution (0.0,1.0);
246  TCost maxCost = (*pCost)[graph.firstEdge()];
247  TCost minCost = (*pCost)[graph.firstEdge()];
248  for (edge e: graph.edges) {
249  maxCost = max(maxCost, (*pCost)[e]);
250  minCost = min(minCost, (*pCost)[e]);
251  }
252  for (edge e: graph.edges) {
253  // do not merge with first FOR !
254  // normalized = pCost transformed to [0,1]
255  TCost normalized = ((*pCost)[e] - minCost) / (maxCost - minCost);
256  // now use randomness
257  normalizedCost[e] = (1.0 - m_randomness)*normalized + m_randomness*distribution(m_randomGenerator);
258  }
259  result = m_heuristic.call(graph, normalizedCost, preferredEdges, heuDelEdges, preferredImplyPlanar);
260  }
261 
262  if(Module::isSolution(result)) {
263  GraphCopy copy(graph);
264 
265  if (pCost != nullptr) {
266  MaximalPlanarSubgraphSimpleInternal::EdgeComparer<TCost> cmp(normalizedCost);
267  heuDelEdges.quicksort(cmp);
268  }
269 
270  for (edge e : heuDelEdges) {
271  copy.delEdge(copy.copy(e));
272  }
273 
274  delEdgesCurrentBest.clear();
275  for (edge e : heuDelEdges) {
276  edge f = copy.newEdge(e);
277 
278  if (!isPlanar(copy)) {
279  delEdgesCurrentBest.pushBack(e);
280  copy.delEdge(f);
281  }
282  }
283 
284  if (pCost == nullptr) {
285  if (i == 0 || delEdgesCurrentBest.size() < delEdges.size()) {
286  // better solution: copy to delEdges
287  delEdges.clear();
288  for (auto e: delEdgesCurrentBest) { delEdges.pushBack(e); };
289  }
290  } else {
291  if (i == 0 || weightOfList(delEdgesCurrentBest,normalizedCost) < weightOfList(delEdges,normalizedCost)) {
292  // better solution: copy to delEdges
293  delEdges.clear();
294  for (auto e: delEdgesCurrentBest) { delEdges.pushBack(e); };
295  }
296  }
297  }
298  }
299 
300  return result;
301  }
302 
303 private:
306  double m_randomness;
307  std::default_random_engine m_randomGenerator;
308  unsigned int m_runs;
309 
315  TCost weightOfList(const List<edge>& list, const EdgeArray<TCost>& weights) {
316  TCost result = 0.0;
317  for (auto e: list) {
318  result += weights[e];
319  }
320  return result;
321  }
322 };
323 
324 }
void clear()
Removes all elements from the list.
Definition: List.h:1394
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:66
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
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.
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.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:548
void quicksort()
Sorts the list using Quicksort.
Definition: List.h:1097
Interface for planar subgraph algorithms.
edge firstEdge() const
Returns the first edge in the list of all edges.
Definition: Graph_d.h:603
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:291
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:250
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:1255
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.
MaximalPlanarSubgraphSimple(PlanarSubgraphModule< TCost > &heuristic)
Constructor with user given heuristic that is executed before extending the solution to maximality...
Dummy implementation for maximum planar subgraph that returns an empty graph.