Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MinSteinerTreeModule.h
Go to the documentation of this file.
1 
32 #pragma once
33 
39 #include <ogdf/graphalg/Dijkstra.h>
44 #include <sstream>
45 
46 namespace ogdf {
47 
57 template<typename T>
59 public:
61  virtual ~MinSteinerTreeModule() { }
62 
73  virtual T call(const EdgeWeightedGraph<T> &G,
74  const List<node> &terminals,
75  const NodeArray<bool> &isTerminal,
76  EdgeWeightedGraphCopy<T> *&finalSteinerTree
77  );
78 
81 
89  static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy<T> &steinerTree, const NodeArray<bool> &isTerminal);
90 
99  static T pruneDanglingSteinerPathFrom(EdgeWeightedGraphCopy<T> &steinerTree, const NodeArray<bool> &isTerminal, node start);
100 
106  static T pruneDanglingSteinerPathsFrom(EdgeWeightedGraphCopy<T> &steinerTree, const NodeArray<bool> &isTerminal, const List<node> &start);
107 
113  static T removeCyclesFrom(EdgeWeightedGraphCopy<T> &steinerTree, const NodeArray<bool> &isTerminal);
114 
118 
134  static void singleSourceShortestPathsPreferringTerminals(const EdgeWeightedGraph<T> &G, node source, const NodeArray<bool> &isTerminal, NodeArray<T> &distance, NodeArray<edge> &pred);
135 
138  Dijkstra<T> sssp;
139  sssp.call(G, G.edgeWeights(), source, pred, distance);
140  }
141 
144  static inline void singleSourceShortestPaths(const EdgeWeightedGraph<T>& G, node source,
145  const NodeArray<bool>& isTerminal, NodeArray<T>& distance, NodeArray<edge>& pred) {
146 #ifdef OGDF_MINSTEINERTREEMODULE_SHORTEST_PATHS_STANDARD
147  singleSourceShortestPathsStandard(G, source, isTerminal, distance, pred);
148 #else
149  singleSourceShortestPathsPreferringTerminals(G, source, isTerminal, distance, pred);
150 #endif
151  }
152 
155  const EdgeWeightedGraph<T> &G,
156  const List<node> &terminals,
157  const NodeArray<bool> &isTerminal,
158  NodeArray<NodeArray<T>> &distance,
159  NodeArray<NodeArray<edge>> &pred)
160  {
161  allTerminalShortestPaths(G, terminals, isTerminal, distance, pred, singleSourceShortestPathsStandard);
162  }
163 
166  const EdgeWeightedGraph<T> &G,
167  const List<node> &terminals,
168  const NodeArray<bool> &isTerminal,
169  NodeArray<NodeArray<T>> &distance,
170  NodeArray<NodeArray<edge>> &pred)
171  {
172  allTerminalShortestPaths(G, terminals, isTerminal, distance, pred, singleSourceShortestPathsPreferringTerminals);
173  }
174 
177  const EdgeWeightedGraph<T> &G,
178  const List<node> &terminals,
179  const NodeArray<bool> &isTerminal,
180  NodeArray<NodeArray<T>> &distance,
181  NodeArray<NodeArray<edge>> &pred,
182  std::function<void(const EdgeWeightedGraph<T>&, node, const NodeArray<bool>&, NodeArray<T>&, NodeArray<edge>&)> ssspFunc = singleSourceShortestPaths) {
183  allNodesByListShortestPaths(G, terminals, isTerminal, terminals, distance, pred, ssspFunc);
184  }
185 
188  const EdgeWeightedGraph<T> &G,
189  const List<node> &terminals,
190  const NodeArray<bool> &isTerminal,
191  NodeArray<NodeArray<T>> &distance,
192  NodeArray<NodeArray<edge>> &pred) {
193  allNodeShortestPaths(G, terminals, isTerminal, distance, pred, singleSourceShortestPathsStandard);
194  }
195 
198  const EdgeWeightedGraph<T> &G,
199  const List<node> &terminals,
200  const NodeArray<bool> &isTerminal,
201  NodeArray<NodeArray<T>> &distance,
202  NodeArray<NodeArray<edge>> &pred) {
203  allNodeShortestPaths(G, terminals, isTerminal, distance, pred, singleSourceShortestPathsPreferringTerminals);
204  }
205 
207  static void allNodeShortestPaths(
208  const EdgeWeightedGraph<T> &G,
209  const List<node> &terminals,
210  const NodeArray<bool> &isTerminal,
211  NodeArray<NodeArray<T>> &distance,
212  NodeArray<NodeArray<edge>> &pred,
213  std::function<void(const EdgeWeightedGraph<T>&, node, const NodeArray<bool>&, NodeArray<T>&, NodeArray<edge>&)> ssspFunc = singleSourceShortestPaths) {
214  allNodesByListShortestPaths(G, terminals, isTerminal, G.nodes, distance, pred, ssspFunc);
215  }
216 
227 
230 
233  static void allPairShortestPaths(const EdgeWeightedGraph<T>& G, const NodeArray<bool>& isTerminal, NodeArray<NodeArray<T>>& distance, NodeArray<NodeArray<edge>>& pred) {
234 #ifdef OGDF_MINSTEINERTREEMODULE_SHORTEST_PATHS_STANDARD
235  allPairShortestPathsStandard(G, isTerminal, distance, pred);
236 #else
237  allPairShortestPathsPreferringTerminals(G, isTerminal, distance, pred);
238 #endif
239  }
240 
244 
253  static void drawSVG(const EdgeWeightedGraph<T> &G, const NodeArray<bool> &isTerminal, const EdgeWeightedGraphCopy<T> &steinerTree, const char *filename);
254 
262  static void drawSVG(const EdgeWeightedGraph<T> &G, const NodeArray<bool> &isTerminal, const char *filename) {
263  EdgeWeightedGraphCopy<T> emptySteinerTree;
264  emptySteinerTree.createEmpty(G);
265  drawSVG(G, isTerminal, emptySteinerTree, filename);
266  }
267 
275  static void drawSteinerTreeSVG(const EdgeWeightedGraphCopy<T> &steinerTree, const NodeArray<bool> &isTerminal, const char *filename);
276 
278 
288  static bool isSteinerTree(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal, const EdgeWeightedGraphCopy<T> &steinerTree);
289 
297  static bool isQuasiBipartite(const EdgeWeightedGraph<T> &G, const NodeArray<bool> &isTerminal);
298 
306  static inline void getTerminals(List<node> &terminals, const EdgeWeightedGraph<T> &G, const NodeArray<bool> &isTerminal)
307  {
308  for (node v : G.nodes) {
309  if (isTerminal[v]) {
310  terminals.pushBack(v);
311  }
312  }
313  }
314 
316  static inline void sortTerminals(List<node> &terminals)
317  {
318  terminals.quicksort(GenericComparer<node, int>([](node v) { return v->index(); }));
319  }
320 
328  static inline void getNonterminals(ArrayBuffer<node> &nonterminals, const EdgeWeightedGraph<T> &G, const NodeArray<bool> &isTerminal)
329  {
330  for (node v : G.nodes) {
331  if (!isTerminal[v]) {
332  nonterminals.push(v);
333  }
334  }
335  }
336 
337 protected:
343  virtual T computeSteinerTree(const EdgeWeightedGraph<T> &G,
344  const List<node> &terminals,
345  const NodeArray<bool> &isTerminal,
346  EdgeWeightedGraphCopy<T> *&finalSteinerTree
347  ) = 0;
348 
349 private:
351  static void apspInit(const EdgeWeightedGraph<T> &G, NodeArray<NodeArray<T>> &distance, NodeArray<NodeArray<edge>> &pred);
353  static void ssspInit(const EdgeWeightedGraph<T> &G, node source, PrioritizedMapQueue<node, T> &queue, NodeArray<T> &distance, NodeArray<edge> &pred);
354 
356  inline static void apspInnerLoop(node v, const EdgeWeightedGraph<T> &G, NodeArray<NodeArray<T>> &distance, std::function<void(node, node, T)> func)
357  {
358  for (node u : G.nodes) {
359  const T duv = distance[u][v];
360  if (duv < std::numeric_limits<T>::max()) {
361  for (node w = u->succ(); w != nullptr; w = w->succ()) {
362  if (distance[v][w] < std::numeric_limits<T>::max()) {
363  func(u, w, duv + distance[v][w]);
364  }
365  }
366  }
367  }
368  }
369 
371  template<typename NODELIST>
372  inline static void allNodesByListShortestPaths(
373  const EdgeWeightedGraph<T> &G,
374  const List<node> &terminals,
375  const NodeArray<bool> &isTerminal,
376  const NODELIST& nodes,
377  NodeArray<NodeArray<T>> &distance,
378  NodeArray<NodeArray<edge>> &pred,
379  std::function<void(const EdgeWeightedGraph<T>&, node, const NodeArray<bool>&, NodeArray<T>&, NodeArray<edge>&)> ssspFunc) {
380  distance.init(G);
381  pred.init(G);
382  for (node u : nodes) {
383  ssspFunc(G, u, isTerminal, distance[u], pred[u]);
384  }
385  }
386 };
387 
388 template<typename T>
390  const List<node> &terminals,
391  const NodeArray<bool> &isTerminal,
392  EdgeWeightedGraphCopy<T> *&finalSteinerTree
393  )
394 {
396 
397  if (terminals.size() > 2) {
398  return this->computeSteinerTree(G, terminals, isTerminal, finalSteinerTree);
399  }
400 
401  finalSteinerTree = new EdgeWeightedGraphCopy<T>();
402  finalSteinerTree->createEmpty(G);
403  if (!terminals.empty()) {
404  finalSteinerTree->newNode(terminals.back());
405  }
406  if (terminals.size() <= 1) {
407  return 0;
408  }
409 
410  OGDF_ASSERT(terminals.size() == 2);
411  T cost(0);
412  AStarSearch<T> astar;
413  NodeArray<edge> pred;
414  NodeArray<T> dist;
415  astar.call(G, G.edgeWeights(), terminals.front(), terminals.back(), pred);
416  OGDF_ASSERT(pred[terminals.back()] != nullptr); // connected
417  for (node t = terminals.back(); t != terminals.front(); t = pred[t]->opposite(t)) {
418  const edge e = pred[t];
419  finalSteinerTree->newNode(e->opposite(t));
420  finalSteinerTree->newEdge(e, G.weight(e));
421  cost += G.weight(e);
422  }
423  return cost;
424 }
425 
426 template<typename T>
428  const EdgeWeightedGraph<T> &G,
429  const List<node> &terminals,
430  const NodeArray<bool> &isTerminal,
431  const EdgeWeightedGraphCopy<T> &steinerTree)
432 {
433  // the Steiner tree is actually a tree
434  if (!isTree(steinerTree)) {
435  return false;
436  }
437 
438  // all terminal nodes are in the graph and have degree >= 1
439  for(node v : terminals) {
440  const node u = steinerTree.copy(v);
441  if (!u || (terminals.size() > 1 && u->degree() < 1)) {
442  return false;
443  }
444  }
445 
446  // all Steiner nodes are inner nodes
447  for(node u : steinerTree.nodes) {
448  if (!isTerminal[steinerTree.original(u)]
449  && u->degree() <= 1) {
450  return false;
451  }
452  }
453 
454  return true;
455 }
456 
457 template<typename T>
459 {
460  for (node v = G.firstNode(); v; v = v->succ()) {
461  if (!isTerminal[v]) {
462  for (adjEntry adj = v->firstAdj(); adj; adj = adj->succ()) {
463  if (!isTerminal[adj->twinNode()]) {
464  return false;
465  }
466  }
467  }
468  }
469  return true;
470 }
471 
472 template<typename T>
474 {
475  OGDF_ASSERT(isConnected(steinerTree));
476  T delWeights(0);
477  node u = start;
478  while (u->degree() == 1
479  && !isTerminal[steinerTree.original(u)]) {
480  const adjEntry adj = u->firstAdj();
481  const node v = adj->twinNode();
482  delWeights += steinerTree.weight(adj->theEdge());
483  steinerTree.delNode(u);
484  u = v;
485  }
486  return delWeights;
487 }
488 
489 template<typename T>
491 {
492  T delWeights(0);
493  for (node v : start) {
494  delWeights += pruneDanglingSteinerPathFrom(steinerTree, isTerminal, v);
495  }
496  return delWeights;
497 }
498 
499 template<typename T>
501 {
502  List<node> start;
503  for (node u : steinerTree.nodes) {
504  if (u->degree() == 1
505  && !isTerminal[steinerTree.original(u)]) {
506  start.pushBack(u);
507  }
508  }
509 
510  return pruneDanglingSteinerPathsFrom(steinerTree, isTerminal, start);
511 }
512 
513 template<typename T>
515 {
516  if (steinerTree.numberOfEdges() > steinerTree.numberOfNodes() - 1) {
517  EdgeArray<bool> isInTree(steinerTree);
518  T oldCost(0);
519  T newCost(computeMinST(steinerTree, steinerTree.edgeWeights(), isInTree));
520 
521  List<node> pendant; // collect resulting pendant edges
522  for (edge nextEdge, e = steinerTree.firstEdge(); e; e = nextEdge) {
523  oldCost += steinerTree.weight(e);
524  nextEdge = e->succ();
525  if (!isInTree[e]) {
526  if (e->source()->degree() == 2) {
527  pendant.pushBack(e->source());
528  }
529  if (e->target()->degree() == 2) {
530  pendant.pushBack(e->target());
531  }
532  steinerTree.delEdge(e);
533  }
534  }
535  newCost -= MinSteinerTreeModule<T>::pruneDanglingSteinerPathsFrom(steinerTree, isTerminal, pendant);
536  return oldCost - newCost;
537  }
538  return 0;
539 }
540 
541 template<typename T>
543 {
544  distance.init(G, std::numeric_limits<T>::max());
545  distance[source] = 0;
546 
547  for (node v : G.nodes) {
548  queue.push(v, distance[v]);
549  }
550 
551  pred.init(G, nullptr);
552 }
553 
554 template<typename T>
556  const NodeArray<bool> &isTerminal, NodeArray<T> &distance, NodeArray<edge> &pred)
557 {
559  ssspInit(G, source, queue, distance, pred);
560 
561  // we must handle the source explicitly because it is a terminal
562  node v = queue.topElement();
563  queue.pop();
564  OGDF_ASSERT(v == source);
565  for (adjEntry adj : v->adjEntries) {
566  edge e = adj->theEdge();
567  node w = adj->twinNode();
568  if (distance[w] > G.weight(e)) { // this check is only necessary for multigraphs, otherwise this is always true
569  queue.decrease(w, (distance[w] = G.weight(e)));
570  pred[w] = e;
571  }
572  }
573 
574  auto setPredecessor = [&](node w, edge e) {
575  bool wOnPathWithTerminal = isTerminal[v] || pred[v] == nullptr;
576  pred[w] = wOnPathWithTerminal ? nullptr : e;
577  };
578  while (!queue.empty()) {
579  v = queue.topElement();
580  queue.pop();
581 
582  if (distance[v] == std::numeric_limits<T>::max()) { // min is unreachable, finished
583  break;
584  }
585  for (adjEntry adj : v->adjEntries) {
586  edge e = adj->theEdge();
587  node w = adj->twinNode();
588  T dist = distance[v] + G.weight(e);
589  if (distance[w] > dist) {
590  queue.decrease(w, (distance[w] = dist));
591  setPredecessor(w, e);
592  } else
593  if (distance[w] == dist
594  && pred[w] != nullptr) { // tie
595  setPredecessor(w, e);
596  }
597  }
598  }
599 }
600 
601 template<typename T>
603  const NodeArray<bool>& isTerminal, NodeArray<NodeArray<T>>& distance, NodeArray<NodeArray<edge>>& pred) {
604  apspInit(G, distance, pred);
605 
606  for (node v : G.nodes) {
607  if (isTerminal[v]) { // v is a terminal
608  apspInnerLoop(v, G, distance, [&distance, &pred](node u, node w, T duvw) {
609  if (duvw <= distance[u][w]) { // prefer terminals
610  distance[w][u] = distance[u][w] = duvw;
611  pred[w][u] = pred[u][w] = nullptr;
612  }
613  });
614  } else { // v is not a terminal
615  apspInnerLoop(v, G, distance, [&v, &distance, &pred](node u, node w, T duvw) {
616  if (duvw < distance[u][w]) { // do not prefer nonterminals
617  distance[w][u] = distance[u][w] = duvw;
618  pred[u][w] = (pred[u][v] ? pred[v][w] : nullptr);
619  pred[w][u] = (pred[w][v] ? pred[v][u] : nullptr);
620  }
621  });
622  }
623  }
624  for (node u : G.nodes) {
625  distance[u][u] = 0;
626  }
627 }
628 
629 template<typename T>
631 {
632  distance.init(G);
633  pred.init(G);
634  for (node u : G.nodes) {
635  distance[u].init(G, std::numeric_limits<T>::max());
636  pred[u].init(G, nullptr);
637  }
638  for (edge e : G.edges) {
639  const node u = e->source(), v = e->target();
640  distance[u][v] = distance[v][u] = G.weight(e);
641  pred[u][v] = pred[v][u] = e;
642  }
643 }
644 
645 template<typename T>
647 {
648  apspInit(G, distance, pred);
649 
650  for (node v : G.nodes) {
651  apspInnerLoop(v, G, distance, [&v, &distance, &pred](node u, node w, T duvw) {
652  if (duvw < distance[u][w]) {
653  distance[w][u] = distance[u][w] = duvw;
654  pred[u][w] = pred[v][w];
655  pred[w][u] = pred[v][u];
656  }
657  });
658  }
659  for (node u : G.nodes) {
660  distance[u][u] = 0;
661  }
662 }
663 
664 template<typename T>
665 void MinSteinerTreeModule<T>::drawSteinerTreeSVG(const EdgeWeightedGraphCopy<T> &steinerTree, const NodeArray<bool> &isTerminal, const char *filename)
666 {
667  GraphAttributes GA(steinerTree,
674 
675  GA.directed() = false;
676 
677  string s;
678 
679  for (node v : steinerTree.nodes) {
680  std::stringstream out;
681  GA.width(v) = GA.height(v) = 25.0;
682  if (isTerminal[steinerTree.original(v)]) {
683  out << "T";
684  GA.shape(v) = Shape::Rect;
685  GA.fillColor(v) = Color::Name::Red;
686  } else {
687  out << "S";
688  GA.shape(v) = Shape::Ellipse;
690  }
691  out << steinerTree.original(v);
692  GA.label(v) = out.str();
693  }
694 
695  FMMMLayout fmmm;
696 
697  fmmm.useHighLevelOptions(true);
698  fmmm.unitEdgeLength(44.0);
699  fmmm.newInitialPlacement(true);
701 
702  fmmm.call(GA);
703  std::ofstream writeStream(filename, std::ofstream::out);
704  GraphIO::drawSVG(GA, writeStream);
705 }
706 
707 template<typename T>
708 void MinSteinerTreeModule<T>::drawSVG(const EdgeWeightedGraph<T> &G, const NodeArray<bool> &isTerminal, const EdgeWeightedGraphCopy<T> &steinerTree, const char *filename)
709 {
710  GraphAttributes GA(G,
717 
718  GA.directed() = false;
719 
720  for (edge e : G.edges) {
722  GA.label(e) = to_string(G.weight(e));
723  GA.strokeWidth(e) = 1;
724  }
725  for (edge e : steinerTree.edges) {
726  GA.strokeColor(steinerTree.original(e)) = Color::Name::Red;
727  GA.strokeWidth(steinerTree.original(e)) = 2;
728  }
729 
730  for (node v : G.nodes) {
731  std::stringstream out;
732  GA.width(v) = GA.height(v) = 25.0;
734  if (isTerminal[v]) {
735  out << "T" << v;
736  GA.shape(v) = Shape::Rect;
737  GA.fillColor(v) = Color::Name::Red;
738  GA.strokeWidth(v) = 2;
739  } else {
740  out << "S" << v;
741  GA.shape(v) = Shape::Ellipse;
742  if (steinerTree.copy(v)) {
744  GA.strokeWidth(v) = 2;
745  } else {
747  GA.strokeWidth(v) = 1;
748  }
749  }
750  GA.label(v) = out.str();
751  }
752 
753  FMMMLayout fmmm;
754 
755  fmmm.useHighLevelOptions(true);
756  fmmm.unitEdgeLength(44.0);
757  fmmm.newInitialPlacement(true);
759 
760  fmmm.call(GA);
761 
762  std::ofstream writeStream(filename, std::ofstream::out);
763  GraphIO::drawSVG(GA, writeStream);
764 }
765 
766 }
static void apspInit(const EdgeWeightedGraph< T > &G, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Common initialization routine for APSP algorithms.
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
virtual ~MinSteinerTreeModule()
Do nothing on destruction.
static void allNodesByListShortestPaths(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const NODELIST &nodes, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred, std::function< void(const EdgeWeightedGraph< T > &, node, const NodeArray< bool > &, NodeArray< T > &, NodeArray< edge > &)> ssspFunc)
Runs a given (or the default) single-source-shortest-paths function from all nodes.
bool newInitialPlacement() const
Returns the current setting of option newInitialPlacement.
Definition: FMMMLayout.h:337
double width(node v) const
Returns the width of the bounding box of node v.
Declaration of class GraphAttributes which extends a Graph by additional attributes.
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:335
static void allTerminalShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Runs singleSourceShortestPathsPreferringTerminals from all terminals.
Dijkstra&#39;s single source shortest path algorithm.
Definition: Dijkstra.h:52
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.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:412
FMMMOptions::QualityVsSpeed qualityVersusSpeed() const
Returns the current setting of option qualityVersusSpeed.
Definition: FMMMLayout.h:343
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
static bool drawSVG(const GraphAttributes &A, std::ostream &os, const SVGSettings &settings)
const Color & fillColor(node v) const
Returns the fill color of node v.
double unitEdgeLength() const
Returns the current setting of option unitEdgeLength.
Definition: FMMMLayout.h:326
static const long edgeLabel
Corresponds to edge attribute label(edge).
static bool isSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const EdgeWeightedGraphCopy< T > &steinerTree)
Checks in O(n) time if a given tree is acually a Steiner Tree.
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim&#39;s algorithm.
T call(const Graph &graph, const EdgeArray< T > &cost, const node source, const node target, NodeArray< edge > &predecessor, std::function< T(node)> heuristic=[](node) { return T(0);})
Computes the shortests path between source and target.
Definition: AStarSearch.h:99
static void apspInnerLoop(node v, const EdgeWeightedGraph< T > &G, NodeArray< NodeArray< T >> &distance, std::function< void(node, node, T)> func)
The inner loop for APSP algorithm to avoid code duplication.
static void allNodeShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Runs singleSourceShortestPathsPreferringTerminals from all nodes.
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.
const Color & strokeColor(node v) const
Returns the stroke color of node v.
static void sortTerminals(List< node > &terminals)
Sort terminals by index.
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
int degree() const
Returns the degree of the node (indegree + outdegree).
Definition: Graph_d.h:210
void call(const Graph &G, const EdgeArray< T > &weight, const List< node > &sources, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false)
Calculates, based on the graph G with corresponding edge costs and source nodes, the shortest paths a...
Definition: Dijkstra.h:61
edge newEdge(node u, node v, T weight)
const EdgeArray< T > & edgeWeights() const
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:618
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
static void allPairShortestPathsStandard(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Standard all-pair-shortest-paths algorithm (Floyd-Warshall)
static void allNodeShortestPaths(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 nodes.
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
Extends the GraphCopy concept to weighted graphs.
static void singleSourceShortestPaths(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &isTerminal, NodeArray< T > &distance, NodeArray< edge > &pred)
The default single-source-shortest-paths algorithm.
bool directed() const
Returns if the graph is directed.
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:218
static const long nodeStyle
Corresponds to node attributes strokeColor(node), strokeType(node), strokeWidth(node), fillPattern(node), fillColor(node), and fillBgColor(node).
Prioritized queue interface wrapper for heaps.
static bool isQuasiBipartite(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Checks in O(n + m) time if a given Steiner tree problem instance is quasi-bipartite.
static void drawSVG(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const char *filename)
Writes an SVG file of the instance graph.
T weight(const edge e) const
double height(node v) const
Returns the height of the bounding box of node v.
static void drawSVG(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, const EdgeWeightedGraphCopy< T > &steinerTree, const char *filename)
Writes an SVG file of a minimum Steiner tree in the original graph.
virtual void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e&#39;s original edge.
The fast multipole multilevel layout algorithm.
Definition: FMMMLayout.h:232
static void ssspInit(const EdgeWeightedGraph< T > &G, node source, PrioritizedMapQueue< node, T > &queue, NodeArray< T > &distance, NodeArray< edge > &pred)
Common initialization routine for SSSP algorithms.
void createEmpty(const Graph &wG)
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
static void allPairShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Modified all-pair-shortest-paths algorithm (Floyd-Warshall) with heuristic to prefer paths over termi...
const E & topElement() const
Returns the topmost element in the queue.
Implementation of Dijkstra&#39;s single source shortest path algorithm.
void quicksort()
Sorts the list using Quicksort.
Definition: List.h:1152
const string & label(node v) const
Returns the label of node v.
static const long nodeLabel
Corresponds to node attribute label(node).
edge firstEdge() const
Returns the first edge in the list of all edges.
Definition: Graph_d.h:623
static T pruneDanglingSteinerPathsFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, const List< node > &start)
Prunes dangling Steiner paths beginning at given nonterminal leaves only.
const_reference front() const
Returns a const reference to the first element.
Definition: List.h:274
const EdgeArray< T > & edgeWeights() const
Stores additional attributes of a graph (like layout information).
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
static void singleSourceShortestPathsPreferringTerminals(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &isTerminal, NodeArray< T > &distance, NodeArray< edge > &pred)
Modified single-source-shortest-paths (Dijkstra) with heuristic to prefer paths over terminals...
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class for the representation of edges.
Definition: Graph_d.h:292
Class for the representation of nodes.
Definition: Graph_d.h:169
Priority queue interface wrapping various heaps.
Declaration of Fast Multipole Multilevel Method (FM^3).
bool useHighLevelOptions() const
Returns the current setting of option useHighLevelOptions.
Definition: FMMMLayout.h:311
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:338
Definition of a Triple used in contraction-based approximation algorithm for the minimum Steiner tree...
static const long edgeGraphics
Corresponds to edge attribute bends(edge).
static void allNodeShortestPathsStandard(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Runs singleSourceShortestPathsStandard from all nodes.
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:286
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
static void getNonterminals(ArrayBuffer< node > &nonterminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as ArrayBuffer<node>) of all nonterminals.
static void allTerminalShortestPathsStandard(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, NodeArray< NodeArray< T >> &distance, NodeArray< NodeArray< edge >> &pred)
Runs singleSourceShortestPathsStandard from all terminals.
int index() const
Returns the (unique) node index.
Definition: Graph_d.h:203
edge succ() const
Returns the successor in the list of all edges.
Definition: Graph_d.h:353
bool empty() const
Checks whether the queue is empty.
A-Star informed search algorithm.
Definition: AStarSearch.h:52
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:148
int size() const
Returns the number of elements in the list.
Definition: List.h:1312
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)=0
Computes the actual Steiner tree.
virtual void call(GraphAttributes &GA) override
Calls the algorithm for graph GA and returns the layout information in GA.
static T removeCyclesFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Remove remaining cycles from a Steiner "almost" tree.
Declaration of simple graph algorithms.
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
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.
The namespace for all OGDF objects.
Declaration of extended graph algorithms.
float strokeWidth(node v) const
Returns the stroke width of node v.
static void singleSourceShortestPathsStandard(const EdgeWeightedGraph< T > &G, node source, const NodeArray< bool > &, NodeArray< T > &distance, NodeArray< edge > &pred)
Standard single-source-shortest-paths algoritm (Dijkstra)
static void drawSteinerTreeSVG(const EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, const char *filename)
Writes a SVG that shows only the given Steiner tree.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
virtual void delNode(node v) override
Removes node v and all its adjacent edges cleaning-up their corresponding lists of original edges...
Declares class GraphIO which provides access to all graph read and write functionality.
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
static T pruneDanglingSteinerPathFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal, node start)
Prunes the dangling Steiner path beginning at a given nonterminal leaf only.
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:213
bool isConnected(const Graph &G)
Returns true iff G is connected.
Implementation of the A* informed search algorithm.
bool empty() const
Returns true iff the list is empty.
Definition: List.h:256
Class for adjacency list elements.
Definition: Graph_d.h:79
Shape shape(node v) const
Returns the shape type of node v.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
static const long nodeGraphics
Corresponds to node attributes x(node), y(node), width(node), height(node), and shape(node).
static void getTerminals(List< node > &terminals, const EdgeWeightedGraph< T > &G, const NodeArray< bool > &isTerminal)
Generates a list (as List<node>) of all terminals.
const_reference back() const
Returns a const reference to the last element.
Definition: List.h:292
static const long edgeStyle
Corresponds to edge attributes strokeColor(edge), strokeType(edge), and strokeWidth(edge).
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200