
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The java code implementation of the quick union union-find algorithm, which is a data structure used for determining if two objects belong to the same set and for unioning two sets into one. The algorithm is commonly used in graph theory and other areas of computer science.
Typology: Study notes
1 / 1
This page cannot be seen from the preview
Don't miss anything!

import java.util.Scanner;
class QuickUnionUF { int id[];
QuickUnionUF(int n) { id=new int[n]; for(int i=0;i<n;i++) id[i]=i; }
private int root (int i) { while(i!=id[i]) i=id[i]; return i; }
public boolean connected(int a, int b) { return root(a)==root(b); }
public void union(int a,int b) { id[root(a)]=root(b); // Note: The roots have the same id as their index number
}