Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MinSteinerTreeZelikovsky.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/List.h>
45 
46 namespace ogdf {
47 
65 template<typename T>
67 public:
68  template<typename TYPE> using Save = steiner_tree::Save<TYPE>;
69  template<typename TYPE> using Triple = steiner_tree::Triple<TYPE>;
70 
72  enum class WinCalculation {
73  absolute,
74  relative
75  };
76 
78  enum class TripleGeneration {
79  exhaustive,
80  voronoi,
81  ondemand
82  };
83 
85  enum class TripleReduction {
86  on,
87  off
88  };
89 
91  enum class SaveCalculation {
94  staticEnum,
97  staticLCATree,
101  dynamicLCATree,
104  hybrid
105  };
106 
108  enum class Pass {
111  one,
113  multi
114  };
115 
121  : m_winCalculation(wc)
122  , m_tripleGeneration(tg)
123  , m_saveCalculation(sc)
124  , m_tripleReduction(tr)
125  , m_pass(pass)
126  , m_ssspDistances(true)
127  {
128  }
129 
131 
132  virtual T call(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal, EdgeWeightedGraphCopy<T> *&finalSteinerTree) override
133  {
134  m_triplesGenerated = 0;
135  m_tripleLookUps = 0;
137  return MinSteinerTreeModule<T>::call(G, terminals, isTerminal, finalSteinerTree);
138  }
139 
145  void forceAPSP(bool force = true)
146  {
147  m_ssspDistances = !force;
148  }
149 
152  {
153  m_winCalculation = wc;
154  }
155 
158  {
159  return m_winCalculation;
160  }
161 
164  {
165  m_tripleGeneration = tg;
166  }
167 
170  {
171  return m_tripleGeneration;
172  }
173 
176  {
177  m_tripleReduction = tr;
178  }
179 
182  {
183  return m_tripleReduction;
184  }
185 
188  {
189  m_saveCalculation = sv;
190  }
191 
194  {
195  return m_saveCalculation;
196  }
197 
199  void pass(Pass p) {
200  m_pass = p;
201  }
202 
204  Pass pass() const
205  {
206  return m_pass;
207  }
208 
211  {
212  return m_triplesGenerated;
213  }
214 
217  {
218  return m_triplesContracted;
219  }
220 
223  {
224  return m_tripleLookUps;
225  }
226 
227 protected:
236  virtual T computeSteinerTree(
237  const EdgeWeightedGraph<T> &G,
238  const List<node> &terminals,
239  const NodeArray<bool> &isTerminal,
240  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override;
241 
245  void computeDistanceMatrix();
246 
250  inline void generateTriple(node u, node v, node w, node center, const T &minCost, const Save<T> &save)
251  {
252  const double gain = save.gain(u, v, w);
253  const double win = calcWin(gain, minCost);
255  || win > 0) {
257  OGDF_ASSERT(center);
258  Triple<T> triple(u, v, w, center, minCost, win);
259  m_triples.pushBack(triple);
260  }
261  }
262 
269  {
271  [this, &save](node u, node v, node w, node center, T minCost) {
272  generateTriple(u, v, w, center, minCost, save);
273  });
274  }
275 
280  inline void generateTriples(const Save<T> &save)
281  {
285  generateTriples(save, fcg);
286  } else {
289  generateTriples(save, fcg);
290  }
291  }
292 
299  void contractTriple(const Triple<T> &triple, Save<T> &save, NodeArray<bool> &isNewTerminal)
300  {
302  save.update(triple);
303  isNewTerminal[triple.z()] = true;
304  }
305 
311  void tripleOnDemand(Save<T> &save, NodeArray<bool> &isNewTerminal);
312 
320  bool findBestTripleForCenter(node center, const Save<T> &save, Triple<T> &maxTriple) const
321  {
322  bool updated = false; // return value
323 
324  // find s0, nearest terminal to center
325  T best = std::numeric_limits<T>::max();
326  node s0 = nullptr;
327  for (node s : *m_terminals) {
328  T tmp = m_distance[s][center];
329  if (best > tmp) {
330  best = tmp;
331  s0 = s;
332  }
333  }
334  OGDF_ASSERT(s0);
335  OGDF_ASSERT(m_pred[s0][center]);
336 
337  // find s1 maximizing save(s0, s1) - d(center, s1)
338  node s1 = nullptr;
339  T save1Val(0);
340  for (node s : *m_terminals) {
341  if (s != s0
342  && m_pred[s][center] != nullptr) {
343  OGDF_ASSERT(m_distance[s][center] != std::numeric_limits<T>::max());
344  T tmpVal = save.saveWeight(s, s0);
345  T tmp = tmpVal - m_distance[s][center];
346  if (!s1 || best < tmp) {
347  best = tmp;
348  s1 = s;
349  save1Val = tmpVal;
350  }
351  }
352  }
353  if (s1) {
354  OGDF_ASSERT(m_pred[s1][center]);
355  node s2 = nullptr;
356  T save2Val(0);
357  const edge save1 = save.saveEdge(s0, s1);
358  for (node s : *m_terminals) {
359  if (s != s0
360  && s != s1
361  && m_pred[s][center] != nullptr) {
362  OGDF_ASSERT(m_distance[s][center] != std::numeric_limits<T>::max());
363  const edge tmp = save.saveEdge(s0, s);
364  save2Val = save.saveWeight(tmp == save1 ? s1 : s0, s);
365  T tmpWin = save1Val + save2Val - m_distance[s0][center] - m_distance[s1][center] - m_distance[s][center];
366  if (!s2 || best < tmpWin) {
367  best = tmpWin;
368  s2 = s;
369  }
370  }
371  }
372 
373  if (s2 // it may happen that s2 does not exist
374  && best > maxTriple.win()) { // best win is better than previous best; also positive
375  OGDF_ASSERT(m_pred[s2][center]);
376  maxTriple.s0(s0);
377  maxTriple.s1(s1);
378  maxTriple.s2(s2);
379  maxTriple.z(center);
380  maxTriple.win(best);
381  //maxTriple.cost(save1Val + save2Val - win); not needed
382  updated = true;
383  }
384  }
385  return updated;
386  }
387 
393  void multiPass(Save<T> &save, NodeArray<bool> &isNewTerminal);
394 
400  void onePass(Save<T> &save, NodeArray<bool> &isNewTerminal);
401 
405  double calcWin(double gain, T cost) const
406  {
407  switch (winCalculation()) {
409  return gain / cost - 1.0;
411  default:
412  return gain - cost;
413  }
414  }
415 
417  {
418  // generate complete graph
419  for (node v : *m_terminals) {
420  steinerTree.newNode(v);
421  }
422  for (node u : steinerTree.nodes) {
423  const NodeArray<T> &dist = m_distance[steinerTree.original(u)];
424  for (node v = u->succ(); v; v = v->succ()) {
425  steinerTree.newEdge(u, v, dist[steinerTree.original(v)]);
426  }
427  }
428  // compute MST
429  makeMinimumSpanningTree(steinerTree, steinerTree.edgeWeights());
430  }
431 
432 private:
439 
446 
450 
451 };
452 
453 template<typename T>
455 {
456  OGDF_ASSERT(tripleGeneration() != TripleGeneration::ondemand // combinations that only work with ondemand:
459  && pass() != Pass::one));
460 
461  m_originalGraph = &G;
462  m_terminals = &terminals;
463  m_isTerminal = &isTerminal;
464 
465  // build the complete terminal graph and a distance matrix
466  NodeArray<bool> isNewTerminal(G, false);
467  for (node v : *m_terminals) {
468  isNewTerminal[v] = true;
469  }
470 
471  if (terminals.size() >= 3) {
473 
474  // init terminal-spanning tree and its save-edge data structure
475  EdgeWeightedGraphCopy<T> steinerTree; // the terminal-spanning tree to be modified
476  steinerTree.createEmpty(G);
478 
479  Save<T> *save = nullptr;
480  switch (saveCalculation()) {
482  save = new steiner_tree::SaveEnum<T>(steinerTree);
483  break;
485  save = new steiner_tree::SaveStatic<T>(steinerTree);
486  break;
489  save = new steiner_tree::SaveDynamic<T>(steinerTree);
490  break;
491  }
492  OGDF_ASSERT(save);
493 
494  if (tripleGeneration() == TripleGeneration::ondemand) { // ondemand triple generation
495  tripleOnDemand(*save, isNewTerminal);
496  } else { // exhaustive or voronoi
497  // triple generation phase
499  steiner_tree::SaveEnum<T> saveTriple(steinerTree);
500  generateTriples(saveTriple);
501  } else {
502  generateTriples(*save);
503  }
504  // contraction phase
505  if (pass() == Pass::multi) {
506  multiPass(*save, isNewTerminal);
507  } else { // pass() == one
508  onePass(*save, isNewTerminal);
509  }
510  }
511  delete save;
512 
513  // cleanup
514  m_triples.clear();
515  }
516 
517  // obtain final Steiner Tree using (MST-based) Steiner tree approximation algorithm
518  return steiner_tree::obtainFinalSteinerTree(G, isNewTerminal, isTerminal, finalSteinerTree);
519 }
520 
521 template<typename T>
523 {
524  if (m_ssspDistances) {
526  } else {
528  }
529 }
530 
531 template<typename T>
533 {
534  Triple<T> maxTriple;
535  ArrayBuffer<node> nonterminals;
537  do {
538  maxTriple.win(0);
539  for (node center : nonterminals) {
540  if (findBestTripleForCenter(center, save, maxTriple)) {
542  }
543  }
544 
545  if (maxTriple.win() > 0) {
546  contractTriple(maxTriple, save, isNewTerminal);
547  }
548  } while (maxTriple.win() > 0);
549 }
550 
551 
552 template<typename T>
554 {
555  m_triples.quicksort(GenericComparer<Triple<T>, double>([](const Triple<T>& x) -> double { return -x.win(); }));
556 
557  for (const Triple<T> &t : m_triples) {
558  ++m_tripleLookUps;
559  if (calcWin(double(save.gain(t.s0(), t.s1(), t.s2())), t.cost()) > 0) {
560  contractTriple(t, save, isNewTerminal);
561  }
562  }
563 }
564 
565 
566 template<typename T>
568 {
569  double win = 0;
570  ListIterator<Triple<T>> maxTriple;
571 
572  do {
573  win = 0;
574  ListIterator<Triple<T>> nextIt;
575  for (ListIterator<Triple<T>> it = m_triples.begin(); it.valid(); it = nextIt) {
576  nextIt = it.succ();
577  ++m_tripleLookUps;
578  Triple<T> &t = *it;
579  t.win(calcWin(double(save.gain(t.s0(), t.s1(), t.s2())), t.cost()));
580  if (t.win() > win) {
581  win = t.win();
582  maxTriple = it;
583  } else {
585  && t.win() <= 0) {
586  m_triples.del(it);
587  }
588  }
589  }
590 
591  if (win > 0) {
592  contractTriple(*maxTriple, save, isNewTerminal);
593  m_triples.del(maxTriple);
594  }
595  } while (win > 0);
596 }
597 
598 }
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
Interface for full 3-component generation including auxiliary functions.
const NodeArray< bool > * m_isTerminal
Incidence vector for terminal nodes.
Pass
Enables a heuristic version (for TG exhaustive and voronoi only)
ListIteratorBase< E, isConst, isReverse > succ() const
Returns successor iterator.
Definition: List.h:145
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.
static void allPairShortestPaths(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
The default all-pair-shortest-paths algorithm.
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:412
Full 3-component generation using Voronoi regions.
double win() const
Definition: Triple.h:76
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
Calls the Steiner tree algorithm for nontrivial cases but handles trivial cases directly.
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
Dynamically updatable weighted Tree for determining save edges via LCA computation.
Definition: SaveDynamic.h:50
edge newEdge(node u, node v, T weight)
const List< node > * m_terminals
List of terminal nodes.
Declaration of ogdf::MinSteinerTreeModule.
virtual T gain(node u, node v, node w) const =0
Returns the gain (sum of the save edges) of a node triple.
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
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:218
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...
Definition of ogdf::steiner_tree::Full3ComponentGeneratorEnumeration class template.
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.
NodeArray< NodeArray< edge > > m_pred
The predecessor matrix.
Implementation of the staticLCATree option for calculating save edges in Zelikovsky&#39;s 11/6-approximat...
virtual void call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const NodeArray< NodeArray< T >> &distance, const NodeArray< NodeArray< edge >> &pred, std::function< void(node, node, node, node, T)> generateFunction) const =0
Generate full components and call generateFunction for each full component.
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.
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.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
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.
Definition of ogdf::steiner_tree::Full3ComponentGeneratorVoronoi class template.
void generateTriples(const Save< T > &save, const steiner_tree::Full3ComponentGeneratorModule< T > &fcg)
Generates triples using the given full 3-component generator.
void computeDistanceMatrix()
Computes the distance matrix for the original graph.
Class for the representation of edges.
Definition: Graph_d.h:292
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:169
Encapsulates a pointer to a list element.
Definition: List.h:42
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:286
void tripleGeneration(TripleGeneration tg)
Sets type of triple generation.
static void getNonterminals(ArrayBuffer< node > &nonterminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as ArrayBuffer<node>) of all nonterminals.
void generateInitialTerminalSpanningTree(EdgeWeightedGraphCopy< T > &steinerTree)
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:1312
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree) override
Calls the Steiner tree algorithm for nontrivial cases but handles trivial cases directly.
void contractTriple(const Triple< T > &triple, Save< T > &save, NodeArray< bool > &isNewTerminal)
Contracts a triple and updates the save data structure.
static void allTerminalShortestPaths(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred, std::function< void(const EdgeWeightedGraph< T > &, node, const NodeArray< bool > &, NodeArray< T > &, NodeArray< edge > &)> ssspFunc=singleSourceShortestPaths)
Runs a given (or the default) single-source-shortest-paths function from all terminals.
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
List< Triple< T > > m_triples
The list of triples during the algorithm.
bool findBestTripleForCenter(node center, const Save< T > &save, Triple< T > &maxTriple) const
Find the best triple for a given nonterminal center.
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.
Compare elements based on a single comparable attribute.
Definition: comparer.h:377
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
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
Builds a "weight tree" (save edges are inner nodes, terminals are leaves and searches save edges via ...
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...