Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
CoreEdgeRandomSpanningTree.h
Go to the documentation of this file.
1
32#pragma once
33
36#include <ogdf/basic/Graph.h>
38#include <ogdf/basic/List.h>
39#include <ogdf/basic/basic.h>
41
42#include <random>
43
44namespace ogdf {
45namespace steiner_tree {
46namespace goemans {
47
49template<typename T>
51 std::minstd_rand& m_rng;
52
53public:
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}
Declaration and implementation of ArrayBuffer class.
Definition of ogdf::steiner_tree::goemans::CoreEdgeModule class template.
Implementation of disjoint sets data structures (union-find functionality).
Includes declaration of graph class.
Decralation of GraphElement and GraphList classes.
Declaration of doubly linked lists and iterators.
Basic declarations, included by all source files.
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition ArrayBuffer.h:64
void push(E e)
Puts a new element in the buffer.
void permute(INDEX l, INDEX r, RNG &rng)
Randomly permutes the subarray with index set [l..r] using random number generator rng.
A Union/Find data structure for maintaining disjoint sets.
int makeSet()
Initializes a singleton set.
int find(disjoint_sets::CompressionOption< CompressionOptions::PathCompression >, int set)
int link(disjoint_sets::LinkOption< LinkOptions::Naive >, int set1, int set2)
Class for the representation of edges.
Definition Graph_d.h:364
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition Graph_d.h:979
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition Graph_d.h:929
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition Graph_d.h:932
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
int size() const
Returns the number of elements in the list.
Definition List.h:1488
Class for the representation of nodes.
Definition Graph_d.h:241
void init(const Base *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
Interface for core edge finder algorithms.
void call(const Graph &graph, const List< node > &terminals, EdgeArray< bool > &isInTree) const override
Compute a set of core edges.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.