-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisjoint.cpp
104 lines (82 loc) · 2.18 KB
/
disjoint.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<iostream>
#include<vector>
using namespace std;
class Disjoint{
public:
vector<int> rank,parent,size;
Disjoint(int n){
rank.resize(n+1,0);
parent.resize(n+1,0);
size.resize(n+1,1);
for(int i=0;i<=n;i++){
parent[i] = i;
}
}
int findUparent(int node){
if(node == parent[node])
return node;
return parent[node] = findUparent(parent[node]);
}
void unionByRank(int u,int v){
int uP_u = findUparent(u);
int uP_v = findUparent(v);
// Both nodes Belonging to same component
if(uP_u == uP_v) return;
if(rank[uP_u] > rank[uP_v]){
parent[uP_v] = uP_u;
}
else if(rank[uP_v] > rank[uP_u]){
parent[uP_u] = uP_v;
}
else{
parent[uP_v] = uP_u;
rank[uP_u]++;
}
}
void unionBySize(int u,int v){
int uP_u = findUparent(u);
int uP_v = findUparent(v);
// Both nodes Belonging to same component
if(uP_u == uP_v) return;
if(size[uP_u] > size[uP_v]){
parent[uP_v] = uP_u;
size[uP_u] += size[uP_v];
}
else if(size[uP_v] > size[uP_u]){
parent[uP_u] = uP_v;
size[uP_v] += size[uP_u];
}
else{
parent[uP_v] = uP_u;
size[uP_u] += size[uP_v];
}
}
};
int main(){
int n = 7;
Disjoint ds(n);
ds.unionBySize(1,2);
ds.unionBySize(2,3);
ds.unionBySize(4,5);
ds.unionBySize(6,7);
ds.unionBySize(5,6);
if(ds.findUparent(3) == ds.findUparent(7)){
cout<<"Belong to Same component\n";
}
else
cout<<"Not belong to the Same Componenet\n";
ds.unionBySize(3,7);
if(ds.findUparent(3) == ds.findUparent(7)){
cout<<"Belong to Same component\n";
}
else
cout<<"Not belong to the Same Componenet\n";
cout<<"Size : \n";
for(auto x : ds.size){
cout<<x<<" ";
}
cout<<"\nParent : \n";
for(auto x : ds.parent){
cout<<x<<" ";
}
}