Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MinSteinerTreeDualAscent.h
Go to the documentation of this file.
1 
33 #pragma once
34 
37 
38 // enable this to print log
39 //#define OGDF_DUAL_ASCENT_LOGGING
40 
41 namespace ogdf {
42 
52 template<typename T>
54 private:
55  const T MAX_VALUE = std::numeric_limits<T>::max();
56 
57  // parameters passed to the module
61 
62  GraphCopy m_diGraph; // the directed graph
63  GraphCopy m_steinerGraph; // the to-be constructed "almost" Steiner tree
64  EdgeArray<edge> m_origMapping; // maps each directed edge to its undirected original
65 
66  EdgeArray<bool> m_edgeInclusions; // stores the resulting Steiner tree
67  EdgeArray<T> m_edgeSlacks; // slack variables for each directed edge representing the adjusted weight
68 
70 
71  // components for the Steiner graph
72  NodeArray<int> m_componentMapping; // maps each node to its component
73 
78  void init();
79 
88  bool isTerminal(const node v, bool rootIsTerminal) const;
89 
96  int findActiveComponent(node *terminal) const;
97 
104  int findComponent(const node v) const;
105 
112  List<edge> *computeCutSet(const node v) const;
113 
123  bool isActiveComponent(const node v) const;
124 
129  void updateComponents();
130 
131 protected:
132 
149  const EdgeWeightedGraph<T> &G,
150  const List<node> &terminals,
152  EdgeWeightedGraphCopy<T> *&finalSteinerTree);
153 };
154 
155 template<typename T>
157 {
158  // local auxilary lists
159  List<node> nodes;
160  m_pOrigGraph->allNodes(nodes);
161  List<edge> edges;
162  m_pOrigGraph->allEdges(edges);
163 
164  // create directed graph
165  // and initialize slack variables
167  m_diGraph.clear();
168  m_edgeSlacks.init(m_diGraph);
169  m_origMapping.init(m_diGraph);
170 
171  // create resulting Steiner tree
175 
176  // TODO: Better choices possible?
177  m_rootTerminal = m_pTerminals->chooseElement();
178 
179  for(node v : nodes) {
180  node w = m_diGraph.newNode(v);
182  }
183 
184  for(edge e : edges) {
185  node source = m_diGraph.copy(e->source());
186  node target = m_diGraph.copy(e->target());
187  edge copiedEdgeS = m_diGraph.newEdge(source, target);
188  edge copiedEdgeT = m_diGraph.newEdge(target, source);
189  m_edgeSlacks[copiedEdgeS] = m_edgeSlacks[copiedEdgeT] = m_pOrigGraph->weight(e);
190  m_origMapping[copiedEdgeS] = m_origMapping[copiedEdgeT] = e;
191  }
193 
194 #ifdef OGDF_DUAL_ASCENT_LOGGING
195  cout << "directed graph has " << m_diGraph.numberOfNodes() << " nodes "
196  << "and " << m_diGraph.numberOfEdges() << " edges." << std::endl
197  << "root terminal is node " << m_rootTerminal << "." << std::endl;
198 #endif
199 }
200 
201 template<typename T>
203 {
204  OGDF_ASSERT(v->graphOf() == &m_steinerGraph);
206  return m_componentMapping[v];
207 }
208 
209 
210 template<typename T>
212 {
214 }
215 
216 template<typename T>
217 bool MinSteinerTreeDualAscent<T>::isTerminal(const node v, bool rootIsTerminal) const
218 {
219  OGDF_ASSERT(v->graphOf() == &m_steinerGraph);
220 
222  return (m_rootTerminal != w || rootIsTerminal) && (*m_pIsTerminal)[w];
223 }
224 template<typename T>
226 {
227  int result = -1;
228 
229 #ifdef OGDF_DUAL_ASCENT_LOGGING
230  cout << " searching for active component.." << std::endl;
231 #endif
232 
233  HashArray<int, bool> checked(false);
234  for(auto it = m_pTerminals->begin(); it != m_pTerminals->end() && result == -1; it++) {
235  if(*it != m_rootTerminal) {
237  int candidate = findComponent(v);
238  if(!checked[candidate]) {
239  checked[candidate] = true;
240  bool isRoot = isActiveComponent(v);
241 
242  if(isRoot) {
243  result = candidate;
244  *terminal = *it;
245 #ifdef OGDF_DUAL_ASCENT_LOGGING
246  cout << " active component found: component " << result << ", terminal " << *terminal << std::endl;
247 #endif
248  }
249  }
250  }
251  }
252 
253 #ifdef OGDF_DUAL_ASCENT_LOGGING
254  if(result == -1) {
255  cout << " could not find an active component" << std::endl;
256  }
257 #endif
258 
259  return result;
260 }
261 
262 template<typename T>
264 {
265  OGDF_ASSERT(root->graphOf() == &m_steinerGraph);
266 
267  // establish "weakly connected" component of v (non-standard definition, see paper for details)
268  NodeArray<bool> visited(m_steinerGraph, false);
269  List<node> weakComp;
270  visited[root] = true;
271 
272  List<node> queue;
273  queue.pushBack(root);
274 
275  // determine all nodes connected to root
276  // meaning all nodes from which a directed path to root exists
277  while(!queue.empty()) {
278  node v = queue.popFrontRet();
279  weakComp.pushBack(v);
280  for(adjEntry adj : v->adjEntries) {
281  edge e = adj->theEdge();
282  node w = e->source();
283  if(!visited[w]) {
284  visited[w] = true;
285  queue.pushBack(w);
286  }
287  }
288  }
289 
290  // identify (incoming) cut edges
291  List<edge> *result = new List<edge>();
292 
293  for(node v : weakComp) {
295  for(adjEntry adj : w->adjEntries) {
296  edge e = adj->theEdge();
297  if(!visited[m_steinerGraph.copy(e->source())]) {
298  result->pushBack(e);
299  OGDF_ASSERT(m_steinerGraph.copy(e) == nullptr);
300  }
301  }
302  }
303  return result;
304 }
305 
306 template<typename T>
308 {
309  OGDF_ASSERT(source->graphOf() == &m_steinerGraph);
310 
311  int comp = findComponent(source);
312  bool danglingTerminalFound = false;
313  bool hasTerminal = false;
314 
315 #ifdef OGDF_DUAL_ASCENT_LOGGING
316  cout << " checking whether component of node " << source << " is active.. " << std::endl;
317  cout << " component has id " << comp << std::endl;
318 #endif
319 
320  List<node> queue;
321  NodeArray<bool> visited(m_steinerGraph, false);
322  queue.pushBack(source);
323  visited[source] = true;
324 #ifdef OGDF_DUAL_ASCENT_LOGGING
325  while(!queue.empty()) {
326 #else
327  while(!queue.empty() && !danglingTerminalFound) {
328 #endif
329  node v = queue.popFrontRet();
330  hasTerminal |= isTerminal(v, false) && findComponent(v) == comp;
331  for(adjEntry adj : v->adjEntries) {
332  edge e = adj->theEdge();
333  node w = e->source();
334  if(!visited[w]) {
335  danglingTerminalFound |= isTerminal(w, true) && findComponent(w) != comp;
336  visited[w] = true;
337  queue.pushBack(w);
338  }
339  }
340  }
341 
342 #ifdef OGDF_DUAL_ASCENT_LOGGING
343  if(hasTerminal) { cout << " component includes a terminal" << std::endl; }
344  if(danglingTerminalFound) { cout << " component has dangling terminal" << std::endl; }
345  if(hasTerminal && !danglingTerminalFound) { cout << " component is active!" << std::endl; }
346 #endif
347  return hasTerminal && !danglingTerminalFound;
348 }
349 
350 template<typename T>
352  const EdgeWeightedGraph<T> &G,
353  const List<node> &terminals,
355  EdgeWeightedGraphCopy<T> *&finalSteinerTree)
356 {
357 #ifdef OGDF_DUAL_ASCENT_LOGGING
358  cout << "MinSteinerTreeDualAscent called." << std::endl;
359  cout << "terminals are: ";
360 
361  for(node v : terminals) {
362  cout << v << " ";
363  }
364  cout << std::endl;
365 #endif
366  m_pOrigGraph = &G;
367  m_pTerminals = &terminals;
369 
370  init();
371 
372  // create resulting Steiner tree
373  finalSteinerTree = new EdgeWeightedGraphCopy<T>();
374  finalSteinerTree->createEmpty(*m_pOrigGraph);
375  T result = 0;
376 
377  int comp = -1;
378  node terminal = nullptr;
379 
380 #ifdef OGDF_DUAL_ASCENT_LOGGING
381  cout << "main loop starting.." << endl;
382 #endif
383  while((comp = findActiveComponent(&terminal)) != -1) {
384  // if active comonents exists we
385  // have one of its terminals
386  OGDF_ASSERT(terminal != nullptr);
387 
388  // find minimal cut edge
389  List<edge> *cutEdges = computeCutSet(m_steinerGraph.copy(m_diGraph.copy(terminal)));
390  edge minEdge = nullptr;
391  T minSlack = MAX_VALUE;
392 #ifdef OGDF_DUAL_ASCENT_LOGGING
393  cout << " cut edges:";
394 #endif
395  for(edge e : *cutEdges) {
396 #ifdef OGDF_DUAL_ASCENT_LOGGING
397  cout << " " << e;
398 #endif
399  if(m_edgeSlacks[e] < MAX_VALUE || minEdge == nullptr) {
400  minEdge = e;
401  minSlack = m_edgeSlacks[e];
402  }
403  }
404 #ifdef OGDF_DUAL_ASCENT_LOGGING
405  cout << std::endl << " next edge: " << minEdge << std::endl;
406 #endif
407  OGDF_ASSERT(minEdge != nullptr);
408 
409  // update slack variables
410  for(edge e : *cutEdges) {
411  m_edgeSlacks[e] -= minSlack;
412  }
413  delete cutEdges;
414 
415  // insert edge
416  m_steinerGraph.newEdge(minEdge);
418 
419  // insert edge into final Steiner tree
420  edge origEdge = m_origMapping[minEdge];
421  T cost = m_pOrigGraph->weight(origEdge);
422  result += cost;
423  if(finalSteinerTree->copy(origEdge->source()) == nullptr) { finalSteinerTree->newNode(origEdge->source()); }
424  if(finalSteinerTree->copy(origEdge->target()) == nullptr) { finalSteinerTree->newNode(origEdge->target()); }
425  if(finalSteinerTree->copy(origEdge) == nullptr) { finalSteinerTree->newEdge(origEdge, cost); }
426  }
427 
428 #ifdef OGDF_DUAL_ASCENT_LOGGING
429  cout << "removing expendable edges" << std::endl;
430 #endif
431  result -= this->pruneAllDanglingSteinerPaths(*finalSteinerTree, *m_pIsTerminal);
432  result -= this->removeCyclesFrom(*finalSteinerTree, *m_pIsTerminal);
433 
434 #ifdef OGDF_DUAL_ASCENT_LOGGING
435  cout << "algorithm terminated." << std::endl;
436 #endif
437  return result;
438 }
439 
440 }
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
const NodeArray< bool > * m_pIsTerminal
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
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.
Dual ascent approximation algorithm for Steiner tree problems.
void createEmpty(const Graph &G)
Associates the graph copy with G, but does not create any nodes or edges.
Extends the GraphCopy concept to weighted graphs.
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
List< edge > * computeCutSet(const node v) const
Returns all incoming cut edges of the component of v.
const EdgeWeightedGraph< T > * m_pOrigGraph
bool isActiveComponent(const node v) const
Determines whether a strongly connected component is active (paper says "is root component").
bool isTerminal(const node v, bool rootIsTerminal) const
Returns whether this node is a terminal.
void updateComponents()
Re-establishes all strongly connected components for the Steiner graph.
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
T computeSteinerTree(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, EdgeWeightedGraphCopy< T > *&finalSteinerTree)
Creates a minimum Steiner tree given a weighted graph and a list of terminals.
void createEmpty(const Graph &wG)
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:581
virtual void clear() override
Removes all nodes and all edges from the graph.
void init()
Intializes all relevant variables.
int findActiveComponent(node *terminal) const
Searches for the next active component.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
Class for the representation of edges.
Definition: Graph_d.h:291
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1360
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:250
Class for the representation of nodes.
Definition: Graph_d.h:167
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. ...
static T removeCyclesFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Remove remaining cycles from an Steiner "almost" tree.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
The namespace for all OGDF objects.
int strongComponents(const Graph &G, NodeArray< int > &component)
Computes the strongly connected components of the digraph G.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:578
bool empty() const
Returns true iff the list is empty.
Definition: List.h:236
Class for adjacency list elements.
Definition: Graph_d.h:78
int findComponent(const node v) const
Returns the component of node v.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:198