Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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 
60 
64 
67 
69 
70  // components for the Steiner graph
72 
77  void init();
78 
87  bool isTerminal(const node v, bool rootIsTerminal) const;
88 
95  int findActiveComponent(node *terminal) const;
96 
103  int findComponent(const node v) const;
104 
111  List<edge> *computeCutSet(const node v) const;
112 
122  bool isActiveComponent(const node v) const;
123 
128  void updateComponents();
129 
130 protected:
131 
148  const EdgeWeightedGraph<T> &G,
149  const List<node> &terminals,
151  EdgeWeightedGraphCopy<T> *&finalSteinerTree);
152 };
153 
154 template<typename T>
156 {
157  // local auxilary lists
158  List<node> nodes;
159  m_pOrigGraph->allNodes(nodes);
160  List<edge> edges;
161  m_pOrigGraph->allEdges(edges);
162 
163  // create directed graph
164  // and initialize slack variables
166  m_diGraph.clear();
167  m_edgeSlacks.init(m_diGraph);
168  m_origMapping.init(m_diGraph);
169 
170  // create resulting Steiner tree
174 
175  // TODO: Better choices possible?
176  m_rootTerminal = m_pTerminals->chooseElement();
177 
178  for(node v : nodes) {
179  node w = m_diGraph.newNode(v);
181  }
182 
183  for(edge e : edges) {
184  node source = m_diGraph.copy(e->source());
185  node target = m_diGraph.copy(e->target());
186  edge copiedEdgeS = m_diGraph.newEdge(source, target);
187  edge copiedEdgeT = m_diGraph.newEdge(target, source);
188  m_edgeSlacks[copiedEdgeS] = m_edgeSlacks[copiedEdgeT] = m_pOrigGraph->weight(e);
189  m_origMapping[copiedEdgeS] = m_origMapping[copiedEdgeT] = e;
190  }
192 
193 #ifdef OGDF_DUAL_ASCENT_LOGGING
194  std::cout << "directed graph has " << m_diGraph.numberOfNodes() << " nodes "
195  << "and " << m_diGraph.numberOfEdges() << " edges." << std::endl
196  << "root terminal is node " << m_rootTerminal << "." << std::endl;
197 #endif
198 }
199 
200 template<typename T>
202 {
203  OGDF_ASSERT(v->graphOf() == &m_steinerGraph);
205  return m_componentMapping[v];
206 }
207 
208 template<typename T>
210 {
212 }
213 
214 template<typename T>
215 bool MinSteinerTreeDualAscent<T>::isTerminal(const node v, bool rootIsTerminal) const
216 {
217  OGDF_ASSERT(v->graphOf() == &m_steinerGraph);
218 
220  return (m_rootTerminal != w || rootIsTerminal) && (*m_pIsTerminal)[w];
221 }
222 
223 template<typename T>
225 {
226  int result = -1;
227 
228 #ifdef OGDF_DUAL_ASCENT_LOGGING
229  std::cout << " searching for active component.." << std::endl;
230 #endif
231 
232  HashArray<int, bool> checked(false);
233  for(auto it = m_pTerminals->begin(); it != m_pTerminals->end() && result == -1; it++) {
234  if(*it != m_rootTerminal) {
236  int candidate = findComponent(v);
237  if(!checked[candidate]) {
238  checked[candidate] = true;
239  bool isRoot = isActiveComponent(v);
240 
241  if(isRoot) {
242  result = candidate;
243  *terminal = *it;
244 #ifdef OGDF_DUAL_ASCENT_LOGGING
245  std::cout << " active component found: component " << result << ", terminal " << *terminal << std::endl;
246 #endif
247  }
248  }
249  }
250  }
251 
252 #ifdef OGDF_DUAL_ASCENT_LOGGING
253  if(result == -1) {
254  std::cout << " could not find an active component" << std::endl;
255  }
256 #endif
257 
258  return result;
259 }
260 
261 template<typename T>
263 {
264  OGDF_ASSERT(root->graphOf() == &m_steinerGraph);
265 
266  // establish "weakly connected" component of v (non-standard definition, see paper for details)
267  NodeArray<bool> visited(m_steinerGraph, false);
268  List<node> weakComp;
269  visited[root] = true;
270 
271  List<node> queue;
272  queue.pushBack(root);
273 
274  // determine all nodes connected to root
275  // meaning all nodes from which a directed path to root exists
276  while(!queue.empty()) {
277  node v = queue.popFrontRet();
278  weakComp.pushBack(v);
279  for(adjEntry adj : v->adjEntries) {
280  edge e = adj->theEdge();
281  node w = e->source();
282  if(!visited[w]) {
283  visited[w] = true;
284  queue.pushBack(w);
285  }
286  }
287  }
288 
289  // identify (incoming) cut edges
290  List<edge> *result = new List<edge>();
291 
292  for(node v : weakComp) {
294  for(adjEntry adj : w->adjEntries) {
295  edge e = adj->theEdge();
296  if(!visited[m_steinerGraph.copy(e->source())]) {
297  result->pushBack(e);
298  OGDF_ASSERT(m_steinerGraph.copy(e) == nullptr);
299  }
300  }
301  }
302  return result;
303 }
304 
305 template<typename T>
307 {
308  OGDF_ASSERT(source->graphOf() == &m_steinerGraph);
309 
310  int comp = findComponent(source);
311  bool danglingTerminalFound = false;
312  bool hasTerminal = false;
313 
314 #ifdef OGDF_DUAL_ASCENT_LOGGING
315  std::cout << " checking whether component of node " << source << " is active.. " << std::endl;
316  std::cout << " component has id " << comp << std::endl;
317 #endif
318 
319  List<node> queue;
320  NodeArray<bool> visited(m_steinerGraph, false);
321  queue.pushBack(source);
322  visited[source] = true;
323 #ifdef OGDF_DUAL_ASCENT_LOGGING
324  while(!queue.empty()) {
325 #else
326  while(!queue.empty() && !danglingTerminalFound) {
327 #endif
328  node v = queue.popFrontRet();
329  hasTerminal |= isTerminal(v, false) && findComponent(v) == comp;
330  for(adjEntry adj : v->adjEntries) {
331  edge e = adj->theEdge();
332  node w = e->source();
333  if(!visited[w]) {
334  danglingTerminalFound |= isTerminal(w, true) && findComponent(w) != comp;
335  visited[w] = true;
336  queue.pushBack(w);
337  }
338  }
339  }
340 
341 #ifdef OGDF_DUAL_ASCENT_LOGGING
342  if(hasTerminal) { std::cout << " component includes a terminal" << std::endl; }
343  if(danglingTerminalFound) { std::cout << " component has dangling terminal" << std::endl; }
344  if(hasTerminal && !danglingTerminalFound) { std::cout << " component is active!" << std::endl; }
345 #endif
346  return hasTerminal && !danglingTerminalFound;
347 }
348 
349 template<typename T>
351  const EdgeWeightedGraph<T> &G,
352  const List<node> &terminals,
354  EdgeWeightedGraphCopy<T> *&finalSteinerTree)
355 {
356 #ifdef OGDF_DUAL_ASCENT_LOGGING
357  std::cout << "MinSteinerTreeDualAscent called." << std::endl;
358  std::cout << "terminals are: ";
359 
360  for(node v : terminals) {
361  std::cout << v << " ";
362  }
363  std::cout << std::endl;
364 #endif
365  m_pOrigGraph = &G;
366  m_pTerminals = &terminals;
368 
369  init();
370 
371  // create resulting Steiner tree
372  finalSteinerTree = new EdgeWeightedGraphCopy<T>();
373  finalSteinerTree->createEmpty(*m_pOrigGraph);
374  T result = 0;
375 
376  int comp = -1;
377  node terminal = nullptr;
378 
379 #ifdef OGDF_DUAL_ASCENT_LOGGING
380  std::cout << "main loop starting.." << std::endl;
381 #endif
382  while((comp = findActiveComponent(&terminal)) != -1) {
383  // if active comonents exists we
384  // have one of its terminals
385  OGDF_ASSERT(terminal != nullptr);
386 
387  // find minimal cut edge
388  List<edge> *cutEdges = computeCutSet(m_steinerGraph.copy(m_diGraph.copy(terminal)));
389  edge minEdge = nullptr;
390  T minSlack = MAX_VALUE;
391 #ifdef OGDF_DUAL_ASCENT_LOGGING
392  std::cout << " cut edges:";
393 #endif
394  for(edge e : *cutEdges) {
395 #ifdef OGDF_DUAL_ASCENT_LOGGING
396  std::cout << " " << e;
397 #endif
398  if(m_edgeSlacks[e] < MAX_VALUE || minEdge == nullptr) {
399  minEdge = e;
400  minSlack = m_edgeSlacks[e];
401  }
402  }
403 #ifdef OGDF_DUAL_ASCENT_LOGGING
404  std::cout << std::endl << " next edge: " << minEdge << std::endl;
405 #endif
406  OGDF_ASSERT(minEdge != nullptr);
407 
408  // update slack variables
409  for(edge e : *cutEdges) {
410  m_edgeSlacks[e] -= minSlack;
411  }
412  delete cutEdges;
413 
414  // insert edge
415  m_steinerGraph.newEdge(minEdge);
417 
418  // insert edge into final Steiner tree
419  edge origEdge = m_origMapping[minEdge];
420  T cost = m_pOrigGraph->weight(origEdge);
421  if (finalSteinerTree->copy(origEdge->source()) == nullptr) {
422  finalSteinerTree->newNode(origEdge->source());
423  }
424  if (finalSteinerTree->copy(origEdge->target()) == nullptr) {
425  finalSteinerTree->newNode(origEdge->target());
426  }
427  if (finalSteinerTree->copy(origEdge) == nullptr) {
428  finalSteinerTree->newEdge(origEdge, cost);
429  result += cost;
430  }
431  }
432 
433 #ifdef OGDF_DUAL_ASCENT_LOGGING
434  std::cout << "removing expendable edges" << std::endl;
435 #endif
436  result -= this->pruneAllDanglingSteinerPaths(*finalSteinerTree, *m_pIsTerminal);
437  result -= this->removeCyclesFrom(*finalSteinerTree, *m_pIsTerminal);
438 
439 #ifdef OGDF_DUAL_ASCENT_LOGGING
440  std::cout << "algorithm terminated." << std::endl;
441 #endif
442  return result;
443 }
444 
445 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
node copy(node v) const
Returns the node in the graph copy corresponding to v.
Definition: GraphCopy.h:335
EdgeArray< T > m_edgeSlacks
slack variables for each directed edge representing the adjusted weight
const List< node > * m_pTerminals
list of terminals passed to the module
const NodeArray< bool > * m_pIsTerminal
terminal incidence vector passed to the module
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
Definition: GraphCopy.h:412
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
NodeArray< int > m_componentMapping
maps each node to its component
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
Serves as an interface for various methods to compute or approximate minimum Steiner trees on undirec...
Indexed arrays using hashing for element access.
Definition: HashArray.h:94
edge newEdge(node u, node v, T weight)
Declaration of ogdf::MinSteinerTreeModule.
Dual ascent heuristic for the minimum Steiner tree problem.
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
original graph passed to the module
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:601
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:40
Class for the representation of edges.
Definition: Graph_d.h:292
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1417
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:249
Class for the representation of nodes.
Definition: Graph_d.h:169
GraphCopy m_steinerGraph
the to-be constructed "almost" Steiner tree
EdgeArray< edge > m_origMapping
maps each directed edge to its undirected original
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.
static T removeCyclesFrom(EdgeWeightedGraphCopy< T > &steinerTree, const NodeArray< bool > &isTerminal)
Remove remaining cycles from a Steiner "almost" tree.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
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:598
bool empty() const
Returns true iff the list is empty.
Definition: List.h:256
Class for adjacency list elements.
Definition: Graph_d.h:79
int findComponent(const node v) const
Returns the component of node v.
EdgeArray< bool > m_edgeInclusions
stores the resulting Steiner tree
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
GraphCopy m_diGraph
the directed graph
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200