Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MinSteinerTreeZelikovsky.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/List.h>
36 #include <ogdf/graphalg/Voronoi.h>
44 
45 namespace ogdf {
46 
64 template<typename T>
66 public:
67  template<typename TYPE> using Save = steiner_tree::Save<TYPE>;
68  template<typename TYPE> using Triple = steiner_tree::Triple<TYPE>;
69 
71  enum class WinCalculation {
72  absolute,
73  relative
74  };
75 
77  enum class TripleGeneration {
78  exhaustive,
79  voronoi,
80  ondemand
81  };
82 
84  enum class TripleReduction {
85  on,
86  off
87  };
88 
90  enum class SaveCalculation {
93  staticEnum,
96  staticLCATree,
100  dynamicLCATree,
103  hybrid
104  };
105 
107  enum class Pass {
110  one,
112  multi
113  };
114 
120  : m_winCalculation(wc)
121  , m_tripleGeneration(tg)
122  , m_saveCalculation(sc)
123  , m_tripleReduction(tr)
124  , m_pass(pass)
125  , m_ssspDistances(true)
126  {
127  }
128 
130 
131  virtual T call(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal, EdgeWeightedGraphCopy<T> *&finalSteinerTree) override
132  {
133  m_triplesGenerated = 0;
134  m_tripleLookUps = 0;
136  return MinSteinerTreeModule<T>::call(G, terminals, isTerminal, finalSteinerTree);
137  }
138 
144  void forceAPSP(bool force = true)
145  {
146  m_ssspDistances = !force;
147  }
148 
151  {
152  m_winCalculation = wc;
153  }
154 
157  {
158  return m_winCalculation;
159  }
160 
163  {
164  m_tripleGeneration = tg;
165  }
166 
169  {
170  return m_tripleGeneration;
171  }
172 
175  {
176  m_tripleReduction = tr;
177  }
178 
181  {
182  return m_tripleReduction;
183  }
184 
187  {
188  m_saveCalculation = sv;
189  }
190 
193  {
194  return m_saveCalculation;
195  }
196 
198  void pass(Pass p) {
199  m_pass = p;
200  }
201 
203  Pass pass() const
204  {
205  return m_pass;
206  }
207 
210  {
211  return m_triplesGenerated;
212  }
213 
216  {
217  return m_triplesContracted;
218  }
219 
222  {
223  return m_tripleLookUps;
224  }
225 
226 protected:
235  virtual T computeSteinerTree(
236  const EdgeWeightedGraph<T> &G,
237  const List<node> &terminals,
238  const NodeArray<bool> &isTerminal,
239  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override;
240 
244  void computeDistanceMatrix();
245 
250  for (node u : m_originalGraph->nodes) {
251  if (pred[u] == nullptr) {
252  dist[u] = numeric_limits<T>::max();
253  }
254  }
255  }
256 
266  inline void updateBestCenter(node x, node &center, T &minCost, const NodeArray<T> &dist1, const NodeArray<T> &dist2, const NodeArray<T> &dist3) const
267  {
268  const T tmp = dist1[x] + dist2[x] + dist3[x];
269  if (minCost > tmp
270  && dist1[x] != numeric_limits<T>::max()
271  && dist2[x] != numeric_limits<T>::max()
272  && dist3[x] != numeric_limits<T>::max()) {
273  center = x;
274  minCost = tmp;
275  }
276  }
277 
281  inline void generateTriple(node u, node v, node w, node center, const T &minCost, const Save<T> &save)
282  {
283  if (center // center found
284  && !(*m_isTerminal)[center]) { // cheapest center is not a terminal
285  const double gain = save.gain(u, v, w);
286  const double win = calcWin(gain, minCost);
288  || win > 0) {
290  OGDF_ASSERT(center);
291  Triple<T> triple(u, v, w, center, minCost, win);
292  m_triples.pushBack(triple);
293  }
294  }
295  }
296 
301  void generateVoronoiTriples(const Save<T> &save);
302 
307  void generateExhaustiveTriples(const Save<T> &save);
308 
313  void generateTriples(const Save<T> &save)
314  {
317  } else
320  }
321  }
322 
329  void contractTriple(const Triple<T> &triple, Save<T> &save, NodeArray<bool> &isNewTerminal)
330  {
332  save.update(triple);
333  isNewTerminal[triple.z()] = true;
334  }
335 
341  void tripleOnDemand(Save<T> &save, NodeArray<bool> &isNewTerminal);
342 
348  void multiPass(Save<T> &save, NodeArray<bool> &isNewTerminal);
349 
355  void onePass(Save<T> &save, NodeArray<bool> &isNewTerminal);
356 
360  double calcWin(double gain, T cost) const
361  {
362  switch (winCalculation()) {
364  return gain / cost - 1.0;
366  default:
367  return gain - cost;
368  }
369  }
370 
372  {
373  // generate complete graph
374  for (node v : *m_terminals) {
375  steinerTree.newNode(v);
376  }
377  for (node u : steinerTree.nodes) {
378  const NodeArray<T> &dist = m_distance[steinerTree.original(u)];
379  for (node v = u->succ(); v; v = v->succ()) {
380  steinerTree.newEdge(u, v, dist[steinerTree.original(v)]);
381  }
382  }
383  // compute MST
384  makeMinimumSpanningTree(steinerTree, steinerTree.edgeWeights());
385  }
386 
387 private:
394 
400 
404 
405 };
406 
407 template<typename T>
409 {
410  OGDF_ASSERT(tripleGeneration() != TripleGeneration::ondemand // combinations that only work with ondemand:
413  && pass() != Pass::one));
414 
415  m_originalGraph = &G;
416  m_terminals = &terminals;
417  m_isTerminal = &isTerminal;
418 
419  // build the complete terminal graph and a distance matrix
420  NodeArray<bool> isNewTerminal(G, false);
421  for (node v : *m_terminals) {
422  isNewTerminal[v] = true;
423  }
424 
425  if (terminals.size() >= 3) {
426  m_distance.init(G); // the distance matrix
428 
429  // init terminal-spanning tree and its save-edge data structure
430  EdgeWeightedGraphCopy<T> steinerTree; // the terminal-spanning tree to be modified
431  steinerTree.createEmpty(G);
433 
434  Save<T> *save = nullptr;
435  switch (saveCalculation()) {
437  save = new steiner_tree::SaveEnum<T>(steinerTree);
438  break;
440  save = new steiner_tree::SaveStatic<T>(steinerTree);
441  break;
444  save = new steiner_tree::SaveDynamic<T>(steinerTree);
445  break;
446  }
447  OGDF_ASSERT(save);
448 
449  if (tripleGeneration() == TripleGeneration::ondemand) { // ondemand triple generation
450  tripleOnDemand(*save, isNewTerminal);
451  } else { // exhaustive or voronoi
452  // triple generation phase
454  steiner_tree::SaveEnum<T> saveTriple(steinerTree);
455  generateTriples(saveTriple);
456  } else {
457  generateTriples(*save);
458  }
459  // contraction phase
460  if (pass() == Pass::multi) {
461  multiPass(*save, isNewTerminal);
462  } else { // pass() == one
463  onePass(*save, isNewTerminal);
464  }
465  }
466  delete save;
467 
468  // cleanup
469  m_triples.clear();
470  }
471 
472  // obtain final Steiner Tree using (MST-based) Steiner tree approximation algorithm
473  return steiner_tree::obtainFinalSteinerTree(G, isNewTerminal, isTerminal, finalSteinerTree);
474 }
475 
476 template<typename T>
478 {
479  if (m_ssspDistances) {
480  NodeArray<edge> pred;
481  for (node u : *m_terminals) {
484  }
485  } else {
488  for (node u : *m_terminals) {
490  }
491  }
492 }
493 
494 template<typename T>
496 {
497  Voronoi<T> voronoi(*m_originalGraph, m_originalGraph->edgeWeights(), *m_terminals);
498  for (ListConstIterator<node> it_u = m_terminals->begin(); it_u.valid(); ++it_u) {
499  const node u = *it_u;
500  const NodeArray<T> &uDistance = m_distance[u];
501  for (ListConstIterator<node> it_v = it_u.succ(); it_v.valid(); ++it_v) {
502  const node v = *it_v;
503  const NodeArray<T> &vDistance = m_distance[v];
504  for (ListConstIterator<node> it_w = it_v.succ(); it_w.valid(); ++it_w) {
505  const node w = *it_w;
506  const NodeArray<T> &wDistance = m_distance[w];
507 
508  node center = nullptr;
509  T minCost = numeric_limits<T>::max();
510  // look in all Voronoi regions for the best center node
511  for (node x : voronoi.nodesInRegion(u)) {
512  updateBestCenter(x, center, minCost, uDistance, vDistance, wDistance);
513  }
514  for (node x : voronoi.nodesInRegion(v)) {
515  updateBestCenter(x, center, minCost, uDistance, vDistance, wDistance);
516  }
517  for (node x : voronoi.nodesInRegion(w)) {
518  updateBestCenter(x, center, minCost, uDistance, vDistance, wDistance);
519  }
520  generateTriple(u, v, w, center, minCost, save);
521  }
522  }
523  }
524 }
525 
526 
527 template<typename T>
529 {
530  for (ListConstIterator<node> it_u = m_terminals->begin(); it_u.valid(); ++it_u) {
531  const node u = *it_u;
532  const NodeArray<T> &uDistance = m_distance[u];
533  for (ListConstIterator<node> it_v = it_u.succ(); it_v.valid(); ++it_v) {
534  const node v = *it_v;
535  const NodeArray<T> &vDistance = m_distance[v];
536  for (ListConstIterator<node> it_w = it_v.succ(); it_w.valid(); ++it_w) {
537  const node w = *it_w;
538  const NodeArray<T> &wDistance = m_distance[w];
539 
540  node center = nullptr;
541  T minCost = numeric_limits<T>::max();
542  for (node x : m_originalGraph->nodes) {
543  updateBestCenter(x, center, minCost, uDistance, vDistance, wDistance);
544  }
545  generateTriple(u, v, w, center, minCost, save);
546  }
547  }
548  }
549 }
550 
551 template<typename T>
553 {
554  Triple<T> maxTriple;
555  do {
556  maxTriple.win(0);
557  for (node v : m_originalGraph->nodes) {
558  if (!(*m_isTerminal)[v]) { // for each non-terminal v
559  // find s0, nearest terminal to v
560  T best = numeric_limits<T>::max();
561  node s0 = nullptr;
562  for (node s : *m_terminals) {
563  T tmp = m_distance[s][v];
564  if (best > tmp) {
565  best = tmp;
566  s0 = s;
567  }
568  }
569  OGDF_ASSERT(s0);
570 
571  // find s1 maximizing save(s0, s1) - d(v, s1)
572  node s1 = nullptr;
573  T save1Val(0);
574  for (node s : *m_terminals) {
575  if (s == s0
576  || m_distance[s][v] == numeric_limits<T>::max()) {
577  continue;
578  }
579  T tmpVal = save.saveWeight(s, s0);
580  T tmp = tmpVal - m_distance[s][v];
581  if (!s1 || best < tmp) {
582  best = tmp;
583  s1 = s;
584  save1Val = tmpVal;
585  }
586  }
587  if (!s1) { // it may happen that s1 does not exist
588  continue;
589  }
590 
591  node s2 = nullptr;
592  T save2Val(0);
593  const edge save1 = save.saveEdge(s0, s1);
594  for (node s : *m_terminals) {
595  if (s == s0
596  || s == s1
597  || m_distance[s][v] == numeric_limits<T>::max()) {
598  continue;
599  }
600  const edge tmp = save.saveEdge(s0, s);
601  if (tmp == save1) {
602  save2Val = save.saveWeight(s1, s);
603  } else {
604  save2Val = save.saveWeight(s0, s);
605  }
606  T tmpWin = save1Val + save2Val - m_distance[s0][v] - m_distance[s1][v] - m_distance[s][v];
607  if (!s2 || best < tmpWin) {
608  best = tmpWin;
609  s2 = s;
610  }
611  }
612  if (!s2) { // it may happen that s2 does not exist
613  continue;
614  }
615 
616  if (s2 // it may happen that s2 does not exist
617  && best > maxTriple.win()) { // best win is better than previous best; also positive
619  maxTriple.s0(s0);
620  maxTriple.s1(s1);
621  maxTriple.s2(s2);
622  maxTriple.z(v);
623  maxTriple.win(best);
624  //maxTriple.cost(save1Val + save2Val - win); not needed
625  }
626  }
627  }
628 
629  if (maxTriple.win() > 0) {
630  contractTriple(maxTriple, save, isNewTerminal);
631  }
632  } while (maxTriple.win() > 0);
633 }
634 
635 
636 template<typename T>
638 {
639  class TripleComparer {
640  public:
641  static double compare(const Triple<T> &x1, const Triple<T> &x2) {
642  return x2.win() - x1.win();
643  }
645  } tc;
646  m_triples.quicksort(tc);
647 
648  for (const Triple<T> &t : m_triples) {
649  ++m_tripleLookUps;
650  if (calcWin(double(save.gain(t.s0(), t.s1(), t.s2())), t.cost()) > 0) {
651  contractTriple(t, save, isNewTerminal);
652  }
653  }
654 }
655 
656 
657 template<typename T>
659 {
660  double win = 0;
661  ListIterator<Triple<T>> maxTriple;
662 
663  do {
664  win = 0;
665  ListIterator<Triple<T>> nextIt;
666  for (ListIterator<Triple<T>> it = m_triples.begin(); it.valid(); it = nextIt) {
667  nextIt = it.succ();
668  ++m_tripleLookUps;
669  Triple<T> &t = *it;
670  t.win(calcWin(double(save.gain(t.s0(), t.s1(), t.s2())), t.cost()));
671  if (t.win() > win) {
672  win = t.win();
673  maxTriple = it;
674  } else {
676  && t.win() <= 0) {
677  m_triples.del(it);
678  }
679  }
680  }
681 
682  if (win > 0) {
683  contractTriple(*maxTriple, save, isNewTerminal);
684  m_triples.del(maxTriple);
685  }
686  } while (win > 0);
687 }
688 
689 
690 }
691 // end namespace ogdf
void generateTriple(node u, node v, node w, node center, const T &minCost, const Save< T > &save)
Add a found triple to the triples list.
generate triples "on the fly", only usable with WinCalculation::absolute
const NodeArray< bool > * m_isTerminal
Incidence vector for terminal nodes.
Pass
Enables a heuristic version (for TG exhaustive and voronoi only)
This class behaves basically the same as SaveDynamic except that the update of the weighted graph is ...
Definition: SaveStatic.h:48
WinCalculation winCalculation() const
Returns type of gain calculation currently in use.
void tripleOnDemand(Save< T > &save, NodeArray< bool > &isNewTerminal)
Contraction phase for algorithm generating triples on demand.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:413
double win() const
Definition: Triple.h:80
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
Builds a minimum Steiner tree given a weighted graph and a list of terminals.
This class serves as an interface for various methods to compute minimum or approximations of minimum...
Dynamically updatable weighted Tree for determining save edges via LCA computation.
Definition: SaveDynamic.h:50
edge newEdge(node u, node v, T weight)
#define OGDF_AUGMENT_STATICCOMPARER(type)
Add this macro to your class to turn it into a full static comparer.
Definition: comparer.h:201
const List< node > * m_terminals
List of terminal nodes.
Interface of Minimum Steiner Tree Algorithms.
virtual T gain(node u, node v, node w) const =0
Returns the gain (sum of the save edges) of a node triple.
heuristic: evaluate all triples, sort them descending by gain, traverse sorted triples once...
Extends the GraphCopy concept to weighted graphs.
void pass(Pass p)
Sets type of pass.
TripleReduction
Switches immediate triple dropping.
long numberOfGeneratedTriples() const
Returns the number of generated triples.
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal&#39;s algorithm.
void saveCalculation(SaveCalculation sv)
Sets type of save calculation.
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:216
MinSteinerTreeZelikovsky(WinCalculation wc=WinCalculation::absolute, TripleGeneration tg=TripleGeneration::voronoi, SaveCalculation sc=SaveCalculation::hybrid, TripleReduction tr=TripleReduction::on, Pass pass=Pass::multi)
virtual void update(const Triple< T > &t)=0
Updates the weighted tree data structure given a contracted triple.
This class implements the 11/6-approximation algorithm by Zelikovsky for the minimum Steiner tree pro...
WinCalculation
Choice of objective function.
Same as staticLCATree but each time a triple has been contracted the "weight tree" is updated dynamic...
ListIteratorBase< E, isConst > succ() const
Returns successor iterator.
Definition: List.h:133
void multiPass(Save< T > &save, NodeArray< bool > &isNewTerminal)
Contraction phase for the original version of the algorithm.
TripleReduction tripleReduction() const
Returns type of triple reduction currently in use.
A weighted tree as auxiliary data structure for contraction based algorithms.
Implementation of the staticLCATree option for calculating save edges in Zelikovsky&#39;s 11/6-approximat...
long m_tripleLookUps
Number of triple lookups.
void createEmpty(const Graph &wG)
TripleGeneration
Choice of triple generation.
bool m_ssspDistances
True iff we only compute SSSP from terminals instead of APSP for full component construction.
Computes Voronoi regions in an edge weighted graph.
Definition: Voronoi.h:45
TripleReduction m_tripleReduction
Chosen option for triple reduction.
const EdgeArray< T > & edgeWeights() const
WinCalculation m_winCalculation
Chosen option for gain calculation.
long numberOfContractedTriples() const
Returns the number of contracted triples.
void generateVoronoiTriples(const Save< T > &save)
Generates triples according to voronoi regions.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree) override
Builds a minimum Steiner tree given a weighted graph and a list of terminals.
void computeDistanceMatrix()
Computes the distance matrix for the original graph.
Class for the representation of edges.
Definition: Graph_d.h:291
const EdgeWeightedGraph< T > * m_originalGraph
The original edge-weighted graph.
Pass pass() const
Returns type of pass currently in use.
double calcWin(double gain, T cost) const
Calculate the win.
Pass m_pass
Chosen option for pass.
Class for the representation of nodes.
Definition: Graph_d.h:167
Encapsulates a pointer to a list element.
Definition: List.h:41
void tripleReduction(TripleReduction tr)
Sets type of triple reduction.
Definition of a Triple used in contraction-based approximation algorithm for the minimum Steiner tree...
T obtainFinalSteinerTree(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
This class computes save edges recursively and stores for every node pair their save edge in a HashAr...
Definition: SaveEnum.h:48
SaveCalculation saveCalculation() const
Returns type of save calculation currently in use.
Uses staticEnum for the triple generation phase (many queries) and dynamicLCATree during the contract...
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:287
void tripleGeneration(TripleGeneration tg)
Sets type of triple generation.
void generateInitialTerminalSpanningTree(EdgeWeightedGraphCopy< T > &steinerTree)
void markUnreachablesInDistanceMatrix(NodeArray< T > &dist, const NodeArray< edge > &pred)
Mark unreachable nodes in distance matrix by setting their distance to infinity (numeric_limits<T>::m...
virtual edge saveEdge(node u, node v) const =0
Returns the save edge between two nodes.
TripleGeneration tripleGeneration() const
Returns type of triple generation currently in use.
void forceAPSP(bool force=true)
For the 3-restricted case, it is sufficient to compute an SSSP from every terminal instead of doing a...
int size() const
Returns the number of elements in the list.
Definition: List.h:1255
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree) override
Builds a minimum Steiner tree given a weighted graph and a list of terminals.
void contractTriple(const Triple< T > &triple, Save< T > &save, NodeArray< bool > &isNewTerminal)
Contracts a triple and updates the save data structure.
Algorithms used by at least two functions of Steiner tree code or its internal helpers.
long m_triplesGenerated
Number of generated triples.
long m_triplesContracted
Number of contracted triples.
The namespace for all OGDF objects.
removes triples as soon as their gain is known to be non positive
Implementation of Voronoi regions in an EdgeWeightedGraph.
List< Triple< T > > m_triples
The list of triples during the algorithm.
void generateExhaustiveTriples(const Save< T > &save)
Generates all possible triples.
static void allPairShortestPathsStrict(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T > > &distance, NodeArray< NodeArray< edge > > &pred)
void onePass(Save< T > &save, NodeArray< bool > &isNewTerminal)
Contraction phase for the one pass heuristic.
NodeArray< NodeArray< T > > m_distance
The distance matrix.
Implementation of the staticTree option for calculating save edges in Zelikovsky&#39;s 11/6-approximation...
TripleGeneration m_tripleGeneration
Chosen option for triple generation.
void generateTriples(const Save< T > &save)
Generates triples according to the chosen option.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
long numberOfTripleLookUps() const
Returns the number of triple lookups during execution time.
This class serves as an interface for different approaches concerning the calculation of save edges...
Definition: Save.h:45
static void singleSourceShortestPathsStrict(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &isTerminal, NodeArray< T > &distance, NodeArray< edge > &pred)
Builds a "weight tree" (save edges are inner nodes, terminals are leaves and searches save edges via ...
void updateBestCenter(node x, node &center, T &minCost, const NodeArray< T > &dist1, const NodeArray< T > &dist2, const NodeArray< T > &dist3) const
Update center node if it is the best so far.
This class represents a triple used by various contraction-based minimum Steiner tree approximations...
Definition: Triple.h:44
virtual T saveWeight(node u, node v) const =0
Returns the weight of the save edge between two nodes.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
SaveCalculation m_saveCalculation
Chosen option for save calculation.
void winCalculation(WinCalculation wc)
Sets type of gain calculation.
Declaration of doubly linked lists and iterators.
SaveCalculation
Different methods for obtaining save edges.
Stores explicitly the save edge for every pair of terminals. Needs O(n^2) space but has fast query ti...