Last active
November 28, 2015 06:20
-
-
Save rhexgomez/860aed32983eae35488c to your computer and use it in GitHub Desktop.
Quick Find Algorithm
This file contains 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 { | |
public static void main(String[] args){ | |
QuickFind pol = new QuickFind(10); | |
pol.union(3, 5); | |
pol.union(5, 6); | |
pol.union(3, 1); | |
pol.union(8, 0); | |
pol.union(8, 2); | |
pol.union(9, 3); | |
// check if 5 and 6 are connected | |
System.out.println(pol.connected(5,6)); | |
} | |
private int[] id; | |
QuickFind(int n) { | |
id = new int[n]; | |
for (int i = 0; i < n; i++) | |
id[i] = i; | |
} | |
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; | |
} | |
boolean connected(int p, int q) { | |
return id[p] == id[q]; | |
} | |
public int[] getResult() { | |
return id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment