Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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 
40 namespace ogdf {
41 
52 template<typename T>
54 public:
56 
58 
67  static void calculateCompleteGraph(const EdgeWeightedGraph<T> &wG, const List<node> &terminals, const Voronoi<T> &voronoi,
68  EdgeArray<edge> &bridges, EdgeWeightedGraphCopy<T> &completeTerminalGraph);
69 
70 protected:
79  virtual T computeSteinerTree(
80  const EdgeWeightedGraph<T> &G,
81  const List<node> &terminals,
82  const NodeArray<bool> &isTerminal,
83  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override;
84 
94  void reinsertShortestPaths(EdgeWeightedGraphCopy<T> &completeTerminalGraph, const Voronoi<T> &voronoi,
95  const NodeArray<edge> &mstPred, const EdgeArray<edge> &bridges, EdgeWeightedGraphCopy<T> &finalSteinerTree,
96  const EdgeWeightedGraph<T> &wG);
97 
105  void insertPath(node u, const Voronoi<T> &voronoi,
106  EdgeWeightedGraphCopy<T> &finalSteinerTree, const EdgeWeightedGraph<T> &wG);
107 
111  struct MehlhornTriple {
114  T value;
116  };
117 
121  class MehlhornTripleBucketMaxFunc: public BucketFunc<MehlhornTriple> {
122  public:
124 
125  int getBucket(const MehlhornTriple& MT) {
126  int source_index = MT.u->index();
127  int target_index = MT.v->index();
128  OGDF_ASSERT(source_index != target_index);
129  if (source_index < target_index)
130  return target_index;
131  else
132  return source_index;
133  }
134  };
135 
139  class MehlhornTripleBucketMinFunc: public BucketFunc<MehlhornTriple> {
140  public:
142 
143  int getBucket(const MehlhornTriple& MT) {
144  int source_index = MT.u->index();
145  int target_index = MT.v->index();
146  OGDF_ASSERT(source_index != target_index);
147  if (source_index < target_index)
148  return source_index;
149  else
150  return target_index;
151  }
152  };
153 };
154 
155 template<typename T>
157  const List<node> &terminals,
158  const NodeArray<bool> &isTerminal,
159  EdgeWeightedGraphCopy<T> *&finalSteinerTree)
160 {
161  EdgeWeightedGraphCopy<T> completeTerminalGraph;
162  EdgeArray<edge> bridges;
163  Voronoi<T> voronoi(G, G.edgeWeights(), terminals);
164 
165  calculateCompleteGraph(G, terminals, voronoi, bridges, completeTerminalGraph);
166 
167  NodeArray<edge> mstPred(completeTerminalGraph);
168  computeMinST(completeTerminalGraph, completeTerminalGraph.edgeWeights(), mstPred);
169 
170  finalSteinerTree = new EdgeWeightedGraphCopy<T>;
171  finalSteinerTree->createEmpty(G);
172 
173  reinsertShortestPaths(completeTerminalGraph, voronoi, mstPred, bridges, *finalSteinerTree, G);
174 
175  T mstWeight = makeMinimumSpanningTree(*finalSteinerTree, finalSteinerTree->edgeWeights());
176  mstWeight -= MinSteinerTreeModule<T>::pruneAllDanglingSteinerPaths(*finalSteinerTree, isTerminal);
177 
178  return mstWeight;
179 }
180 
181 template<typename T>
183  const List<node> &terminals,
184  const Voronoi<T> &voronoi,
185  EdgeArray<edge> &bridges,
186  EdgeWeightedGraphCopy<T> &completeTerminalGraph)
187 {
188  completeTerminalGraph.createEmpty(wG);
189 
190  for (node v : terminals) {
191  completeTerminalGraph.newNode(v);
192  }
193  if (completeTerminalGraph.numberOfNodes() <= 1) {
194  bridges.init(completeTerminalGraph);
195  return;
196  }
197 
198  // extract complete graph edges
199  List<MehlhornTriple> triples;
200  for(edge e : wG.edges) {
201  MehlhornTriple triple;
202  triple.u = voronoi.seed(e->source());
203  triple.v = voronoi.seed(e->target());
204  if (triple.u != triple.v) {
205  triple.value = voronoi.distance(e->source()) + voronoi.distance(e->target()) + wG.weight(e);
206  triple.bridge = e;
207  triples.pushBack(triple);
208  }
209  }
210 
213 
214  triples.bucketSort(0, wG.maxNodeIndex(), mtbMax);
215  triples.bucketSort(0, wG.maxNodeIndex(), mtbMin);
216 
217  int currentSource = triples.front().u->index();
218  int currentTarget = triples.front().v->index();
219  ListConstIterator<MehlhornTriple> minTriple = triples.begin();
220 
221  bridges.init(completeTerminalGraph);
222 
223  for (ListConstIterator<MehlhornTriple> mtIt = triples.begin().succ(); mtIt.valid(); ++mtIt) {
224  if (((*mtIt).u->index() == currentSource && (*mtIt).v->index() == currentTarget)
225  || ((*mtIt).u->index() == currentTarget && (*mtIt).v->index() == currentSource)) {
226  if ((*mtIt).value < (*minTriple).value) {
227  minTriple = mtIt;
228  }
229  } else {
230  // add new direct edge
231  edge tmp = completeTerminalGraph.newEdge(completeTerminalGraph.copy((*minTriple).u),
232  completeTerminalGraph.copy((*minTriple).v), (*minTriple).value);
233  bridges[tmp] = (*minTriple).bridge;
234 
235  currentSource = (*mtIt).u->index();
236  currentTarget = (*mtIt).v->index();
237  minTriple = mtIt;
238  }
239  }
240  // insert last triple
241  if (minTriple.valid()) {
242  edge tmp = completeTerminalGraph.newEdge(completeTerminalGraph.copy((*minTriple).u),
243  completeTerminalGraph.copy((*minTriple).v), (*minTriple).value);
244  bridges[tmp] = (*minTriple).bridge;
245  }
246 }
247 
248 template<typename T>
250  const Voronoi<T> &voronoi,
251  const NodeArray<edge> &mstPred,
252  const EdgeArray<edge> &bridges,
253  EdgeWeightedGraphCopy<T> &finalSteinerTree,
254  const EdgeWeightedGraph<T> &wG)
255 {
256  for(node u : completeTerminalGraph.nodes) {
257  if (mstPred[u] != nullptr) {
258  edge bridge = bridges[mstPred[u]];
259  node v = bridge->source();
260  node w = bridge->target();
261  insertPath(v, voronoi, finalSteinerTree, wG);
262  insertPath(w, voronoi, finalSteinerTree, wG);
263  edge e = finalSteinerTree.newEdge(finalSteinerTree.copy(v), finalSteinerTree.copy(w), wG.weight(bridge));
264  finalSteinerTree.setEdge(bridge, e);
265  }
266  }
267 }
268 
269 template<typename T>
271  const Voronoi<T> &voronoi,
272  EdgeWeightedGraphCopy<T> &finalSteinerTree,
273  const EdgeWeightedGraph<T> &wG)
274 {
275  node currentSource;
276  node currentTarget = finalSteinerTree.copy(u);
277  if (!currentTarget) {
278  currentTarget = finalSteinerTree.newNode(u);
279  }
280  edge e = voronoi.predecessorEdge(u);
281  edge newE;
282  while (e && finalSteinerTree.chain(e).empty()) { // e is not in ST yet
283  if ((currentSource = finalSteinerTree.copy(e->opposite(finalSteinerTree.original(currentTarget)))) == nullptr) {
284  currentSource = finalSteinerTree.newNode(e->opposite(finalSteinerTree.original(currentTarget)));
285  }
286  if (finalSteinerTree.original(currentSource) == e->source()) {
287  newE = finalSteinerTree.newEdge(currentSource, currentTarget, wG.weight(e));
288  } else {
289  newE = finalSteinerTree.newEdge(currentTarget, currentSource, wG.weight(e));
290  }
291  finalSteinerTree.setEdge(e, newE);
292  currentTarget = currentSource;
293  e = voronoi.predecessorEdge(finalSteinerTree.original(currentTarget));
294  }
295 }
296 
297 namespace steiner_tree {
298 
299 template<typename T>
301  const EdgeWeightedGraph<T>& graph,
302  const List<node>& terminals) {
303  EdgeArray<edge> bridges;
304  Voronoi<T> voronoi(graph, graph.edgeWeights(), terminals);
305 
306  MinSteinerTreeMehlhorn<T>::calculateCompleteGraph(graph, terminals, voronoi, bridges, terminalSpanningTree);
307 
308  return makeMinimumSpanningTree(terminalSpanningTree, terminalSpanningTree.edgeWeights());
309 }
310 }
311 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
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 init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:283
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
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
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.
iterator begin()
Returns an iterator to the first element of the list.
Definition: List.h:352
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:1551
bool valid() const
Returns true iff the iterator points to an element.
Definition: List.h:124
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:568
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:274
const EdgeArray< T > & edgeWeights() const
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
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:103
const List< edge > & chain(edge e) const
Returns the list of edges coresponding to edge e.
Definition: GraphCopy.h:342
Class for the representation of edges.
Definition: Graph_d.h:292
Helper class to sort MehlhornTriples lexicographically.
T distance(node v) const
Returns the distance between v and its Voronoi seed.
Definition: Voronoi.h:97
Class for the representation of nodes.
Definition: Graph_d.h:169
Encapsulates a pointer to a list element.
Definition: List.h:42
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:338
Helper class to sort MehlhornTriples lexicographically.
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:286
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prunes nonterminal leaves and their paths to terminal or branching nodes.
Abstract base class for bucket functions.
Definition: basic.h:226
int index() const
Returns the (unique) node index.
Definition: Graph_d.h:203
T constructTerminalSpanningTreeUsingVoronoiRegions(EdgeWeightedGraphCopy< T > &terminalSpanningTree, const EdgeWeightedGraph< T > &graph, const List< node > &terminals)
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:604
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
The namespace for all OGDF objects.
Definition of ogdf::Voronoi class template.
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:598
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
bool empty() const
Returns true iff the list is empty.
Definition: List.h:256
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:84
Declaration of doubly linked lists and iterators.