Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

PlanarSubgraphFast.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 #include <ogdf/basic/STNumbering.h>
39 #include <ogdf/basic/Thread.h>
40 #include <atomic>
41 
42 namespace ogdf {
43 
67 template<typename TCost>
69 
70  using BlockType = std::pair<Graph*,EdgeArray<edge>*>;
71 
72  class ThreadMaster {
75  int m_nBlocks;
78  std::atomic<int> m_runs;
79  std::mutex m_mutex;
80 
81  public:
83  : m_bestSolution(block.size())
84  , m_bestDelEdges(block.size())
85  , m_nBlocks(block.size())
86  , m_block(block)
87  , m_pCost(pCost)
88  , m_runs(runs)
89  {
90  for(int i = 0; i < m_nBlocks; ++i) {
91  m_bestDelEdges[i] = nullptr;
92  m_bestSolution[i] = (m_block[i].first != nullptr) ? std::numeric_limits<int>::max() : 0;
93  }
94  }
95 
96  int numBlocks() const {
97  return m_nBlocks;
98  }
99 
100  const Graph &block(int i) const {
101  return *m_block[i].first;
102  }
103 
104  bool considerBlock(int i) const {
105  return m_bestSolution[i] > 1;
106  }
107 
108  List<edge> *postNewResult(int i, List<edge> *pNewDelEdges) {
109  TCost newSolution = pNewDelEdges->size();
110  if(m_pCost != nullptr) {
111  const EdgeArray<edge> &origEdge = *m_block[i].second;
112  newSolution = TCost(0);
113  for(edge e : *pNewDelEdges)
114  newSolution += (*m_pCost)[origEdge[e]];
115  }
116 
117  // m_mutex is automatically released when guard goes out of scope
118  std::lock_guard<std::mutex> guard(m_mutex);
119 
120  if(newSolution < m_bestSolution[i]) {
121  std::swap(pNewDelEdges, m_bestDelEdges[i]);
122  m_bestSolution[i] = newSolution;
123  }
124 
125  return pNewDelEdges;
126  }
127 
128  // creates a solution for the original graph from best solutions per block
129  // also deletes (intermediate) solution lists
130  void buildSolution(List<edge> &delEdges) {
131  for(int i = 0; i < m_nBlocks; ++i) {
132  if(m_bestDelEdges[i] != nullptr) {
133  const EdgeArray<edge> &origEdge = *m_block[i].second;
134  for(edge e : *m_bestDelEdges[i])
135  delEdges.pushBack(origEdge[e]);
136  delete m_bestDelEdges[i];
137  }
138  }
139  }
140 
141  bool getNextRun() {
142  return --m_runs >= 0;
143  }
144  };
145 
146  class Worker {
147  ThreadMaster *m_pMaster; // associated master
148 
149  public:
150  Worker(ThreadMaster *pMaster) : m_pMaster(pMaster) { }
151  ~Worker() = default;
152 
153  void operator()() {
154  doWorkHelper(*m_pMaster);
155  }
156 
157  private:
158  Worker(const Worker &other); // = delete
159  Worker &operator=(const Worker &other); // = delete
160  };
161 
162 public:
165  m_nRuns = 10;
166  }
167 
169  virtual PlanarSubgraphFast *clone() const override {
170  auto* res = new PlanarSubgraphFast<TCost>(*this);
171  res->m_nRuns = m_nRuns;
172  return res;
173  }
174 
176  void runs (int nRuns) {
177  m_nRuns = nRuns;
178  }
179 
181  int runs() const {
182  return m_nRuns;
183  }
184 
185 
186 protected:
188 
192  const Graph &G,
193  const List<edge> &preferedEdges,
194  List<edge> &delEdges,
195  const EdgeArray<TCost> *pCost,
196  bool preferedImplyPlanar) override
197  {
198  delEdges.clear();
199 
200  if (G.numberOfEdges() < 9)
202 
203  // Determine Biconnected Components
204  EdgeArray<int> componentID(G);
205  int nBlocks = biconnectedComponents(G,componentID);
206 
207  // Determine edges per biconnected component
208  Array<SList<edge>> blockEdges(0,nBlocks-1);
209  for(edge e : G.edges) {
210  if (!e->isSelfLoop())
211  blockEdges[componentID[e]].pushFront(e);
212  }
213 
214  // Build non-trivial blocks
215  Array<BlockType> block(nBlocks);
216  NodeArray<node> copyV(G,nullptr);
217 
218  for(int i = 0; i < nBlocks; i++) {
219  if(blockEdges[i].size() < 9) {
220  block[i] = BlockType((Graph*)nullptr, (EdgeArray<edge>*)nullptr); // explicit casts required for VS2010
221  continue;
222  }
223 
224  Graph *bc = new Graph;
225  EdgeArray<edge> *origE = new EdgeArray<edge>(*bc,nullptr);
226  block[i] = BlockType(bc,origE);
227 
228  SList<node> marked;
229  for (edge e : blockEdges[i]) {
230  if (copyV[e->source()] == nullptr) {
231  copyV[e->source()] = bc->newNode();
232  marked.pushBack(e->source());
233  }
234  if (copyV[e->target()] == nullptr) {
235  copyV[e->target()] = bc->newNode();
236  marked.pushBack(e->target());
237  }
238 
239  (*origE)[bc->newEdge(copyV[e->source()],copyV[e->target()])] = e;
240  }
241 
242  for (node v : marked)
243  copyV[v] = nullptr;
244  }
245  copyV.init();
246 
247  int nRuns = max(1, m_nRuns);
248  unsigned int nThreads = min(this->maxThreads(), (unsigned int)nRuns);
249 
250  if(nThreads == 1)
251  seqCall(block, pCost, nRuns, (m_nRuns == 0), delEdges);
252  else
253  parCall(block, pCost, nRuns, nThreads, delEdges);
254 
255  // clean-up
256  for(int i = 0; i < nBlocks; i++) {
257  delete block[i].first;
258  delete block[i].second;
259  }
260 
262  }
263 
264 
265 private:
266  int m_nRuns;
267 
270  const EdgeArray<TCost> *pCost,
271  int nRuns,
272  bool randomize,
273  List<edge> &delEdges
274  ) {
275  const int nBlocks = block.size();
276 
277  Array<TCost> bestSolution(nBlocks);
278  Array<List<edge>*> bestDelEdges(nBlocks);
279 
280  for(int i = 0; i < nBlocks; ++i) {
281  bestDelEdges[i] = nullptr;
282  bestSolution[i] = (block[i].first != nullptr) ? std::numeric_limits<TCost>::max() : 0;
283  }
284 
285  for(int run = 0; run < nRuns; ++run) {
286  for(int i = 0; i < nBlocks; ++i) {
287  if(bestSolution[i] > 1) {
288  const Graph &B = *block[i].first;
289  const EdgeArray<edge> &origEdge = *block[i].second;
290 
291  // compute (randomized) st-numbering
292  NodeArray<int> numbering(B, 0);
293  computeSTNumbering(B, numbering, nullptr, nullptr, randomize);
294 
295  List<edge> *pCurrentDelEdges = new List<edge>;
296  planarize(B, numbering, *pCurrentDelEdges);
297 
298  TCost currentSolution;
299  if(pCost == nullptr) {
300  currentSolution = pCurrentDelEdges->size();
301  } else {
302  currentSolution = 0;
303  for(edge e : *pCurrentDelEdges)
304  currentSolution += (*pCost)[origEdge[e]];
305  }
306 
307  if(currentSolution < bestSolution[i]) {
308  delete bestDelEdges[i];
309  bestDelEdges[i] = pCurrentDelEdges;
310  bestSolution[i] = currentSolution;
311  } else
312  delete pCurrentDelEdges;
313  }
314  }
315  }
316 
317  // build final solution from block solutions
318  for(int i = 0; i < nBlocks; ++i) {
319  if(bestDelEdges[i] != nullptr) {
320  const EdgeArray<edge> &origEdge = *block[i].second;
321  for(edge e : *bestDelEdges[i])
322  delEdges.pushBack(origEdge[e]);
323  delete bestDelEdges[i];
324  }
325  }
326  }
327 
330  const EdgeArray<TCost> *pCost,
331  int nRuns,
332  unsigned int nThreads,
333  List<edge> &delEdges
334  ) {
335  ThreadMaster master(block, pCost, nRuns-nThreads);
336 
337  Array<Worker*> worker(nThreads-1);
338  Array<Thread> thread(nThreads-1);
339  for(unsigned int i = 0; i < nThreads-1; ++i) {
340  worker[i] = new Worker(&master);
341  thread[i] = Thread(*worker[i]);
342  }
343 
344  doWorkHelper(master);
345 
346  for(unsigned int i = 0; i < nThreads-1; ++i) {
347  thread[i].join();
348  delete worker[i];
349  }
350 
351  master.buildSolution(delEdges);
352  }
353 
355 
357  static void planarize(const Graph &G, NodeArray<int> &numbering, List<edge> &delEdges) {
358  using PlanarLeafKey = booth_lueker::PlanarLeafKey<whaInfo*>;
359 
362  Array<node> table(G.numberOfNodes()+1);
363 
364  for(node v : G.nodes) {
365  for(adjEntry adj : v->adjEntries) {
366  edge e = adj->theEdge();
367  if (numbering[e->opposite(v)] > numbering[v]) { // sideeffect: ignores selfloops
368  PlanarLeafKey* L = new PlanarLeafKey(e);
369  inLeaves[v].pushFront(L);
370  }
371  }
372  table[numbering[v]] = v;
373  }
374 
375  for(node v : G.nodes) {
376  for (PlanarLeafKey *L : inLeaves[v]) {
377  outLeaves[L->userStructKey()->opposite(v)].pushFront(L);
378  }
379  }
380 
381  SList<PQLeafKey<edge,whaInfo*,bool>*> totalEliminatedKeys;
382 
384  T.Initialize(inLeaves[table[1]]);
385  for (int i = 2; i < G.numberOfNodes(); i++) {
386  SList<PQLeafKey<edge,whaInfo*,bool>*> eliminatedKeys;
387  T.Reduction(outLeaves[table[i]], eliminatedKeys);
388  totalEliminatedKeys.conc(eliminatedKeys);
389  T.ReplaceRoot(inLeaves[table[i]]);
391  }
392 
393  for (PQLeafKey<edge, whaInfo*, bool> *key : totalEliminatedKeys) {
394  edge e = key->userStructKey();
395  delEdges.pushBack(e);
396  }
397 
398  //cleanup
399  for(node v : G.nodes) {
400  while (!inLeaves[v].empty()) {
401  PlanarLeafKey *L = inLeaves[v].popFrontRet();
402  delete L;
403  }
404  }
405 
406  T.Cleanup(); // Explicit call for destructor necessary. This allows to call virtual
407  // function CleanNode for freeing node information class.
408  }
409 
410  static void doWorkHelper(ThreadMaster &master) {
411  const int nBlocks = master.numBlocks();
412 
413  do {
414  for(int i = 0; i < nBlocks; ++i) {
415  if(master.considerBlock(i)) {
416  const Graph &B = master.block(i);
417 
418  NodeArray<int> numbering(B,0); // compute (randomized) st-numbering
419  computeSTNumbering(B, numbering, nullptr, nullptr, true);
420 
421  List<edge> *pCurrentDelEdges = new List<edge>;
422  planarize(B, numbering, *pCurrentDelEdges);
423 
424  pCurrentDelEdges = master.postNewResult(i, pCurrentDelEdges);
425  delete pCurrentDelEdges;
426  }
427  }
428 
429  } while(master.getNextRun());
430  }
431 };
432 
433 }
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
The solution is optimal.
int computeSTNumbering(const Graph &G, NodeArray< int > &numbering, node s=nullptr, node t=nullptr, bool randomized=false)
Computes an st-Numbering of G.
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
Array< List< edge > * > m_bestDelEdges
best solution for block
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
Timeouter & operator=(const Timeouter &t)
Definition: Timeouter.h:63
static void doWorkHelper(ThreadMaster &master)
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
virtual void Cleanup()
Removes the entire PQ-tree.
Definition: PQTree.h:1543
unsigned int maxThreads() const
Returns the maximal number of used threads.
virtual Module::ReturnType doCall(const Graph &G, const List< edge > &preferedEdges, List< edge > &delEdges, const EdgeArray< TCost > *pCost, bool preferedImplyPlanar) override
Returns true, if G is planar, false otherwise.
Declaration of class PlanarLeafKey.
std::pair< Graph *, EdgeArray< edge > * > BlockType
ReturnType
The return type of a module.
Definition: Module.h:51
Singly linked lists (maintaining the length of the list).
Definition: SList.h:773
Declaration of st-Numbering functions.
Computation of a planar subgraph using PQ-trees.
void seqCall(const Array< BlockType > &block, const EdgeArray< TCost > *pCost, int nRuns, bool randomize, List< edge > &delEdges)
Realizes the sequential implementation.
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.
void buildSolution(List< edge > &delEdges)
PlanarSubgraphFast()
Creates an instance of the fast planar subgraph algorithm with default settings (runs = 10)...
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:282
The solution is feasible.
Declaration of Thread class representing threads.
Class for the representation of edges.
Definition: Graph_d.h:292
void parCall(const Array< BlockType > &block, const EdgeArray< TCost > *pCost, int nRuns, unsigned int nThreads, List< edge > &delEdges)
Realizes the parallel implementation.
virtual bool Reduction(SListPure< PlanarLeafKey *> &leafKeys, SList< PQLeafKey< edge, whaInfo *, bool > *> &eliminatedKeys)
Reduces a set of leaves.
void ReplaceRoot(SListPure< PlanarLeafKey *> &leafKeys)
Replaces the pertinent subtree by a set of new leaves.
Class for the representation of nodes.
Definition: Graph_d.h:169
int m_nRuns
The number of runs for randomization.
void conc(SList< E > &L2)
Appends L2 to this list and makes L2 empty.
Definition: SList.h:948
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:338
void runs(int nRuns)
Sets the number of randomized runs to nRuns.
virtual int Initialize(SListPure< PlanarLeafKey *> &leafKeys)
Initializes a new PQ-tree with a set of leaves.
int runs() const
Returns the current number of randomized runs.
The class template PQLeafKey is a derived class of class template PQBasicKey.
Definition: PQLeafKey.h:86
SListIterator< E > pushBack(const E &x)
Adds element x at the end of the list.
Definition: SList.h:872
bool isSelfLoop() const
Returns true iff the edge is a self-loop (source node = target node).
Definition: Graph_d.h:341
Threads supporting OGDF&#39;s memory management.
Definition: Thread.h:53
std::mutex m_mutex
thread synchronization
int size() const
Returns the number of elements in the list.
Definition: List.h:1312
Declaration of simple graph algorithms.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
Array< TCost > m_bestSolution
value of best solution for block
int biconnectedComponents(const Graph &G, EdgeArray< int > &component, int &nonEmptyComponents)
Computes the biconnected components of G.
The namespace for all OGDF objects.
Declaration of class PlanarSubgraphPQTree.
Declaration of interface for planar subgraph algorithms.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
const Array< BlockType > & m_block
the blocks (graph and edge mapping)
static void planarize(const Graph &G, NodeArray< int > &numbering, List< edge > &delEdges)
Performs a planarization on a biconnected component of G.
ThreadMaster(const Array< BlockType > &block, const EdgeArray< TCost > *pCost, int runs)
virtual PlanarSubgraphFast * clone() const override
Returns a new instance of fast planar subgraph with the same option settings.
Class for adjacency list elements.
Definition: Graph_d.h:79
const EdgeArray< TCost > * m_pCost
edge cost (may be 0)
List< edge > * postNewResult(int i, List< edge > *pNewDelEdges)
virtual void emptyAllPertinentNodes()
Does a clean up after a reduction.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200