Skip to content

Commit 6064790

Browse files
author
precisecharmer
authored
Add disjoint_set.cpp
1 parent 18f8626 commit 6064790

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

disjoint set - Copy/code.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// This section is for Disjoint set data structure
2+
// when we find parent of any number in worst case we go O(n) so if for all the numbers on the path we directly make their parent as root
3+
// so when we make M number of operations and take thier average complexity that is alpha(n) which <= 4 for even biggest values
4+
// their is no difference in code except the part where we find parent there we add one more line for changing parent of every node on
5+
// the path
6+
7+
#include <bits/stdc++.h>
8+
using namespace std;
9+
#define ll long long
10+
const int inf = 1e9 + 7;
11+
12+
int find(vector<int> &parent, int x)
13+
{
14+
if (parent[x] == x)
15+
{
16+
return x;
17+
}
18+
parent[x] = find(parent, parent[x]);
19+
return parent[x];
20+
}
21+
22+
void union_num(vector<int> &parent, vector<int> &rank, int num1, int num2)
23+
{
24+
int fparent = find(parent, num1);
25+
int sparent = find(parent, num2);
26+
if (fparent == sparent)
27+
{
28+
return;
29+
}
30+
if (rank[fparent] < rank[sparent])
31+
{
32+
parent[fparent] = sparent;
33+
}
34+
else if (rank[fparent] > rank[sparent])
35+
{
36+
parent[sparent] = fparent;
37+
}
38+
else
39+
{
40+
parent[fparent] = sparent;
41+
rank[sparent]++;
42+
}
43+
}
44+
45+
void solve()
46+
{
47+
int n = 8; // 0 - 7
48+
vector<int> parent(n), rank(n, 0);
49+
for (int i = 0; i < n; i++)
50+
{
51+
parent[i] = i;
52+
}
53+
int total;
54+
cin >> total;
55+
while (total--)
56+
{
57+
int choice;
58+
cin >> choice;
59+
if (choice == 1)
60+
{
61+
// union
62+
int num1, num2;
63+
cin >> num1 >> num2;
64+
union_num(parent, rank, num1, num2);
65+
}
66+
else
67+
{
68+
// find
69+
int num1, num2;
70+
cin >> num1 >> num2;
71+
int fparent = find(parent, num1), sparent = find(parent, num2);
72+
73+
if (fparent == sparent)
74+
{
75+
cout << "They are in same group!!" << endl;
76+
}
77+
else
78+
{
79+
cout << "They are not in same group!!" << endl;
80+
}
81+
}
82+
83+
cout << "Check parent array: " << endl;
84+
for (int i = 0; i < parent.size(); i++)
85+
{
86+
cout << parent[i] << " ";
87+
}
88+
cout << endl;
89+
cout << "Check Rank array: " << endl;
90+
for (int i = 0; i < rank.size(); i++)
91+
{
92+
cout << rank[i] << " ";
93+
}
94+
cout << endl;
95+
}
96+
}
97+
98+
int main()
99+
{
100+
solve();
101+
return 0;
102+
}

disjoint set - Copy/readme.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This section is for Disjoint set data structure
2+
3+
4+
First one is basic one which adds first over second whithout any optimisation
5+
6+
second one sees height of tree and add according to that
7+
8+
Third one makes average time complexity <= 4 because it makes parent of every node as root during any normal find function

0 commit comments

Comments
 (0)