Skip to content

Instantly share code, notes, and snippets.

@gtke
Created September 2, 2012 21:57
Show Gist options
  • Save gtke/3604922 to your computer and use it in GitHub Desktop.
Save gtke/3604922 to your computer and use it in GitHub Desktop.
Quick Find Algorithm Java Implementation
public class quickFind {
private int [] id;
public void QuickFindUF (int N){
id = new int [N];
for(int i = 0; i< N; i++){
id[i]=i;
}
}
public boolean connected(int p, int q){
return id[p] == id[q];
}
public void union(int p, int q){
int pid = id[p];
int qid = id[q];
for(int i =0; i<id.length; i++){
if(id[i] == pid){
id[i] = qid;
}
}
}
}
@juhar44
Copy link

juhar44 commented Mar 31, 2018

i need more clear example

@zerolimitss
Copy link

Why did you set a new variable qid in union method? You can use id[q] inside for loop instead of qid. The result will not change.

@abranhe
Copy link

abranhe commented Aug 17, 2018

There you have an amazing implementation of QuickFind Algorithm from the Algorithms Course at Princeton University.

@nehuenlabs
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment