Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

CoreEdgeRandomSpanningTree.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 #include <ogdf/basic/NodeArray.h>
37 
38 namespace ogdf {
39 namespace steiner_tree {
40 namespace goemans {
41 
43 template<typename T>
45  : public CoreEdgeModule<T>
46 {
47  std::minstd_rand &m_rng;
48 
49 public:
50  CoreEdgeRandomSpanningTree(std::minstd_rand &rng)
51  : m_rng(rng)
52  {
53  }
54 
55  void call(const Graph &graph, const List<node> &terminals, EdgeArray<bool> &isInTree) const override
56  {
57  // Let's do Kruskal's algorithm without weights but on a randomly permuted edge list.
58  // We virtually contract all terminals in the union-find data structure.
59  NodeArray<int> setID(graph, -1);
60  isInTree.init(graph, false);
61  DisjointSets<> uf(graph.numberOfNodes() - terminals.size() + 1);
62 
63  int contractedID = uf.makeSet();
64  OGDF_ASSERT(contractedID >= 0);
65  for (node t : terminals) {
66  setID[t] = contractedID;
67  }
68  for (node v : graph.nodes) {
69  if (setID[v] < 0) {
70  setID[v] = uf.makeSet();
71  }
72  }
73 
74  // obtain a random edge permutation
75  ArrayBuffer<edge> edgePermutation;
76  for (edge e : graph.edges) {
77  edgePermutation.push(e);
78  }
79  edgePermutation.permute(m_rng);
80 
81  // add edges if we do not close a cycle
82  for (edge e : edgePermutation) {
83  const int v = setID[e->source()];
84  const int w = setID[e->target()];
85  if (uf.find(v) != uf.find(w)) {
86  isInTree[e] = true;
87  uf.link(uf.find(v), uf.find(w));
88  }
89  }
90  }
91 };
92 
93 }
94 }
95 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
void permute(INDEX l, INDEX r, RNG &rng)
Randomly permutes the subarray with index set [l..r] using random number generator rng...
Definition: ArrayBuffer.h:359
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:283
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
Definition of ogdf::steiner_tree::goemans::CoreEdgeModule class template.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
A Union/Find data structure for maintaining disjoint sets.
Definition: DisjointSets.h:81
Class for the representation of edges.
Definition: Graph_d.h:292
Class for the representation of nodes.
Definition: Graph_d.h:169
Declaration and implementation of NodeArray class.
void call(const Graph &graph, const List< node > &terminals, EdgeArray< bool > &isInTree) const override
Compute a set of core edges.
int makeSet()
Initializes a singleton set.
Definition: DisjointSets.h:176
int size() const
Returns the number of elements in the list.
Definition: List.h:1312
Interface for core edge finder algorithms.
Implementation of disjoint sets data structures (union-find functionality).
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
The namespace for all OGDF objects.
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
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41