-
Notifications
You must be signed in to change notification settings - Fork 0
/
disjoint_set.cpp
52 lines (46 loc) · 1.41 KB
/
disjoint_set.cpp
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
#include "disjoint_set.hpp"
// x is essentially the same as forest_[x].value
// recursive parent search
int disjoint_set::find_set(int x) {
if (forest_.find(x) != forest_.end()) {
if (forest_[x].parent == forest_[x].value) {
return x;
} else {
return find_set(forest_[x].parent);
}
} else {
return -1;
}
}
bool disjoint_set::set_union(int val1, int val2) {
int parent1 = find_set(val1);
int parent2 = find_set(val2);
if (parent1 == parent2) {
return true; //already in the same set (can be thought of as condition for cycle in graph)
}
if (forest_[parent1].rank > forest_[parent2].rank) {
forest_[parent2].parent = parent1;
} else if (forest_[parent1].rank < forest_[parent2].rank) {
forest_[parent1].parent = parent2;
} else {
forest_[parent2].parent = parent1;
forest_[parent1].rank += 1;
}
return false;
}
void disjoint_set::initialize_set(std::unordered_set<int> &ele_set) {
for (auto i : ele_set) {
auto p = std::make_pair(i, Element(i));
forest_.insert(p);
}
count_ = forest_.size();
}
// int main(int argc, char **argv) {
// std::unordered_set<int> test_set{0, 1, 2, 3, 4};
// disjoint_set D = disjoint_set(test_set);
// std::cout << D.set_union(0, 2) << "\n"; // 0
// std::cout << D.set_union(4, 2) << "\n"; // 0
// std::cout << D.set_union(0, 4) << "\n"; // 1
// std::cout << D.set_union(1, 3) << "\n"; // 0
// return 0;
// }