Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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) ? 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 
170 
172  virtual PlanarSubgraphFast *clone() const override {
173  auto* res = new PlanarSubgraphFast<TCost>(*this);
174  res->m_nRuns = m_nRuns;
175  return res;
176  }
177 
180  maxThreads(fps.maxThreads());
181  this->m_timeLimit = fps.m_timeLimit;
182  m_nRuns = fps.m_nRuns;
183  return *this;
184  }
185 
186 
188  void runs (int nRuns) {
189  m_nRuns = nRuns;
190  }
191 
193  int runs() const {
194  return m_nRuns;
195  }
196 
197 
198 protected:
200 
204  const Graph &G,
205  const List<edge> &preferedEdges,
206  List<edge> &delEdges,
207  const EdgeArray<TCost> *pCost,
208  bool preferedImplyPlanar) override
209  {
210  delEdges.clear();
211 
212  if (G.numberOfEdges() < 9)
214 
215  // Determine Biconnected Components
216  EdgeArray<int> componentID(G);
217  int nBlocks = biconnectedComponents(G,componentID);
218 
219  // Determine edges per biconnected component
220  Array<SList<edge>> blockEdges(0,nBlocks-1);
221  for(edge e : G.edges) {
222  if (!e->isSelfLoop())
223  blockEdges[componentID[e]].pushFront(e);
224  }
225 
226  // Build non-trivial blocks
227  Array<BlockType> block(nBlocks);
228  NodeArray<node> copyV(G,nullptr);
229 
230  for(int i = 0; i < nBlocks; i++) {
231  if(blockEdges[i].size() < 9) {
232  block[i] = BlockType((Graph*)nullptr, (EdgeArray<edge>*)nullptr); // explicit casts required for VS2010
233  continue;
234  }
235 
236  Graph *bc = new Graph;
237  EdgeArray<edge> *origE = new EdgeArray<edge>(*bc,nullptr);
238  block[i] = BlockType(bc,origE);
239 
240  SList<node> marked;
241  for (edge e : blockEdges[i]) {
242  if (copyV[e->source()] == nullptr) {
243  copyV[e->source()] = bc->newNode();
244  marked.pushBack(e->source());
245  }
246  if (copyV[e->target()] == nullptr) {
247  copyV[e->target()] = bc->newNode();
248  marked.pushBack(e->target());
249  }
250 
251  (*origE)[bc->newEdge(copyV[e->source()],copyV[e->target()])] = e;
252  }
253 
254  for (node v : marked)
255  copyV[v] = nullptr;
256  }
257  copyV.init();
258 
259  int nRuns = max(1, m_nRuns);
260  unsigned int nThreads = min(this->maxThreads(), (unsigned int)nRuns);
261 
262  if(nThreads == 1)
263  seqCall(block, pCost, nRuns, (m_nRuns == 0), delEdges);
264  else
265  parCall(block, pCost, nRuns, nThreads, delEdges);
266 
267  // clean-up
268  for(int i = 0; i < nBlocks; i++) {
269  delete block[i].first;
270  delete block[i].second;
271  }
272 
274  }
275 
276 
277 private:
278  int m_nRuns;
279 
282  const EdgeArray<TCost> *pCost,
283  int nRuns,
284  bool randomize,
285  List<edge> &delEdges
286  ) {
287  const int nBlocks = block.size();
288 
289  Array<TCost> bestSolution(nBlocks);
290  Array<List<edge>*> bestDelEdges(nBlocks);
291 
292  for(int i = 0; i < nBlocks; ++i) {
293  bestDelEdges[i] = nullptr;
294  bestSolution[i] = (block[i].first != nullptr) ? numeric_limits<TCost>::max() : 0;
295  }
296 
297  for(int run = 0; run < nRuns; ++run) {
298  for(int i = 0; i < nBlocks; ++i) {
299  if(bestSolution[i] > 1) {
300  const Graph &B = *block[i].first;
301  const EdgeArray<edge> &origEdge = *block[i].second;
302 
303  // compute (randomized) st-numbering
304  NodeArray<int> numbering(B, 0);
305  computeSTNumbering(B, numbering, nullptr, nullptr, randomize);
306 
307  List<edge> *pCurrentDelEdges = new List<edge>;
308  planarize(B, numbering, *pCurrentDelEdges);
309 
310  TCost currentSolution;
311  if(pCost == nullptr) {
312  currentSolution = pCurrentDelEdges->size();
313  } else {
314  currentSolution = 0;
315  for(edge e : *pCurrentDelEdges)
316  currentSolution += (*pCost)[origEdge[e]];
317  }
318 
319  if(currentSolution < bestSolution[i]) {
320  delete bestDelEdges[i];
321  bestDelEdges[i] = pCurrentDelEdges;
322  bestSolution[i] = currentSolution;
323  } else
324  delete pCurrentDelEdges;
325  }
326  }
327  }
328 
329  // build final solution from block solutions
330  for(int i = 0; i < nBlocks; ++i) {
331  if(bestDelEdges[i] != nullptr) {
332  const EdgeArray<edge> &origEdge = *block[i].second;
333  for(edge e : *bestDelEdges[i])
334  delEdges.pushBack(origEdge[e]);
335  delete bestDelEdges[i];
336  }
337  }
338  }
339 
342  const EdgeArray<TCost> *pCost,
343  int nRuns,
344  unsigned int nThreads,
345  List<edge> &delEdges
346  ) {
347  ThreadMaster master(block, pCost, nRuns-nThreads);
348 
349  Array<Worker*> worker(nThreads-1);
350  Array<Thread> thread(nThreads-1);
351  for(unsigned int i = 0; i < nThreads-1; ++i) {
352  worker[i] = new Worker(&master);
353  thread[i] = Thread(*worker[i]);
354  }
355 
356  doWorkHelper(master);
357 
358  for(unsigned int i = 0; i < nThreads-1; ++i) {
359  thread[i].join();
360  delete worker[i];
361  }
362 
363  master.buildSolution(delEdges);
364  }
365 
367 
369  static void planarize(const Graph &G, NodeArray<int> &numbering, List<edge> &delEdges) {
370  using PlanarLeafKey = booth_lueker::PlanarLeafKey<whaInfo*>;
371 
374  Array<node> table(G.numberOfNodes()+1);
375 
376  for(node v : G.nodes) {
377  for(adjEntry adj : v->adjEntries) {
378  edge e = adj->theEdge();
379  if (numbering[e->opposite(v)] > numbering[v]) { // sideeffect: ignores selfloops
380  PlanarLeafKey* L = new PlanarLeafKey(e);
381  inLeaves[v].pushFront(L);
382  }
383  }
384  table[numbering[v]] = v;
385  }
386 
387  for(node v : G.nodes) {
388  for (PlanarLeafKey *L : inLeaves[v]) {
389  outLeaves[L->userStructKey()->opposite(v)].pushFront(L);
390  }
391  }
392 
393  SList<PQLeafKey<edge,whaInfo*,bool>*> totalEliminatedKeys;
394 
396  T.Initialize(inLeaves[table[1]]);
397  for (int i = 2; i < G.numberOfNodes(); i++) {
398  SList<PQLeafKey<edge,whaInfo*,bool>*> eliminatedKeys;
399  T.Reduction(outLeaves[table[i]], eliminatedKeys);
400  totalEliminatedKeys.conc(eliminatedKeys);
401  T.ReplaceRoot(inLeaves[table[i]]);
403  }
404 
405  for (PQLeafKey<edge, whaInfo*, bool> *key : totalEliminatedKeys) {
406  edge e = key->userStructKey();
407  delEdges.pushBack(e);
408  }
409 
410  //cleanup
411  for(node v : G.nodes) {
412  while (!inLeaves[v].empty()) {
413  PlanarLeafKey *L = inLeaves[v].popFrontRet();
414  delete L;
415  }
416  }
417 
418  T.Cleanup(); // Explicit call for destructor necessary. This allows to call virtual
419  // function CleanNode for freeing node information class.
420  }
421 
422  static void doWorkHelper(ThreadMaster &master) {
423  const int nBlocks = master.numBlocks();
424 
425  do {
426  for(int i = 0; i < nBlocks; ++i) {
427  if(master.considerBlock(i)) {
428  const Graph &B = master.block(i);
429 
430  NodeArray<int> numbering(B,0); // compute (randomized) st-numbering
431  computeSTNumbering(B, numbering, nullptr, nullptr, true);
432 
433  List<edge> *pCurrentDelEdges = new List<edge>;
434  planarize(B, numbering, *pCurrentDelEdges);
435 
436  pCurrentDelEdges = master.postNewResult(i, pCurrentDelEdges);
437  delete pCurrentDelEdges;
438  }
439  }
440 
441  } while(master.getNextRun());
442  }
443 };
444 
445 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
void clear()
Removes all elements from the list.
Definition: List.h:1394
The solution is optimal.
PlanarSubgraphFast & operator=(const PlanarSubgraphFast &fps)
Assignment operator. Copies option settings only.
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:95
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:1316
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
static const int Graph
Definition: GmlParser.h:59
static void doWorkHelper(ThreadMaster &master)
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
virtual void Cleanup()
Removes the entire PQ-tree.
Definition: PQTree.h:1550
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:777
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:548
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:581
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
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:134
The solution is feasible.
Declaration of Thread class representing threads.
Class for the representation of edges.
Definition: Graph_d.h:291
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:167
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:952
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:335
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:876
bool isSelfLoop() const
Definition: Graph_d.h:337
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:1255
Declaration of simple graph algorithms.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
Array< TCost > m_bestSolution
value of best solution for block
double m_timeLimit
Time limit for module calls (< 0 means no limit).
Definition: Timeouter.h:89
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:578
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
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:78
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:198