Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MinSteinerTreeKou.h
Go to the documentation of this file.
1 
34 #pragma once
35 
36 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/List.h>
39 #include <ogdf/graphalg/Dijkstra.h>
42 
43 namespace ogdf {
44 template<typename T>
45 class EdgeWeightedGraph;
46 
57 template<typename T>
59 public:
61 
62  virtual ~MinSteinerTreeKou() { }
63 
64 protected:
73  virtual T computeSteinerTree(const EdgeWeightedGraph<T>& G, const List<node>& terminals,
74  const NodeArray<bool>& isTerminal, EdgeWeightedGraphCopy<T>*& finalSteinerTree) override;
75 
83  void calculateCompleteGraph(const EdgeWeightedGraph<T>& wG, const List<node>& terminals,
84  EdgeArray<List<edge>>& predecessor, EdgeWeightedGraphCopy<T>& completeTerminalGraph);
85 
94  void reinsertShortestPaths(const EdgeWeightedGraphCopy<T>& completeTerminalGraph,
95  const EdgeArray<List<edge>>& ssspPred, const NodeArray<edge>& mstPred,
96  EdgeWeightedGraphCopy<T>& finalSteinerTree, const EdgeWeightedGraph<T>& wG);
97 
104  void insertPath(const List<edge>& ssspPred, EdgeWeightedGraphCopy<T>& finalSteinerTree,
105  const EdgeWeightedGraph<T>& wG);
106 };
107 
108 template<typename T>
110  const NodeArray<bool>& isTerminal, EdgeWeightedGraphCopy<T>*& finalSteinerTree) {
111  EdgeWeightedGraphCopy<T> completeTerminalGraph;
112  completeTerminalGraph.setOriginalGraph(G);
113 
114  for (node v : terminals) {
115  completeTerminalGraph.newNode(v);
116  }
117 
118  List<edge> steinerTreeEdges;
119  EdgeArray<List<edge>> ssspPred(completeTerminalGraph);
120 
121  calculateCompleteGraph(G, terminals, ssspPred, completeTerminalGraph);
122 
123  NodeArray<edge> mstPred(completeTerminalGraph);
124  computeMinST(completeTerminalGraph, completeTerminalGraph.edgeWeights(), mstPred);
125 
126  finalSteinerTree = new EdgeWeightedGraphCopy<T>();
127  finalSteinerTree->setOriginalGraph(G);
128 
129  reinsertShortestPaths(completeTerminalGraph, ssspPred, mstPred, *finalSteinerTree, G);
130 
131  T mstWeight = makeMinimumSpanningTree(*finalSteinerTree, finalSteinerTree->edgeWeights());
132  mstWeight -= MinSteinerTreeModule<T>::pruneAllDanglingSteinerPaths(*finalSteinerTree, isTerminal);
133 
134  return mstWeight;
135 }
136 
137 template<typename T>
139  const List<node>& terminals, EdgeArray<List<edge>>& predecessor,
140  EdgeWeightedGraphCopy<T>& completeTerminalGraph) {
141  Dijkstra<T> sssp;
142  for (node u = completeTerminalGraph.firstNode(); u->succ(); u = u->succ()) {
143  NodeArray<T> d;
145  sssp.call(wG, wG.edgeWeights(), completeTerminalGraph.original(u), pi, d);
146  for (node v = u->succ(); v; v = v->succ()) {
147  edge e = completeTerminalGraph.newEdge(u, v, d[completeTerminalGraph.original(v)]);
148  predecessor[e].clear();
149  for (node t = completeTerminalGraph.original(v); pi[t]; t = pi[t]->opposite(t)) {
150  predecessor[e].pushBack(pi[t]);
151  }
152  }
153  }
154 }
155 
156 template<typename T>
158  const EdgeArray<List<edge>>& ssspPred, const NodeArray<edge>& mstPred,
159  EdgeWeightedGraphCopy<T>& finalSteinerTree, const EdgeWeightedGraph<T>& wG) {
160  for (node u : completeTerminalGraph.nodes) {
161  if (mstPred[u]) {
162  insertPath(ssspPred[mstPred[u]], finalSteinerTree, wG);
163  }
164  }
165 }
166 
167 template<typename T>
169  EdgeWeightedGraphCopy<T>& finalSteinerTree, const EdgeWeightedGraph<T>& wG) {
170  for (edge e : ssspPred) {
171  if (e != nullptr && finalSteinerTree.chain(e).size() == 0) {
172  node edgeSource = e->source();
173  node edgeTarget = e->target();
174 
175  node stSource = finalSteinerTree.copy(edgeSource);
176  if (stSource == nullptr) {
177  stSource = finalSteinerTree.newNode(edgeSource);
178  }
179 
180  node stTarget = finalSteinerTree.copy(edgeTarget);
181  if (stTarget == nullptr) {
182  stTarget = finalSteinerTree.newNode(edgeTarget);
183  }
184 
185  if (e->source() == finalSteinerTree.original(stSource)) {
186  edge newE = finalSteinerTree.newEdge(stSource, stTarget, wG.weight(e));
187  finalSteinerTree.setEdge(e, newE);
188  }
189  }
190  }
191 }
192 
193 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
Graph.h
Includes declaration of graph class.
ogdf::EdgeWeightedGraph::weight
T weight(const edge e) const
Definition: EdgeWeightedGraph.h:59
ogdf::MinSteinerTreeKou::~MinSteinerTreeKou
virtual ~MinSteinerTreeKou()
Definition: MinSteinerTreeKou.h:62
ogdf::EdgeWeightedGraphCopy::setOriginalGraph
void setOriginalGraph(const Graph *wG) override
Associates the graph copy with G, but does not create any nodes or edges.
Definition: EdgeWeightedGraphCopy.h:162
extended_graph_alg.h
Declaration of extended graph algorithms.
ogdf::MinSteinerTreeKou::reinsertShortestPaths
void reinsertShortestPaths(const EdgeWeightedGraphCopy< T > &completeTerminalGraph, const EdgeArray< List< edge >> &ssspPred, const NodeArray< edge > &mstPred, EdgeWeightedGraphCopy< T > &finalSteinerTree, const EdgeWeightedGraph< T > &wG)
Swaps an edge in the complete terminal graph with the corresponding shortest path in the original gra...
Definition: MinSteinerTreeKou.h:157
ogdf::computeMinST
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim's algorithm.
Definition: extended_graph_alg.h:117
ogdf::EdgeWeightedGraphCopy::newEdge
edge newEdge(node u, node v, T weight)
Definition: EdgeWeightedGraphCopy.h:169
EdgeWeightedGraphCopy.h
Extends the GraphCopy concept to weighted graphs.
ogdf::MinSteinerTreeModule
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
Definition: MinSteinerTreeModule.h:72
MinSteinerTreeModule.h
Declaration of ogdf::MinSteinerTreeModule.
ogdf::Dijkstra
Dijkstra's single source shortest path algorithm.
Definition: Dijkstra.h:60
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:932
ogdf::EdgeWeightedGraph
Definition: GraphIO.h:56
ogdf::MinSteinerTreeModule::pruneAllDanglingSteinerPaths
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
Definition: MinSteinerTreeModule.h:501
ogdf::GraphCopy::chain
const List< edge > & chain(edge e) const
Returns the list of edges coresponding to edge e.
Definition: GraphCopy.h:454
ogdf::EdgeWeightedGraph::edgeWeights
const EdgeArray< T > & edgeWeights() const
Definition: EdgeWeightedGraph.h:61
ogdf::Dijkstra::call
void call(const Graph &G, const EdgeArray< T > &weight, const List< node > &sources, NodeArray< edge > &predecessor, NodeArray< T > &distance, bool directed=false, bool arcsReversed=false, node target=nullptr, T maxLength=std::numeric_limits< T >::max())
Calculates, based on the graph G with corresponding edge costs and source nodes, the shortest paths a...
Definition: Dijkstra.h:253
ogdf::Graph::firstNode
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:997
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:192
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Math::pi
constexpr double pi
The constant .
Definition: Math.h:63
ogdf::GraphCopy::copy
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition: GraphCopy.h:464
ogdf::GraphCopy::setEdge
void setEdge(edge eOrig, edge eCopy)
sets eOrig to be the corresponding original edge of eCopy and vice versa
ogdf::MinSteinerTreeKou::computeSteinerTree
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: MinSteinerTreeKou.h:109
ogdf::MinSteinerTreeKou::MinSteinerTreeKou
MinSteinerTreeKou()
Definition: MinSteinerTreeKou.h:60
ogdf::EdgeWeightedGraphCopy::edgeWeights
const EdgeArray< T > & edgeWeights() const
Definition: EdgeWeightedGraphCopy.h:65
ogdf::makeMinimumSpanningTree
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal's algorithm.
Definition: extended_graph_alg.h:243
ogdf::NodeElement::succ
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:292
ogdf::EdgeWeightedGraphCopy
Definition: EdgeWeightedGraphCopy.h:44
ogdf::MinSteinerTreeKou::insertPath
void insertPath(const List< edge > &ssspPred, EdgeWeightedGraphCopy< T > &finalSteinerTree, const EdgeWeightedGraph< T > &wG)
Inserts a shortest path corresponding to an edge in the complete terminal graph.
Definition: MinSteinerTreeKou.h:168
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
Dijkstra.h
Implementation of Dijkstra's single source shortest path algorithm.
List.h
Declaration of doubly linked lists and iterators.
ogdf::List::size
int size() const
Returns the number of elements in the list.
Definition: List.h:1488
ogdf::MinSteinerTreeKou
This class implements the Minimum Steiner Tree 2-approximation algorithm by Kou et al.
Definition: MinSteinerTreeKou.h:58
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::MinSteinerTreeKou::calculateCompleteGraph
void calculateCompleteGraph(const EdgeWeightedGraph< T > &wG, const List< node > &terminals, EdgeArray< List< edge >> &predecessor, EdgeWeightedGraphCopy< T > &completeTerminalGraph)
Builds a complete terminal graph.
Definition: MinSteinerTreeKou.h:138
ogdf::GraphCopyBase::original
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:105
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716