Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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>
42
43namespace ogdf {
44template<typename T>
45class EdgeWeightedGraph;
46
57template<typename T>
59public:
61
62 virtual ~MinSteinerTreeKou() { }
63
64protected:
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
108template<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
137template<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
156template<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
167template<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}
Extends the GraphCopy concept to weighted graphs.
Includes declaration of graph class.
Declaration of doubly linked lists and iterators.
Declaration of ogdf::MinSteinerTreeModule.
Dijkstra's single source shortest path algorithm.
Definition Dijkstra.h:60
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
Class for the representation of edges.
Definition Graph_d.h:364
edge newEdge(node u, node v, T weight)
void setOriginalGraph(const Graph *wG) override
Re-initializes the copy using G (which might be null), but does not create any nodes or edges.
const EdgeArray< T > & edgeWeights() const
T weight(const edge e) const
const EdgeArray< T > & edgeWeights() const
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition GraphCopy.h:191
const Graph & original() const
Returns a reference to the original graph.
Definition GraphCopy.h:104
const List< edge > & chain(edge e) const
Returns the list of edges coresponding to edge e.
Definition GraphCopy.h:453
edge copy(edge e) const override
Returns the first edge in the list of edges corresponding to edge e.
Definition GraphCopy.h:463
void setEdge(edge eOrig, edge eCopy)
sets eOrig to be the corresponding original edge of eCopy and vice versa
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition Graph_d.h:929
node firstNode() const
Returns the first node in the list of all nodes.
Definition Graph_d.h:994
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
int size() const
Returns the number of elements in the list.
Definition List.h:1488
This class implements the Minimum Steiner Tree 2-approximation algorithm by Kou et al.
void calculateCompleteGraph(const EdgeWeightedGraph< T > &wG, const List< node > &terminals, EdgeArray< List< edge > > &predecessor, EdgeWeightedGraphCopy< T > &completeTerminalGraph)
Builds a complete terminal graph.
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 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...
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.
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
Class for the representation of nodes.
Definition Graph_d.h:241
node succ() const
Returns the successor in the list of all nodes.
Definition Graph_d.h:293
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
Declaration of extended graph algorithms.
Implementation of Dijkstra's single source shortest path algorithm.
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal's algorithm.
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim's algorithm.
The namespace for all OGDF objects.