-
Notifications
You must be signed in to change notification settings - Fork 14
/
union_find.cpp
82 lines (63 loc) · 2.01 KB
/
union_find.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
using namespace std;
class union_find
{
public:
union_find(int N)
{
num_sets = N; set_size.assign(N,1);
rank.assign(N,0); p.assign(N,0);
for (int i=0; i < N; i++) {p[i] = i;}
}
int find_set(int i) {return (p[i] == i) ? i : (p[i] = find_set(p[i]));}
bool is_same_set(int i, int j) {return find_set(i) == find_set(j);}
void union_set(int i, int j)
{
if (!is_same_set(i, j))
{
num_sets--;
int x = find_set(i);
int y = find_set(j);
//rank heuristic is used to keep the tree height small
if (rank[x] > rank[y])
{
p[y] = x; set_size[x] += set_size[y];
}
else
{
p[x] = y; set_size[y] += set_size[x];
if (rank[x] == rank[y]) {rank[y]++;}
}
}
}
int num_disjoint_sets() {return num_sets;}
int size_of_set(int i) {return set_size[find_set(i)];}
private:
int num_sets;
vector<int> p, rank, set_size;
};
int main()
{
union_find UF(5);
cout << "number of disjoint sets: " << UF.num_disjoint_sets() << endl;
UF.union_set(0,1);
cout << "number of disjoint sets: " << UF.num_disjoint_sets() << endl;
UF.union_set(2,3);
cout << "number of disjoint sets: " << UF.num_disjoint_sets() << endl;
UF.union_set(4,3);
cout << "number of disjoint sets: " << UF.num_disjoint_sets() << endl;
cout << "is same set (0, 3): " << UF.is_same_set(0, 3) << endl;
cout << "is same set (4, 3): " << UF.is_same_set(4, 3) << endl;
for (int i=0; i < 5; i++)
{
printf("find_set(%d) = %d, size_of_set(%d) = %d\n", i, UF.find_set(i), i, UF.size_of_set(i));
}
UF.union_set(0, 3);
cout << "number of disjoint sets: " << UF.num_disjoint_sets() << endl;
for (int i=0; i < 5; i++)
{
printf("find_set(%d) = %d, size_of_set(%d) = %d\n", i, UF.find_set(i), i, UF.size_of_set(i));
}
return 0;
}