-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjointSet.cpp
More file actions
202 lines (172 loc) · 4.26 KB
/
Copy pathDisjointSet.cpp
File metadata and controls
202 lines (172 loc) · 4.26 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <vector>
using namespace std;
/*
Disjoint Set (Union Find)
The main idea of a “disjoint set” is to have all connected vertices have the same parent node or root node, whether directly or indirectly connected.
*/
// Quick find based Union Find
class QuickFindBasedUnionFind{
private:
vector<int> root;
public:
// O(n)
QuickFindBasedUnionFind(int size) : root(size) {
for(int i=0; i<size; ++i){
root[i]=i;
}
}
// O(1)
int find(int x){
return root[x];
}
// O(n)
void unionSet(int x, int y){
// check
int rootX = find(x);
int rootY = find(y);
// merge children of rootY --> rootX
if(rootX!=rootY){
for(int i=0; i<root.size(); ++i){
if(root[i]==rootY){
root[i]=rootX;
}
}
}
}
// O(1)
bool isConnected(int x, int y){
return find(x) == find(y);
}
};
// Quick Union based Union Find
class QuickUnionBasedUnionFind{
private:
vector<int> parent;
public:
QuickUnionBasedUnionFind(int size) : parent(size){
for(int i=0; i<size; ++i){
parent[i]=i;
}
}
// O(n)
int find(int x){
while(x!=find(x)){
x=find(x);
}
return x;
}
// O(n)
void unionSet(int x, int y){
int rootX = find(x);
int rootY = find(y);
if(rootX != rootY){
parent[rootY]=rootX;
}
}
// O(n)
bool isConnected(int x, int y){
return find(x)==find(y);
}
};
// Quick Union --(optimized)--> union by rank
class UnionByRankBasedUnionFind{
private:
vector<int> rank;
vector<int> parent;
public:
UnionByRankBasedUnionFind(int size) : rank(size), parent(size){
for(int i=0; i<size; ++i){
parent[i]=i;
rank[i]=1;
}
}
// O(logn). in the worst-case scenario, when we repeatedly union components of equal rank, the tree height will be at most logn +1.
int find(int x){
while(x!=parent[x]){
x=parent[x];
}
return x;
}
// O(logn)
void unionSet(int x, int y){
int rootX = find(x);
int rootY = find(y);
if(rootX != rootY){
if(rank[rootX] > rank[rootY]){
parent[rootY]=rootX;
}
else if(rank[rootX] < rank[rootY]){
parent[rootX] = rootY;
}
else{
parent[rootY]=rootX;
rank[rootX]++;
}
}
}
// O(logn)
bool isConnected(int x, int y){
return find(x)==find(y);
}
};
// union by rank --(optimized)--> Path Compression
class PathCompressedUnionFind{
private:
vector<int> rank;
vector<int> root;
public:
PathCompressedUnionFind(int size) : rank(size), root(size){
for(int i=0; i<size; ++i){
root[i]=i;
rank[i]=1;
}
}
// O(n). but, it will take less if find function is called repeatly.
int find(int x){
if(x!=root[x]){
return root[x]=find(root[x]);
}
return x;
}
// O(logn)
void unionSet(int x, int y){
int rootX = find(x);
int rootY = find(y);
if(rootX != rootY){
if(rank[rootX] > rank[rootY]){
root[rootY]=rootX;
}
else if(rank[rootX] < rank[rootY]){
root[rootX] = rootY;
}
else{
root[rootY]=rootX;
rank[rootX]++;
}
}
}
// O(logn)
bool isConnected(int x, int y){
return find(x)==find(y);
}
};
int main(){
// for displaying booleans as literal strings, instead of 0 and 1
cout << boolalpha;
PathCompressedUnionFind uf(10);
// 1-2-5-6-7 3-8-9 4
uf.unionSet(1, 2);
uf.unionSet(2, 5);
uf.unionSet(5, 6);
uf.unionSet(6, 7);
uf.unionSet(3, 8);
uf.unionSet(8, 9);
cout << uf.isConnected(1, 5) << endl; // true
cout << uf.isConnected(5, 7) << endl; // true
cout << uf.isConnected(4, 9) << endl; // false
// 1-2-5-6-7 3-8-9-4
uf.unionSet(9, 4);
cout << uf.isConnected(4, 9) << endl; // true
return 0;
}