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/List.h>
37 #include <ogdf/graphalg/Dijkstra.h>
40 
41 namespace ogdf {
42 
53 template<typename T>
55 public:
57 
58  virtual ~MinSteinerTreeKou() { }
59 
60 protected:
69  virtual T computeSteinerTree(const EdgeWeightedGraph<T>& G, const List<node>& terminals,
70  const NodeArray<bool>& isTerminal, EdgeWeightedGraphCopy<T>*& finalSteinerTree) override;
71 
79  void calculateCompleteGraph(const EdgeWeightedGraph<T>& wG, const List<node>& terminals,
80  EdgeArray<List<edge>>& predecessor, EdgeWeightedGraphCopy<T>& completeTerminalGraph);
81 
90  void reinsertShortestPaths(const EdgeWeightedGraphCopy<T>& completeTerminalGraph,
91  const EdgeArray<List<edge>>& ssspPred, const NodeArray<edge>& mstPred,
92  EdgeWeightedGraphCopy<T>& finalSteinerTree, const EdgeWeightedGraph<T>& wG);
93 
100  void insertPath(const List<edge>& ssspPred, EdgeWeightedGraphCopy<T>& finalSteinerTree,
101  const EdgeWeightedGraph<T>& wG);
102 };
103 
104 template<typename T>
106  const NodeArray<bool>& isTerminal, EdgeWeightedGraphCopy<T>*& finalSteinerTree) {
107  EdgeWeightedGraphCopy<T> completeTerminalGraph;
108  completeTerminalGraph.setOriginalGraph(G);
109 
110  for (node v : terminals) {
111  completeTerminalGraph.newNode(v);
112  }
113 
114  List<edge> steinerTreeEdges;
115  EdgeArray<List<edge>> ssspPred(completeTerminalGraph);
116 
117  calculateCompleteGraph(G, terminals, ssspPred, completeTerminalGraph);
118 
119  NodeArray<edge> mstPred(completeTerminalGraph);
120  computeMinST(completeTerminalGraph, completeTerminalGraph.edgeWeights(), mstPred);
121 
122  finalSteinerTree = new EdgeWeightedGraphCopy<T>();
123  finalSteinerTree->setOriginalGraph(G);
124 
125  reinsertShortestPaths(completeTerminalGraph, ssspPred, mstPred, *finalSteinerTree, G);
126 
127  T mstWeight = makeMinimumSpanningTree(*finalSteinerTree, finalSteinerTree->edgeWeights());
128  mstWeight -= MinSteinerTreeModule<T>::pruneAllDanglingSteinerPaths(*finalSteinerTree, isTerminal);
129 
130  return mstWeight;
131 }
132 
133 template<typename T>
135  const List<node>& terminals, EdgeArray<List<edge>>& predecessor,
136  EdgeWeightedGraphCopy<T>& completeTerminalGraph) {
137  Dijkstra<T> sssp;
138  for (node u = completeTerminalGraph.firstNode(); u->succ(); u = u->succ()) {
139  NodeArray<T> d;
141  sssp.call(wG, wG.edgeWeights(), completeTerminalGraph.original(u), pi, d);
142  for (node v = u->succ(); v; v = v->succ()) {
143  edge e = completeTerminalGraph.newEdge(u, v, d[completeTerminalGraph.original(v)]);
144  predecessor[e].clear();
145  for (node t = completeTerminalGraph.original(v); pi[t]; t = pi[t]->opposite(t)) {
146  predecessor[e].pushBack(pi[t]);
147  }
148  }
149  }
150 }
151 
152 template<typename T>
154  const EdgeArray<List<edge>>& ssspPred, const NodeArray<edge>& mstPred,
155  EdgeWeightedGraphCopy<T>& finalSteinerTree, const EdgeWeightedGraph<T>& wG) {
156  for (node u : completeTerminalGraph.nodes) {
157  if (mstPred[u]) {
158  insertPath(ssspPred[mstPred[u]], finalSteinerTree, wG);
159  }
160  }
161 }
162 
163 template<typename T>
165  EdgeWeightedGraphCopy<T>& finalSteinerTree, const EdgeWeightedGraph<T>& wG) {
166  for (edge e : ssspPred) {
167  if (e != nullptr && finalSteinerTree.chain(e).size() == 0) {
168  node edgeSource = e->source();
169  node edgeTarget = e->target();
170 
171  node stSource = finalSteinerTree.copy(edgeSource);
172  if (stSource == nullptr) {
173  stSource = finalSteinerTree.newNode(edgeSource);
174  }
175 
176  node stTarget = finalSteinerTree.copy(edgeTarget);
177  if (stTarget == nullptr) {
178  stTarget = finalSteinerTree.newNode(edgeTarget);
179  }
180 
181  if (e->source() == finalSteinerTree.original(stSource)) {
182  edge newE = finalSteinerTree.newEdge(stSource, stTarget, wG.weight(e));
183  finalSteinerTree.setEdge(e, newE);
184  }
185  }
186  }
187 }
188 
189 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::EdgeWeightedGraph::weight
T weight(const edge e) const
Definition: EdgeWeightedGraph.h:58
ogdf::MinSteinerTreeKou::~MinSteinerTreeKou
virtual ~MinSteinerTreeKou()
Definition: MinSteinerTreeKou.h:58
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:158
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:153
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:107
ogdf::EdgeWeightedGraphCopy::newEdge
edge newEdge(node u, node v, T weight)
Definition: EdgeWeightedGraphCopy.h:165
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:59
MinSteinerTreeModule.h
Declaration of ogdf::MinSteinerTreeModule.
ogdf::Dijkstra
Dijkstra's single source shortest path algorithm.
Definition: Dijkstra.h:53
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:924
ogdf::EdgeWeightedGraph
Definition: EdgeWeightedGraph.h:39
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:488
ogdf::GraphCopy::chain
const List< edge > & chain(edge e) const
Returns the list of edges coresponding to edge e.
Definition: GraphCopy.h:447
ogdf::EdgeWeightedGraph::edgeWeights
const EdgeArray< T > & edgeWeights() const
Definition: EdgeWeightedGraph.h:60
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:246
ogdf::Graph::firstNode
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:989
ogdf::GraphCopyBase::newNode
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:185
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: List.h:42
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:651
ogdf::Math::pi
constexpr double pi
The constant .
Definition: Math.h:59
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:457
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:105
ogdf::MinSteinerTreeKou::MinSteinerTreeKou
MinSteinerTreeKou()
Definition: MinSteinerTreeKou.h:56
ogdf::EdgeWeightedGraphCopy::edgeWeights
const EdgeArray< T > & edgeWeights() const
Definition: EdgeWeightedGraphCopy.h:61
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:233
ogdf::NodeElement::succ
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:285
ogdf::EdgeWeightedGraphCopy
Definition: EdgeWeightedGraphCopy.h:40
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:164
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:356
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:1478
ogdf::MinSteinerTreeKou
This class implements the Minimum Steiner Tree 2-approximation algorithm by Kou et al.
Definition: MinSteinerTreeKou.h:54
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:233
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:134
ogdf::GraphCopyBase::original
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:98
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:709