Interview Questions: Union-Find
Hint: union−find.
题目的意思是有一个包含n个成员的社交网络,日志文件log按照时间戳顺序存储了两个成员之间成为朋友的时间,共有m条记录。让我们设计一个算法来根据这个log文件来计算m个成员全部通过朋友关系连通的时间。
这是个典型的并查集。思路是读取日志文件,遍历文件记录,逐条记录union。采用加权quick-union算法,就可以满足mlogn的复杂度要求。
public class SocialNetwork{
public void main(String[] args){
}
public class UnionFind{
private int[] id;
private int[] sz;
private int nCount;
private boolean bAllConnected;
// we use the path compress
private int root(int i){
while(i != id[i]){
id[i] = id[id[i]];
i = id[i];
}
return i;
}
public UnionFind(int n){
nCount = n;
bAllConnected = false;
id = new int[n];
sz = new int[n];
for(int i = 0; i < n; i++){
id[i] = i;
sz[i] = 1;
}
}
public boolean connected(int p, int q){
return root(p) == root(q);
}
public boolean linkPair(int p, int q){
int rp = root(p);
int rq = root(q);
if(rp == rq) return bAllConnected ;
if(sz[rp] < sz[rq]){
sz[rq] += sz[rp];
id[rp] = rq;
if(sz[rq] == nCount)
bAllConnected = true;
}else{
sz[rp] += sz[rq];
id[rq] = rp;
if(sz[rp] == nCount)
bAllConnected = true;
}
return bAllConnected;
}
}
}
Hint: maintain an extra array to the weighted quick-union data structure that stores for each root \mathtt{i}i the large element in the connected component containing \mathtt{i}i.
分析:
与加权 quick-union算法思路大概一致,在union()方法中进行根节点的控制,使得根节点始终是最大的数。
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class UF {
private int[] id; //分量id
private int count; //分量数量
public UF(int N) {
//初始化分量id数组
count = N;
id = new int[N];
for (int i = 0; i < N; i++)
id[i] = i;
}
public int count() {
return count;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int find(int p) {
while (p != id[p])
p = id[p];
return p;
}
public void union(int p, int q) {
int i = find(p);
int j = find(q);
if (i == j)
return;
if (i > j)
id[j] = i;
else
id[i] = j;
count--;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//解决由StdIn得到的动态连通性问题
int N = StdIn.readInt();
UF uf = new UF(N);
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.connected(p, q)) continue;
uf.union(p, q);
StdOut.println(p + "" + q);
}
StdOut.println(uf.count() + "components");
}
}
分析:
是对题目2的应用;每次remove(x)都是将 x 和 x + 1 进行连接,每次remove(x)会使数组的x消失可以等价成寻找根节点的模型。getsuccessor(x)即是返回 x 的根节点。
Hint: use the modification of the union−find data discussed in the previous question.
import edu.princeton.cs.algs4.StdOut;
public class Successor {
private int[] id; //父链接索引
public Successor(int N) {
id = new int[N];
for(int i = 0; i < N; i++)
id[i] = i;
}
public int find(int p) {
//找到根节点
while(p != id[p])
p = id[p];
return p;
}
public void remove(int p) {
union(p, p + 1);
}
public void union(int p, int q) {
int i = id[p];
int j = id[q];
if (i == j)
return;
//将较大的数作为根节点
else if (i > j)
id[q] = i;
else
id[p] = j;
}
public int getsuccessor(int p) {
return find(p);
}
public static void main(String[] args) {
Successor s = new Successor(10);
s.remove(6);
s.remove(5);
s.remove(3);
StdOut.println("the successor of 3 is " + s.getsuccessor(3));
s.remove(4);
s.remove(7);
StdOut.println("the successor of 3 is " + s.getsuccessor(3));
StdOut.println("the successor of 1 is " + s.getsuccessor(1));
}
}