Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MinSteinerTreePrimalDual.h
Go to the documentation of this file.
1 
34 #pragma once
35 
36 #include <limits>
37 
41 
42 //#define OGDF_MINSTEINERTREE_PRIMAL_DUAL_LOGGING
43 
44 namespace ogdf {
45 
56 template<typename T>
58 private:
62  const T MAX_VALUE = std::numeric_limits<T>::max();
63 
68  double m_lowerBound;
70 
77  void mergeComponents(const node v, const node w);
78 
85  void makeActive(int component);
86 
92  bool isActive(int component) const;
93 
99  int getComponent(const node v) const;
100 
107  double getNextEdge(edge *nextEdge);
108 
115  void updatePriorities(double eps);
116 
120  void init();
121 
122 protected:
138  virtual T computeSteinerTree(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal,
139  EdgeWeightedGraphCopy<T> *&finalSteinerTree) override;
140 
141 public:
142  virtual T call(const EdgeWeightedGraph<T> &G, const List<node> &terminals, const NodeArray<bool> &isTerminal, EdgeWeightedGraphCopy<T> *&finalSteinerTree) override
143  {
144  m_lowerBound = 0;
145  return MinSteinerTreeModule<T>::call(G, terminals, isTerminal, finalSteinerTree);
146  }
147 
153  double getLastLowerBound() const;
154 };
155 
156 template<typename T>
158 {
163 }
164 
165 template<typename T>
167 {
169 }
170 
171 template<typename T>
172 bool MinSteinerTreePrimalDual<T>::isActive(int component) const
173 {
174  return m_activeComponentIterators[component].valid();
175 }
176 
177 template<typename T>
179 {
181 }
182 
183 template<typename T>
185 {
186 
187  int compV = getComponent(v);
188  int compW = getComponent(w);
189 
190  // remove former components
191  if(isActive(compV)) {
193  }
194  if(isActive(compW)) {
196  }
197 
198  // craete new component
199  int compNew = m_pComponents->link(compV, compW);
200  if(!m_activeComponents.empty()) {
201  makeActive(compNew);
202  }
203 }
204 
205 template<typename T>
207 {
208  List<node> nodes;
209  m_pGraph->allNodes(nodes);
210  for(node v : nodes) {
211  if(isActive(getComponent(v))) {
212  m_priorities(v) += eps;
213  }
214  }
215 }
216 
217 template<typename T>
219 {
220  double result = MAX_VALUE;
221  *nextEdge = nullptr;
222 
223  List<edge> edges;
224  m_pGraph->allEdges(edges);
225  for(edge e : edges) {
226  node v = e->source();
227  node w = e->target();
228  int compV = getComponent(v);
229  int compW = getComponent(w);
230  if(compV != compW) { // spanning different components ?
231  double value = m_pGraph->weight(e) - m_priorities(v) - m_priorities(w);
232  int divisor = isActive(compV) + isActive(compW);
233  if(divisor == 0) {
234  value = MAX_VALUE;
235  } else {
236  value /= divisor;
237  }
238  if(*nextEdge == nullptr || value < result) {
239  *nextEdge = e;
240  result = value;
241  }
242  }
243  }
244  return result;
245 }
246 
247 template<typename T>
249 {
250  m_pGraph = &G;
251  m_pTerminals = &terminals;
252  m_pIsTerminal = &isTerminal;
253  DisjointSets<> components;
254  m_pComponents = &components;
255 
256  finalSteinerTree = new EdgeWeightedGraphCopy<T>();
257  finalSteinerTree->createEmpty(*m_pGraph);
258 
259  init();
260 
261  // initialize components
262  List<node> nodes;
263  m_pGraph->allNodes(nodes);
264  for(node v : nodes) {
265  int comp = m_pComponents->makeSet();
266  m_componentMapping[v] = comp;
267  if(isTerminal(v)) {
268  makeActive(comp);
269  }
270  }
271 
272 #ifdef OGDF_MINSTEINERTREE_PRIMAL_DUAL_LOGGING
273  cout << "Goemans primal-dual starting..." << std::endl;
274  cout << "terminals:";
275  for(node v : *m_pTerminals) {
276  cout << " " << v;
277  }
278  cout << std::endl;
279 
280  cout << "loop starting... " << std::endl;
281 #endif
282 
283  T result = 0;
284  while(!m_activeComponents.empty()) {
285 #ifdef OGDF_MINSTEINERTREE_PRIMAL_DUAL_LOGGING
286  cout << "active component exists" << std::endl;
287 #endif
288  // idendify next edge
289  edge minEdge = nullptr;
290  double minValue = getNextEdge(&minEdge);
291  OGDF_ASSERT(minEdge != nullptr);
292 
293 #ifdef OGDF_MINSTEINERTREE_PRIMAL_DUAL_LOGGING
294  cout << "minEdge found: " << minEdge << ", weight is " << m_pGraph->weight(minEdge) << ", adjusted weight is " << minValue << std::endl;
295 #endif
296  node v = minEdge->source();
297  node w = minEdge->target();
298 
299  // include nodes in Steiner tree
300  if(finalSteinerTree->copy(v) == nullptr) {
301  finalSteinerTree->newNode(v);
302  }
303  if(finalSteinerTree->copy(w) == nullptr) {
304  finalSteinerTree->newNode(w);
305  }
306 
307  // include edge in Steiner tree
308  T weight = m_pGraph->weight(minEdge);
309  result += weight;
310  finalSteinerTree->newEdge(minEdge, weight);
311 
312  m_lowerBound += m_activeComponents.size() * minValue;
313 
314  mergeComponents(v, w);
315 
316  updatePriorities(minValue);
317  }
318  result -= this->pruneAllDanglingSteinerPaths(*finalSteinerTree, *m_pIsTerminal);
319 
320 #ifdef OGDF_MINSTEINERTREE_PRIMAL_DUAL_LOGGING
321  cout << "calculation finished!" << std::endl;
322 #endif
323  return result;
324 }
325 
326 template<typename T>
328 {
329  return m_lowerBound;
330 }
331 
332 } // end namespace ogdf
bool isActive(int component) const
Returns whether the given component is active.
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
void clear()
Removes all elements from the list.
Definition: List.h:1394
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
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
virtual T call(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
Builds a minimum Steiner tree given a weighted graph and a list of terminals.
This class serves as an interface for various methods to compute minimum or approximations of minimum...
Indexed arrays using hashing for element access.
Definition: HashArray.h:94
edge newEdge(node u, node v, T weight)
Interface of Minimum Steiner Tree Algorithms.
Extends the GraphCopy concept to weighted graphs.
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.
void del(iterator it)
Removes it from the list.
Definition: List.h:1380
const EdgeWeightedGraph< T > * m_pGraph
void createEmpty(const Graph &wG)
virtual T call(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.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
int find(disjoint_sets::CompressionOption< CompressionOptions::PathCompression >, int set)
Definition: DisjointSets.h:269
A Union/Find data structure for maintaining disjoint sets.
Definition: DisjointSets.h:81
int getComponent(const node v) const
Finds the biggest set including node v.
Class for the representation of edges.
Definition: Graph_d.h:291
Class for the representation of nodes.
Definition: Graph_d.h:167
void makeActive(int component)
Marks the specified component as active.
Primal-Dual approximation algorithm for Steiner tree problems.
static T pruneAllDanglingSteinerPaths(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Prune Steiner nodes with degree 1 and their paths to terminal or branching nodes. ...
int makeSet()
Initializes a singleton set.
Definition: DisjointSets.h:172
void init()
Initializes all required datastructes.
HashArray< int, ListIterator< int > > m_activeComponentIterators
int size() const
Returns the number of elements in the list.
Definition: List.h:1255
Implementation of disjoint sets data structures (union-find functionality).
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
int link(disjoint_sets::LinkOption< LinkOptions::Naive >, int set1, int set2)
Definition: DisjointSets.h:599
The namespace for all OGDF objects.
double getNextEdge(edge *nextEdge)
Idendifies the next edge with a tight-to-be packing constraint.
void updatePriorities(double eps)
Must be called after merging any two components.
bool empty() const
Returns true iff the list is empty.
Definition: List.h:236
const NodeArray< bool > * m_pIsTerminal
double getLastLowerBound() const
Returns the lower bound calculated while solving the last problem.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
void mergeComponents(const node v, const node w)
Merges two disjoint components.