-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdisjoint_set_union_parent_array.cpp
More file actions
108 lines (88 loc) · 2.46 KB
/
Copy pathdisjoint_set_union_parent_array.cpp
File metadata and controls
108 lines (88 loc) · 2.46 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
// Implementation of disjoin set union find by parent array representation
// Time Complexity : O(N)
#include<iostream>
#include<algorithm>
#define MAX 1000
using namespace std ;
// Manage the connectivity of elements
//store the parent corresponding to the index
//initially the parent of index is the index itself
int parent[MAX];
int lenParent ;
void makeSet(){
for(int i = 0; i<lenParent ;i++){
parent[i] = i;
}
}
//Returns the root of the input index
//The root is found if the parent of index is the index itself
int root(int i){
if(parent[i] == i)
return i;
// If i is not the root, then move upper to find the root
else
root(parent[i]);
}
// Union by changing the parent of the element
// a is being merged to b so the root of either will be changed
int unionSet(int a, int b){
int rootA = root(a);
int rootB = root(b) ;
parent[rootA] = rootB ;
}
//The elements are in same set if their roots are same
bool isConnected(int a , int b){
if(root(a) == root(b)){
return true;
}
else
return false;
}
//Display parent array
void displayParents(){
for(int i= 0 ; i<lenParent; i++){
cout << parent[i] << " ";
}
cout << endl ;
}
// Driver program to test the above implementation
int main()
{
int n, t;
//int arr[MAX] /*You can use arr[MAX] to store the elements and then perfom disjoint set operations according to index of arr*/
cout << "Enter the number of elements..Min-10..Max Limit("<<MAX<<")\t";
cin >> n;
lenParent = n;
// Make the set for n elements
makeSet();
cout << "After make set \n";
displayParents();
unionSet(2,1);
cout << "After union of 1 and 2\n" ;
displayParents();
unionSet(4,3);
unionSet(8,4);
unionSet(9,3);
cout << "After union of 4,3,8,9 \n";
displayParents();
cout <<"Parent of element with index 4\n";
cout << parent[4] << endl;
cout << "Root of element with index 4\n";
cout << root(4) << endl;
cout <<"Is 1 and 2 connected or in same set\n";
if(isConnected(1,2))
cout << "True" << endl ;
else
cout << "False" << endl ;
cout << "Is 4 and 8 connected or in same set\n" ;
if(isConnected(4,8))
cout << "True" << endl;
else
cout << "False" << endl ;
cout << "Is 4 and 7 connected or in same set\n";
if(isConnected(4,0))
cout << "True" << endl ;
else
cout << "False" << endl ;
return 0 ;
}