Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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 
74  virtual T call(const EdgeWeightedGraph<T> &G,
75  const List<node> &terminals,
76  const NodeArray<bool> &isTerminal,
77  EdgeWeightedGraphCopy<T> *&finalSteinerTree,
78  const node startNode)
79  {
80  return call(G, terminals, isTerminal, isTerminal, finalSteinerTree, startNode);
81  }
82 
83  virtual T call(const EdgeWeightedGraph<T> &G,
84  const List<node> &terminals,
85  const NodeArray<bool> &isTerminal,
86  const NodeArray<bool> &isOriginalTerminal,
87  EdgeWeightedGraphCopy<T> *&finalSteinerTree)
88  {
89  return call(G, terminals, isTerminal, isOriginalTerminal, finalSteinerTree, terminals.front());
90  }
91 
93 
106  virtual T call(const EdgeWeightedGraph<T> &G,
107  const List<node> &terminals,
108  const NodeArray<bool> &isTerminal,
109  const NodeArray<bool> &isOriginalTerminal,
110  EdgeWeightedGraphCopy<T> *&finalSteinerTree,
111  const node startNode);
112 
113 protected:
123  const EdgeWeightedGraph<T> &G,
124  const List<node> &terminals,
125  const NodeArray<bool> &isTerminal,
126  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override
127  {
128  return call(G, terminals, isTerminal, isTerminal, finalSteinerTree, terminals.front());
129  }
130 
141  EdgeWeightedGraphCopy<T> &intermediateTerminalSpanningTree,
142  const node s,
143  int numberOfTerminals,
144  const NodeArray<bool> &isTerminal);
145 };
146 
147 template<typename T>
149  const List<node> &terminals,
150  const NodeArray<bool> &isTerminal,
151  const NodeArray<bool> &isOriginalTerminal,
152  EdgeWeightedGraphCopy<T> *&finalSteinerTree,
153  const node startNode)
154 {
155  EdgeWeightedGraphCopy<T> terminalSpanningTree;
156  terminalSpanningTree.createEmpty(G);
157  terminalDijkstra(G, terminalSpanningTree, startNode, terminals.size(), isTerminal);
158 
159  finalSteinerTree = new EdgeWeightedGraphCopy<T>(G);
160  for(node u : G.nodes) {
161  if (!terminalSpanningTree.copy(u)) {
162  finalSteinerTree->delNode(finalSteinerTree->copy(u));
163  }
164  }
165 
166  T mstWeight = makeMinimumSpanningTree(*finalSteinerTree, finalSteinerTree->edgeWeights());
167  mstWeight -= MinSteinerTreeModule<T>::pruneAllDanglingSteinerPaths(*finalSteinerTree, isOriginalTerminal);
168 
169  return mstWeight;
170 }
171 
172 template<typename T>
174  EdgeWeightedGraphCopy<T> &intermediateTerminalSpanningTree, const node s, int numberOfTerminals,
175  const NodeArray<bool> &isTerminal)
176 {
177  NodeArray<edge> predecessor(wG, nullptr);
178  NodeArray<T> distance(wG, numeric_limits<T>::max());
179  distance[s] = 0;
180  NodeArray<T> bestDistance(wG, numeric_limits<T>::max());
181  bestDistance[s] = 0;
182  NodeArray<bool> isInQueue(wG, true);
183 
184  PrioritizedMapQueue<node, T> queue(wG); //priority queue
185  for (node v : wG.nodes) {
186  queue.push(v, distance[v]);
187  }
188 
189  T mstWeight = 0;
190  int terminalsFound = 1;
191  while (!queue.empty() && terminalsFound < numberOfTerminals) {
192  node v = queue.topElement();
193  queue.pop();
194  isInQueue[v] = false;
195  bestDistance[v] = distance[v];
196  if (isTerminal[v]
197  && distance[v] > 0) {
198  ++terminalsFound;
199  // insert path from new node to old tree
200  node tmpT = intermediateTerminalSpanningTree.newNode(v);
201  while (distance[v] > 0) {
202  distance[v] = 0;
203  queue.push(v, distance[v]);
204  isInQueue[v] = true;
205  const edge e = predecessor[v];
206  OGDF_ASSERT(e);
207  const node w = e->opposite(v);
208  node tmpS = intermediateTerminalSpanningTree.copy(w);
209  if (!tmpS) {
210  tmpS = intermediateTerminalSpanningTree.newNode(w);
211  }
212  edge tmpE;
213  if (e->target() == v) {
214  tmpE = intermediateTerminalSpanningTree.newEdge(tmpS, tmpT, wG.weight(e));
215  } else {
216  tmpE = intermediateTerminalSpanningTree.newEdge(tmpT, tmpS, wG.weight(e));
217  }
218  mstWeight += wG.weight(e);
219  intermediateTerminalSpanningTree.setEdge(e, tmpE);
220  tmpT = tmpS;
221  v = w;
222  }
223  } else { // !isTerminal[v] || distance[v] == 0
224  for(adjEntry adj : v->adjEntries) {
225  const node w = adj->twinNode();
226  const edge e = adj->theEdge();
227  if (distance[w] > distance[v] + wG.weight(e)
228  && bestDistance[w] >= distance[w]) {
229  distance[w] = distance[v] + wG.weight(e);
230  if (!isInQueue[w]) {
231  queue.push(w, distance[w]);
232  isInQueue[w] = true;
233  } else {
234  queue.decrease(w, distance[w]);
235  }
236  predecessor[w] = e;
237  }
238  }
239  }
240  }
241  return mstWeight;
242 }
243 
244 } // end namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:95
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:336
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:413
void setEdge(edge eOrig, edge eCopy)
sets eOrig to be the corresponding original edge of eCopy and vice versa
This class serves as an interface for various methods to compute minimum or approximations of minimum...
edge newEdge(node u, node v, T weight)
Interface of Minimum Steiner Tree Algorithms.
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.
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:254
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:105
Class for the representation of edges.
Definition: Graph_d.h:291
Class for the representation of nodes.
Definition: Graph_d.h:167
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:335
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)
Prune Steiner nodes with degree 1 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:1255
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:545
Class for adjacency list elements.
Definition: Graph_d.h:78
#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)
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:198