-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighted_union_find.cpp
More file actions
54 lines (50 loc) · 1.08 KB
/
weighted_union_find.cpp
File metadata and controls
54 lines (50 loc) · 1.08 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
#include <iostream>
#include <vector>
using namespace std;
int getRoot(int * A, int i) {
while(A[i] != i) {
i = A[i];
}
return i;
}
void WeightedUnion(int a, int b, int * A, int * S) {
int rootA = getRoot(A, a);
int rootB = getRoot(A, b);
if(S[rootA] > S[rootB]) {
A[rootB] = rootA;
S[rootA] += S[rootB];
} else {
A[rootA] = rootB;
S[rootB] += S[rootA];
}
}
bool find(int a, int b, int * A) {
if(getRoot(A, a) == getRoot(A, b)) {
cout << a << " and " << b << " are connected" << endl;
} else {
cout << a << " and " << b << " not connected" << endl;
}
}
int main() {
int A[10];
int S[10];
for(int i = 0; i < 10; i++) {
A[i] = i;
S[i] = 1;
}
WeightedUnion(0, 1, A, S);
WeightedUnion(1, 9, A, S);
WeightedUnion(2, 3, A, S);
WeightedUnion(5, 4, A, S);
WeightedUnion(6, 4, A, S);
WeightedUnion(3, 6, A, S);
WeightedUnion(7, 8, A, S);
WeightedUnion(8, 9, A, S);
WeightedUnion(9, 3, A, S);
find(1, 3, A);
find(5, 0, A);
find(2, 8, A);
for(int i = 0; i < 10; i++) {cout << S[i] << " ";}
cout << endl;
return 0;
}