-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisjoint_sets.cpp
67 lines (57 loc) · 1.25 KB
/
disjoint_sets.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
#include <bits/stdc++.h>
using namespace std;
void makeSet(int parent[], int rank[], int n){
for(int i = 0; i < n; i++){
parent[i] = i;
rank[i] = 0;
}
}
//path compressed find
int find(int i, int parent[]){
//if the ith node is its parent
if(parent[i] == i){
return i;
}
return parent[i] = find(parent[i], parent);
}
//union by rank
void unionByRank(int u, int v, int parent[], int rank[]){
u = find(u, parent); //find the set of u
v = find(v, parent); //find the set of v
if(rank[u] < rank[v]){
parent[u] = v;
}
else if(rank[u] > rank[v]){
parent[v] = u;
}
else{
parent[v] = u; //if same ranked sets are union then
rank[u]++; //increase the rank of parent set by 1
}
}
int main(){
int n = 10;
int parent[n];
int rank[n];
makeSet(parent, rank, n);
unionByRank(1, 2, parent, rank);
unionByRank(3, 4, parent, rank);
unionByRank(2, 4, parent, rank);
unionByRank(5, 6, parent, rank);
unionByRank(7, 8, parent, rank);
unionByRank(5, 8, parent, rank);
unionByRank(3, 8, parent, rank);
//node
for(int i = 0; i < n; i++){
cout << i << " ";
}cout << "\n";
//parent
for(int i = 0; i < n; i++){
cout << parent[i] << " ";
}cout << "\n";
//rank
for(int i = 0; i < n; i++){
cout << rank[i] << " ";
}
return 0;
}