Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Disjoint_Sets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <bits/stdc++.h>
using namespace std;

class DisjSet {
int *rank, *parent, n;

public:
DisjSet(int n)
{
rank = new int[n];
parent = new int[n];
this->n = n;
makeSet();
}

void makeSet()
{
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}

int find(int x)
{
if (parent[x] != x) {

parent[x] = find(parent[x]);

}

return parent[x];
}

void Union(int x, int y)
{
int xset = find(x);
int yset = find(y);

if (xset == yset)
return;

if (rank[xset] < rank[yset]) {
parent[xset] = yset;
}
else if (rank[xset] > rank[yset]) {
parent[yset] = xset;
}

else {
parent[yset] = xset;
rank[xset] = rank[xset] + 1;
}
}
};

int main()
{

DisjSet obj(5);
obj.Union(0, 2);
obj.Union(4, 2);
obj.Union(3, 1);

if (obj.find(4) == obj.find(0))
cout << "Yes\n";
else
cout << "No\n";
if (obj.find(1) == obj.find(0))
cout << "Yes\n";
else
cout << "No\n";

return 0;
}