Union Find
Use array to save space
int[] array = new int[len]; // assign a initial size
public void makeSet(int i) {
array[i] = i;
}
public void union(int a, int b) {
if(findSet(a)!=findSet(b)) {
array[array[a]] = array[b];
}
}
public int findSet(int a) {
if(array[a]!=a) {
array[a] = findSet(array[a]);
}
return array[a];
}Self contained into a class
Use rank to balance the set
Last updated