Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MinSteinerTreeTakahashi.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/List.h>
39 
40 namespace ogdf {
41 
56 template<typename T>
58 public:
60 
62 
70  virtual T call(const EdgeWeightedGraph<T> &G,
71  const List<node> &terminals,
72  const NodeArray<bool> &isTerminal,
73  EdgeWeightedGraphCopy<T> *&finalSteinerTree,
74  const node startNode)
75  {
76  return call(G, terminals, isTerminal, isTerminal, finalSteinerTree, startNode);
77  }
78 
86  virtual T call(const EdgeWeightedGraph<T> &G,
87  const List<node> &terminals,
88  const NodeArray<bool> &isTerminal,
89  const NodeArray<bool> &isOriginalTerminal,
90  EdgeWeightedGraphCopy<T> *&finalSteinerTree)
91  {
92  return call(G, terminals, isTerminal, isOriginalTerminal, finalSteinerTree, terminals.front());
93  }
94 
96 
105  virtual T call(const EdgeWeightedGraph<T> &G,
106  const List<node> &terminals,
107  const NodeArray<bool> &isTerminal,
108  const NodeArray<bool> &isOriginalTerminal,
109  EdgeWeightedGraphCopy<T> *&finalSteinerTree,
110  const node startNode);
111 
112 protected:
114  const EdgeWeightedGraph<T> &G,
115  const List<node> &terminals,
116  const NodeArray<bool> &isTerminal,
117  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override
118  {
119  return call(G, terminals, isTerminal, isTerminal, finalSteinerTree, terminals.front());
120  }
121 
132  EdgeWeightedGraphCopy<T> &intermediateTerminalSpanningTree,
133  const node s,
134  int numberOfTerminals,
135  const NodeArray<bool> &isTerminal);
136 };
137 
138 template<typename T>
140  const List<node> &terminals,
141  const NodeArray<bool> &isTerminal,
142  const NodeArray<bool> &isOriginalTerminal,
143  EdgeWeightedGraphCopy<T> *&finalSteinerTree,
144  const node startNode)
145 {
147 
148  EdgeWeightedGraphCopy<T> terminalSpanningTree;
149  terminalSpanningTree.createEmpty(G);
150  terminalDijkstra(G, terminalSpanningTree, startNode, terminals.size(), isTerminal);
151 
152  finalSteinerTree = new EdgeWeightedGraphCopy<T>(G);
153  for(node u : G.nodes) {
154  if (!terminalSpanningTree.copy(u)) {
155  finalSteinerTree->delNode(finalSteinerTree->copy(u));
156  }
157  }
158 
159  T mstWeight = makeMinimumSpanningTree(*finalSteinerTree, finalSteinerTree->edgeWeights());
160  mstWeight -= MinSteinerTreeModule<T>::pruneAllDanglingSteinerPaths(*finalSteinerTree, isOriginalTerminal);
161 
162  return mstWeight;
163 }
164 
165 template<typename T>
167  EdgeWeightedGraphCopy<T> &intermediateTerminalSpanningTree, const node s, int numberOfTerminals,
168  const NodeArray<bool> &isTerminal)
169 {
170  NodeArray<edge> predecessor(wG, nullptr);
171  NodeArray<T> distance(wG, std::numeric_limits<T>::max());
172  distance[s] = 0;
173  NodeArray<T> bestDistance(wG, std::numeric_limits<T>::max());
174  bestDistance[s] = 0;
175  NodeArray<bool> isInQueue(wG, true);
176 
177  PrioritizedMapQueue<node, T> queue(wG); //priority queue
178  for (node v : wG.nodes) {
179  queue.push(v, distance[v]);
180  }
181 
182  T mstWeight = 0;
183  int terminalsFound = 1;
184  while (!queue.empty() && terminalsFound < numberOfTerminals) {
185  node v = queue.topElement();
186  queue.pop();
187  isInQueue[v] = false;
188  bestDistance[v] = distance[v];
189  if (isTerminal[v]
190  && distance[v] > 0) {
191  ++terminalsFound;
192  // insert path from new node to old tree
193  node tmpT = intermediateTerminalSpanningTree.newNode(v);
194  while (distance[v] > 0) {
195  distance[v] = 0;
196  queue.push(v, distance[v]);
197  isInQueue[v] = true;
198  const edge e = predecessor[v];
199  OGDF_ASSERT(e);
200  const node w = e->opposite(v);
201  node tmpS = intermediateTerminalSpanningTree.copy(w);
202  if (!tmpS) {
203  tmpS = intermediateTerminalSpanningTree.newNode(w);
204  }
205  edge tmpE;
206  if (e->target() == v) {
207  tmpE = intermediateTerminalSpanningTree.newEdge(tmpS, tmpT, wG.weight(e));
208  } else {
209  tmpE = intermediateTerminalSpanningTree.newEdge(tmpT, tmpS, wG.weight(e));
210  }
211  mstWeight += wG.weight(e);
212  intermediateTerminalSpanningTree.setEdge(e, tmpE);
213  tmpT = tmpS;
214  v = w;
215  }
216  } else { // !isTerminal[v] || distance[v] == 0
217  for(adjEntry adj : v->adjEntries) {
218  const node w = adj->twinNode();
219  const edge e = adj->theEdge();
220  if (distance[w] > distance[v] + wG.weight(e)
221  && bestDistance[w] >= distance[w]) {
222  distance[w] = distance[v] + wG.weight(e);
223  if (!isInQueue[w]) {
224  queue.push(w, distance[w]);
225  isInQueue[w] = true;
226  } else {
227  queue.decrease(w, distance[w]);
228  }
229  predecessor[w] = e;
230  }
231  }
232  }
233  }
234  return mstWeight;
235 }
236 
237 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
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
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:412
void setEdge(edge eOrig, edge eCopy)
sets eOrig to be the corresponding original edge of eCopy and vice versa
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
edge newEdge(node u, node v, T weight)
Declaration of ogdf::MinSteinerTreeModule.
virtual T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree) override
Computes the actual Steiner tree.
Extends the GraphCopy concept to weighted graphs.
T makeMinimumSpanningTree(Graph &G, const EdgeArray< T > &weight)
Reduce a graph to its minimum spanning tree (MST) using Kruskal&#39;s algorithm.
Prioritized queue interface wrapper for heaps.
T weight(const edge e) const
void createEmpty(const Graph &wG)
const E & topElement() const
Returns the topmost element in the queue.
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree, const node startNode)
An extended call method with specific start node.
T terminalDijkstra(const EdgeWeightedGraph< T > &wG, EdgeWeightedGraphCopy< T > &intermediateTerminalSpanningTree, const node s, int numberOfTerminals, const NodeArray< bool > &isTerminal)
Modified Dijkstra algorithm to solve the Minimum Steiner Tree problem.
const_reference front() const
Returns a const reference to the first element.
Definition: List.h:274
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
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
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:338
This class implements the minimum Steiner tree 2-approximation algorithm by Takahashi and Matsuyama w...
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
bool empty() const
Checks whether the queue is empty.
int size() const
Returns the number of elements in the list.
Definition: List.h:1312
The namespace for all OGDF objects.
Declaration of extended graph algorithms.
virtual void delNode(node v) override
Removes node v and all its adjacent edges cleaning-up their corresponding lists of original edges...
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
bool isConnected(const Graph &G)
Returns true iff G is connected.
Class for adjacency list elements.
Definition: Graph_d.h:79
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, const NodeArray< bool > &isOriginalTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
An extended call method with intermediate and final (original) terminals.
Declaration of doubly linked lists and iterators.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200