Created
February 5, 2015 22:13
-
-
Save gnusosa/cfec495afbf4bcea752f to your computer and use it in GitHub Desktop.
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
package com.example.unionfind; | |
/** | |
* Created by gnusosa on 2/5/15. | |
*/ | |
public class QuickFindUF { | |
private int[] id; | |
public 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; | |
} | |
} | |
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
package com.example.unionfind; | |
/** | |
* Created by gnusosa on 2/5/15. | |
*/ | |
import com.example.unionfind.QuickFindUF; | |
public class UnionFindRun { | |
public static void main(String[] args) { | |
QuickFindUF uf = new QuickFindUF(10); | |
uf.union(1,9); | |
uf.union(6,0); | |
uf.union(4,5); | |
uf.union(2,9); | |
uf.union(9,7); | |
uf.union(3,6); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment