Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

CoreEdgeRandomSpanningTree.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/ArrayBuffer.h>
36 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/GraphList.h>
38 #include <ogdf/basic/List.h>
39 #include <ogdf/basic/basic.h>
41 
42 #include <random>
43 
44 namespace ogdf {
45 namespace steiner_tree {
46 namespace goemans {
47 
49 template<typename T>
51  std::minstd_rand& m_rng;
52 
53 public:
54  CoreEdgeRandomSpanningTree(std::minstd_rand& rng) : m_rng(rng) { }
55 
56  void call(const Graph& graph, const List<node>& terminals,
57  EdgeArray<bool>& isInTree) const override {
58  // Let's do Kruskal's algorithm without weights but on a randomly permuted edge list.
59  // We virtually contract all terminals in the union-find data structure.
60  NodeArray<int> setID(graph, -1);
61  isInTree.init(graph, false);
62  DisjointSets<> uf(graph.numberOfNodes() - terminals.size() + 1);
63 
64  int contractedID = uf.makeSet();
65  OGDF_ASSERT(contractedID >= 0);
66  for (node t : terminals) {
67  setID[t] = contractedID;
68  }
69  for (node v : graph.nodes) {
70  if (setID[v] < 0) {
71  setID[v] = uf.makeSet();
72  }
73  }
74 
75  // obtain a random edge permutation
76  ArrayBuffer<edge> edgePermutation;
77  for (edge e : graph.edges) {
78  edgePermutation.push(e);
79  }
80  edgePermutation.permute(m_rng);
81 
82  // add edges if we do not close a cycle
83  for (edge e : edgePermutation) {
84  const int v = setID[e->source()];
85  const int w = setID[e->target()];
86  if (uf.find(v) != uf.find(w)) {
87  isInTree[e] = true;
88  uf.link(uf.find(v), uf.find(w));
89  }
90  }
91  }
92 };
93 
94 }
95 }
96 }
ogdf::ArrayBuffer< edge >
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ArrayBuffer.h
Declaration and implementation of ArrayBuffer class.
Graph.h
Includes declaration of graph class.
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::DisjointSets::find
int find(disjoint_sets::CompressionOption< CompressionOptions::PathCompression >, int set)
Definition: DisjointSets.h:332
ogdf::steiner_tree::goemans::CoreEdgeRandomSpanningTree::m_rng
std::minstd_rand & m_rng
Definition: CoreEdgeRandomSpanningTree.h:51
ogdf::DisjointSets::makeSet
int makeSet()
Initializes a singleton set.
Definition: DisjointSets.h:235
ogdf::DisjointSets::link
int link(disjoint_sets::LinkOption< LinkOptions::Naive >, int set1, int set2)
Definition: DisjointSets.h:624
ogdf::Graph::nodes
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:932
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:982
ogdf::DisjointSets
A Union/Find data structure for maintaining disjoint sets.
Definition: DisjointSets.h:90
GraphList.h
Decralation of GraphElement and GraphList classes.
CoreEdgeModule.h
Definition of ogdf::steiner_tree::goemans::CoreEdgeModule class template.
ogdf::ArrayBuffer::push
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:217
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::ArrayBuffer::permute
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:475
ogdf::Graph::edges
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:935
basic.h
Basic declarations, included by all source files.
ogdf::steiner_tree::goemans::CoreEdgeModule
Interface for core edge finder algorithms.
Definition: BlowupGraph.h:48
DisjointSets.h
Implementation of disjoint sets data structures (union-find functionality).
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
List.h
Declaration of doubly linked lists and iterators.
ogdf::steiner_tree::goemans::CoreEdgeRandomSpanningTree
Computes a random set of core edges.
Definition: CoreEdgeRandomSpanningTree.h:50
ogdf::RegisteredArray< GraphRegistry< EdgeElement >, Value, WithDefault, Graph >::init
void init(const Graph *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
Definition: RegisteredArray.h:858
ogdf::List::size
int size() const
Returns the number of elements in the list.
Definition: List.h:1488
ogdf::steiner_tree::goemans::CoreEdgeRandomSpanningTree::CoreEdgeRandomSpanningTree
CoreEdgeRandomSpanningTree(std::minstd_rand &rng)
Definition: CoreEdgeRandomSpanningTree.h:54
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
ogdf::steiner_tree::goemans::CoreEdgeRandomSpanningTree::call
void call(const Graph &graph, const List< node > &terminals, EdgeArray< bool > &isInTree) const override
Compute a set of core edges.
Definition: CoreEdgeRandomSpanningTree.h:56
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716