public class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n) {
rank = new int[n];
parent = new int[n];
this.n=n;
makeSet();
}
void makeSet() {
for(int i=0;i<n;i++){ // Initially, all elements are in their own set.
parent[i] = i;
}
}
int find(int x){ // Finds the representative of the set that x is an element of
if(parent[x]!=x){
//if x is not the parent of itself
// Then x is not the representative of his set,
parent[x] = find(parent[x]);
// so we recursively call Find on its parent
// and move i's node directly under the representative of this set
}
return parent[x];
}
void union(int x, int y){ // Unites the set that includes x and the set that includes y
int xRoot = find(x); // Find the representatives (or the root nodes) for the set that includes x
int yRoot = find(y); // And do the same for the set that includes y
if(xRoot == yRoot) // Elements are in the same set, no need to unite anything.
return;
if(rank[xRoot]<rank[yRoot]){ // If x's rank is less than y's rank
parent[xRoot] = yRoot; // Then move x under y so that depth of tree remains less
}
else if(rank[yRoot]<rank[xRoot]){ // Else if y's rank is less than x's rank
parent[yRoot] = xRoot; // Then move y under x so that depth of tree remains less
}
else{ // Else if their ranks are the same
parent[yRoot] = xRoot; // Then move y under x (doesn't matter which one goes where)
rank[xRoot] = rank[xRoot] + 1; // And increment the the result tree's rank by 1
}
}
}