Created
September 2, 2012 21:57
-
-
Save gtke/3604922 to your computer and use it in GitHub Desktop.
Quick Find Algorithm Java Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
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.
There you have an amazing implementation of QuickFind Algorithm from the Algorithms Course at Princeton University.
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i need more clear example