-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathdisjoint_sets.cpp
More file actions
89 lines (71 loc) · 1.89 KB
/
disjoint_sets.cpp
File metadata and controls
89 lines (71 loc) · 1.89 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/disjoint_sets.h"
using std::vector;
namespace polyscope {
// Constructor
DisjointSets::DisjointSets(size_t n_) : n(n_), parent(n + 1), rank(n + 1) {
// Initialize all elements to be in different sets and to have rank 0
for (size_t i = 0; i <= n; i++) {
rank[i] = 0;
parent[i] = i;
}
}
// Find parent of element x
size_t DisjointSets::find(size_t x) {
if (x != parent[x]) parent[x] = find(parent[x]);
return parent[x];
}
// Union by rank
void DisjointSets::merge(size_t x, size_t y) {
x = find(x);
y = find(y);
// Smaller tree becomes a subtree of the larger tree
if (rank[x] > rank[y])
parent[y] = x;
else
parent[x] = y;
if (rank[x] == rank[y]) rank[y]++;
}
// Constructor
MarkedDisjointSets::MarkedDisjointSets(size_t n_) : n(n_), parent(n + 1), rank(n + 1), marked(n + 1) {
// Initialize all elements to be in different sets and to have rank 0
for (size_t i = 0; i <= n; i++) {
rank[i] = 0;
parent[i] = i;
marked[i] = false;
}
}
void MarkedDisjointSets::mark(size_t x) {
size_t p = find(x);
marked[p] = true;
}
void MarkedDisjointSets::unmark(size_t x) {
size_t p = find(x);
marked[p] = false;
}
bool MarkedDisjointSets::isMarked(size_t x) {
size_t p = find(x);
return marked[p];
}
// Find parent of element x
size_t MarkedDisjointSets::find(size_t x) {
if (x != parent[x]) parent[x] = find(parent[x]);
return parent[x];
}
// Union by rank
void MarkedDisjointSets::merge(size_t x, size_t y) {
x = find(x);
y = find(y);
// Smaller tree becomes a subtree of the larger tree
if (rank[x] > rank[y])
parent[y] = x;
else
parent[x] = y;
if (rank[x] == rank[y]) rank[y]++;
// If either was marked, both are marked
if (marked[x] || marked[y]) {
marked[x] = true;
marked[y] = true;
}
}
} // namespace polyscope