Docsity

QuickFindUf: A Connected Component Algorithm, Study notes of Algorithms and Programming

Partial preview of the text

Download QuickFindUf: A Connected Component Algorithm and more Study notes Algorithms and Programming in PDF only on Docsity!

class QuickFindUf { private int[] id;

QuickFindUf(int n) { id=new int[n]; for(int i=0;i<n;i++) id[i]=i; }

public boolean connected(int a,int b) { return (id[a]==id[b]); }

public void union(int a,int b) { int ida=id[a]; int idb=id[b];

int i;

for(i=0;i<id.length;i++) { if(id[i]==ida) id[i]=idb; } } }