-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathUnion–Find-Algorithm.cpp
More file actions
76 lines (61 loc) · 1.64 KB
/
Union–Find-Algorithm.cpp
File metadata and controls
76 lines (61 loc) · 1.64 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
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
// A class to represent a disjoint set
class DisjointSet
{
unordered_map<int, int> parent;
public:
// perform MakeSet operation
void makeSet(vector<int> const &universe)
{
// create `n` disjoint sets (one for each item)
for (int i: universe) {
parent[i] = i;
}
}
// Find the root of the set in which element `k` belongs
int Find(int k)
{
// if `k` is root
if (parent[k] == k) {
return k;
}
// recur for the parent until we find the root
return Find(parent[k]);
}
// Perform Union of two subsets
void Union(int a, int b)
{
// find the root of the sets in which elements `x` and `y` belongs
int x = Find(a);
int y = Find(b);
parent[x] = y;
}
};
void printSets(vector<int> const &universe, DisjointSet &ds)
{
for (int i: universe) {
cout << ds.Find(i) << " ";
}
cout << endl;
}
// Disjoint–Set data structure (Union–Find algorithm)
int main()
{
// universe of items
vector<int> universe = { 1, 2, 3, 4, 5 };
// initialize `DisjointSet` class
DisjointSet ds;
// create a singleton set for each element of the universe
ds.makeSet(universe);
printSets(universe, ds);
ds.Union(4, 3); // 4 and 3 are in the same set
printSets(universe, ds);
ds.Union(2, 1); // 1 and 2 are in the same set
printSets(universe, ds);
ds.Union(1, 3); // 1, 2, 3, 4 are in the same set
printSets(universe, ds);
return 0;
}