Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MinSteinerTreeMehlhorn.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/List.h>
36 #include <ogdf/graphalg/Voronoi.h>
39 
41 #include <sstream>
42 
43 namespace ogdf {
44 
55 template<typename T>
57 public:
59 
61 
70  static void calculateCompleteGraph(const EdgeWeightedGraph<T> &wG, const List<node> &terminals, const Voronoi<T> &voronoi,
71  EdgeArray<edge> &bridges, EdgeWeightedGraphCopy<T> &completeTerminalGraph);
72 
73 protected:
82  virtual T computeSteinerTree(
83  const EdgeWeightedGraph<T> &G,
84  const List<node> &terminals,
85  const NodeArray<bool> &isTerminal,
86  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override;
87 
97  void reinsertShortestPaths(EdgeWeightedGraphCopy<T> &completeTerminalGraph, const Voronoi<T> &voronoi,
98  const NodeArray<edge> &mstPred, const EdgeArray<edge> &bridges, EdgeWeightedGraphCopy<T> &finalSteinerTree,
99  const EdgeWeightedGraph<T> &wG);
100 
108  void insertPath(node u, const Voronoi<T> &voronoi,
109  EdgeWeightedGraphCopy<T> &finalSteinerTree, const EdgeWeightedGraph<T> &wG);
110 
114  struct MehlhornTriple {
117  T value;
119  };
120 
124  class MehlhornTripleBucketMaxFunc: public BucketFunc<MehlhornTriple> {
125  public:
127 
128  int getBucket(const MehlhornTriple& MT) {
129  int source_index = MT.u->index();
130  int target_index = MT.v->index();
131  OGDF_ASSERT(source_index != target_index);
132  if (source_index < target_index)
133  return target_index;
134  else
135  return source_index;
136  }
137  };
138 
142  class MehlhornTripleBucketMinFunc: public BucketFunc<MehlhornTriple> {
143  public:
145 
146  int getBucket(const MehlhornTriple& MT) {
147  int source_index = MT.u->index();
148  int target_index = MT.v->index();
149  OGDF_ASSERT(source_index != target_index);
150  if (source_index < target_index)
151  return source_index;
152  else
153  return target_index;
154  }
155  };
156 };
157 
158 template<typename T>
160  const List<node> &terminals,
161  const NodeArray<bool> &isTerminal,
162  EdgeWeightedGraphCopy<T> *&finalSteinerTree)
163 {
164  EdgeWeightedGraphCopy<T> completeTerminalGraph;
165  EdgeArray<edge> bridges;
166  Voronoi<T> voronoi(G, G.edgeWeights(), terminals);
167 
168  calculateCompleteGraph(G, terminals, voronoi, bridges, completeTerminalGraph);
169 
170  NodeArray<edge> mstPred(completeTerminalGraph);
171  computeMinST(completeTerminalGraph, completeTerminalGraph.edgeWeights(), mstPred);
172 
173  finalSteinerTree = new EdgeWeightedGraphCopy<T>;
174  finalSteinerTree->createEmpty(G);
175 
176  reinsertShortestPaths(completeTerminalGraph, voronoi, mstPred, bridges, *finalSteinerTree, G);
177 
178  T mstWeight = makeMinimumSpanningTree(*finalSteinerTree, finalSteinerTree->edgeWeights());
179  mstWeight -= MinSteinerTreeModule<T>::pruneAllDanglingSteinerPaths(*finalSteinerTree, isTerminal);
180 
181  return mstWeight;
182 }
183 
184 template<typename T>
186  const List<node> &terminals,
187  const Voronoi<T> &voronoi,
188  EdgeArray<edge> &bridges,
189  EdgeWeightedGraphCopy<T> &completeTerminalGraph)
190 {
191  completeTerminalGraph.createEmpty(wG);
192 
193  for (node v : terminals) {
194  completeTerminalGraph.newNode(v);
195  }
196  if (completeTerminalGraph.numberOfNodes() <= 1) {
197  bridges.init(completeTerminalGraph);
198  return;
199  }
200 
201  // extract complete graph edges
202  List<MehlhornTriple> triples;
203  for(edge e : wG.edges) {
204  MehlhornTriple triple;
205  triple.u = voronoi.seed(e->source());
206  triple.v = voronoi.seed(e->target());
207  if (triple.u != triple.v) {
208  triple.value = voronoi.distance(e->source()) + voronoi.distance(e->target()) + wG.weight(e);
209  triple.bridge = e;
210  triples.pushBack(triple);
211  }
212  }
213 
216 
217  triples.bucketSort(0, wG.maxNodeIndex(), mtbMax);
218  triples.bucketSort(0, wG.maxNodeIndex(), mtbMin);
219 
220  int currentSource = triples.front().u->index();
221  int currentTarget = triples.front().v->index();
222  ListConstIterator<MehlhornTriple> minTriple = triples.begin();
223 
224  bridges.init(completeTerminalGraph);
225 
226  for (ListConstIterator<MehlhornTriple> mtIt = triples.begin().succ(); mtIt.valid(); ++mtIt) {
227  if (((*mtIt).u->index() == currentSource && (*mtIt).v->index() == currentTarget)
228  || ((*mtIt).u->index() == currentTarget && (*mtIt).v->index() == currentSource)) {
229  if ((*mtIt).value < (*minTriple).value) {
230  minTriple = mtIt;
231  }
232  } else {
233  // add new direct edge
234  edge tmp = completeTerminalGraph.newEdge(completeTerminalGraph.copy((*minTriple).u),
235  completeTerminalGraph.copy((*minTriple).v), (*minTriple).value);
236  bridges[tmp] = (*minTriple).bridge;
237 
238  currentSource = (*mtIt).u->index();
239  currentTarget = (*mtIt).v->index();
240  minTriple = mtIt;
241  }
242  }
243  // insert last triple
244  if (minTriple.valid()) {
245  edge tmp = completeTerminalGraph.newEdge(completeTerminalGraph.copy((*minTriple).u),
246  completeTerminalGraph.copy((*minTriple).v), (*minTriple).value);
247  bridges[tmp] = (*minTriple).bridge;
248  }
249 }
250 
251 template<typename T>
253  const Voronoi<T> &voronoi,
254  const NodeArray<edge> &mstPred,
255  const EdgeArray<edge> &bridges,
256  EdgeWeightedGraphCopy<T> &finalSteinerTree,
257  const EdgeWeightedGraph<T> &wG)
258 {
259  for(node u : completeTerminalGraph.nodes) {
260  if (mstPred[u] != nullptr) {
261  edge bridge = bridges[mstPred[u]];
262  node v = bridge->source();
263  node w = bridge->target();
264  insertPath(v, voronoi, finalSteinerTree, wG);
265  insertPath(w, voronoi, finalSteinerTree, wG);
266  edge e = finalSteinerTree.newEdge(finalSteinerTree.copy(v), finalSteinerTree.copy(w), wG.weight(bridge));
267  finalSteinerTree.setEdge(bridge, e);
268  }
269  }
270 }
271 
272 template<typename T>
274  const Voronoi<T> &voronoi,
275  EdgeWeightedGraphCopy<T> &finalSteinerTree,
276  const EdgeWeightedGraph<T> &wG)
277 {
278  node currentSource;
279  node currentTarget = finalSteinerTree.copy(u);
280  if (!currentTarget) {
281  currentTarget = finalSteinerTree.newNode(u);
282  }
283  edge e = voronoi.predecessorEdge(u);
284  edge newE;
285  while (e && finalSteinerTree.chain(e).empty()) { // e is not in ST yet
286  if ((currentSource = finalSteinerTree.copy(e->opposite(finalSteinerTree.original(currentTarget)))) == nullptr) {
287  currentSource = finalSteinerTree.newNode(e->opposite(finalSteinerTree.original(currentTarget)));
288  }
289  if (finalSteinerTree.original(currentSource) == e->source()) {
290  newE = finalSteinerTree.newEdge(currentSource, currentTarget, wG.weight(e));
291  } else {
292  newE = finalSteinerTree.newEdge(currentTarget, currentSource, wG.weight(e));
293  }
294  finalSteinerTree.setEdge(e, newE);
295  currentTarget = currentSource;
296  e = voronoi.predecessorEdge(finalSteinerTree.original(currentTarget));
297  }
298 }
299 
300 } // end namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
Declaration of class GraphAttributes which extends a Graph by additional attributes.
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 init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:320
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
T computeMinST(const Graph &G, const EdgeArray< T > &weight, EdgeArray< bool > &isInTree)
Computes a minimum spanning tree using Prim&#39;s algorithm.
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.
iterator begin()
Returns an iterator to the first element of the list.
Definition: List.h:332
const EdgeArray< T > & edgeWeights() const
void reinsertShortestPaths(EdgeWeightedGraphCopy< T > &completeTerminalGraph, const Voronoi< T > &voronoi, const NodeArray< edge > &mstPred, const EdgeArray< edge > &bridges, EdgeWeightedGraphCopy< T > &finalSteinerTree, const EdgeWeightedGraph< T > &wG)
Swaps an edge in the complete terminal graph with the corresponding shortest path in the original gra...
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.
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
void bucketSort(int l, int h, BucketFunc< E > &f)
Sorts the list using bucket sort.
Definition: List.h:1494
T weight(const edge e) const
void createEmpty(const Graph &wG)
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:548
Computes Voronoi regions in an edge weighted graph.
Definition: Voronoi.h:45
const_reference front() const
Returns a const reference to the first element.
Definition: List.h:254
const EdgeArray< T > & edgeWeights() const
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
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.
node seed(node v) const
Returns the Voronoi seed of node v.
Definition: Voronoi.h:86
const List< edge > & chain(edge e) const
Returns the list of edges coresponding to edge e.
Definition: GraphCopy.h:343
Class for the representation of edges.
Definition: Graph_d.h:291
Helper class to sort MehlhornTriples lexicographically.
T distance(node v) const
Returns the distance between v and its Voronoi seed.
Definition: Voronoi.h:80
Class for the representation of nodes.
Definition: Graph_d.h:167
Encapsulates a pointer to a list element.
Definition: List.h:41
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:335
Helper class to sort MehlhornTriples lexicographically.
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:287
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prune Steiner nodes with degree 1 and their paths to terminal or branching nodes. ...
Abstract base class for bucket functions.
Definition: basic.h:255
int index() const
Returns the (unique) node index.
Definition: Graph_d.h:201
void insertPath(node u, const Voronoi< T > &voronoi, EdgeWeightedGraphCopy< T > &finalSteinerTree, const EdgeWeightedGraph< T > &wG)
Inserts a shortest path corresponding to an edge in the complete terminal graph.
int maxNodeIndex() const
Returns the largest used node index.
Definition: Graph_d.h:584
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
The namespace for all OGDF objects.
Implementation of Voronoi regions in an EdgeWeightedGraph.
This class implements the Minimum Steiner Tree 2-approximation algorithm by Mehlhorn.
static void calculateCompleteGraph(const EdgeWeightedGraph< T > &wG, const List< node > &terminals, const Voronoi< T > &voronoi, EdgeArray< edge > &bridges, EdgeWeightedGraphCopy< T > &completeTerminalGraph)
Builds a complete terminal graph.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:578
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
bool empty() const
Returns true iff the list is empty.
Definition: List.h:236
bool valid() const
Returns true iff the iterator points to an element.
Definition: List.h:112
Represents a triple as specified in the algorithms description (see paper)
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
edge predecessorEdge(node v) const
Returns the edge incident to v and its predecessor. Note that the predecessor of a terminal is nullpt...
Definition: Voronoi.h:67
Declaration of doubly linked lists and iterators.