This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisjointSet.h
77 lines (63 loc) · 2.55 KB
/
DisjointSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef DISJOINT_SET_H
#define DISJOINT_SET_H
#include <vector> // std::vector
#include "DisjointSetBase.h"
namespace disjoint_set {
/**
* Simple union-by-size implementation of a Disjoint-Set.
* DisjointSet accepts element of type T, which must be an unsigned integer type.
*/
template <typename T>
class DisjointSet : public DisjointSetBase<T> {
using super = DisjointSetBase<T>;
using parameter_t = typename super::parameter_t;
// vector that keeps track of the size of every element in the DisjointSet
std::vector<std::size_t> sizes;
public:
// x_list must contain unsigned integers of distinct value in the range [0, x_list.size())
explicit DisjointSet(const std::vector<T>& x_list) :
super(x_list), sizes(x_list.size(), 1) {
} // fill sizes with value 1
// x_list must contain unsigned integers of distinct value in the range [0, x_list.size())
explicit DisjointSet(std::vector<T>&& x_list) :
super(std::move(x_list)), sizes(x_list.size(), 1) {
} // fill sizes with value 1
~DisjointSet() = default;
/**
* Returns the index of the representative of the unique set containing the given item.
* T must be cast-able to size_t.
* O(logN)
*/
[[nodiscard]] std::size_t find(parameter_t element) noexcept override {
auto x = element;
while (x != this->parents[x]) {
// move towards the parent
x = this->parents[x];
}
return x;
}
/**
* Unites the dynamic sets that contain x and y into a new set that is the union
* of these two sets.
* x should be different than y.
* Uses union-by-size policy: it makes the node whose set size is smaller point to
* the node whose set size is bigger, breaking ties arbitrarily.
* The complexity of this method is the one of find + O(1)
*/
void unite(parameter_t x, parameter_t y) noexcept override {
const auto i = find(x);
const auto j = find(y);
// if the two nodes are the same set, do nothing
if (i != j) {
if (sizes[i] < sizes[j]) {
this->parents[i] = j;
sizes[j] += sizes[i];
} else {
this->parents[j] = i;
sizes[i] += sizes[j];
}
}
}
};
} // namespace disjoint_set
#endif // DISJOINT_SET_H