Interview Questions: Union-Find

Union-Find

Social network connectivity. Given a social network containing n members and a log file containing m timestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member is a friend of a friend of a friend … of a friend). Assume that the log file is sorted by timestamp and that friendship is an equivalence relation. The running time of your algorithm should be mlogn or better and use extra space proportional to n.

import java.util.Scanner;

public class UF {
    int count;
    int[] id;
    int[] height;

    public UF(int n) {
        count = n;
        id = new int[n];
        height = new int[n];
        for (int i = 0; i < n; i++) {
            id[i] = i;
            height[i] = 0;
        }
    }

    public int count() {
        return count;
    }

    public int find(int p) {
        int root = p;
        while (root != id[root]) {
            root = id[root];
        }
        while (p != root) {
            int temp = id[p];
            id[p] = root;
            p = temp;
        }
        return root;
    }

    public boolean connected(int p, int q) {
        return find(p) == find(q);
    }

    public void union(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        if (rootP == rootQ) {
            return;
        }

        if (height[p] < height[q]) {
            id[rootP] = rootQ;
        } else {
            id[rootQ] = rootP;
            if (height[rootP] == height[rootQ]) {
                height[rootP]++;
            }
        }
        count--;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        UF uf = new UF(n);

        for (int i = 0; i < m; i++) {
            String timeStamp = sc.next();
            int p = sc.nextInt();
            int q = sc.nextInt();

            if (uf.connected(p, q)) {
                continue;
            }

            uf.union(p, q);
            if (uf.count == 1) {
                System.out.println(timeStamp);
                break;
            }
        }
    }
}

Union-find with specific canonical element. Add a method ????() to the union-find data type so that ????(?) returns the largest element in the connected component containing i. The operations, ?????(), ?????????(), and ????() should all take logarithmic time or better.

For example, if one of the connected components is {1, 2, 6, 9}, then the ????() method should return 9 for each of the four elements in the connected components.

public class UF {
    int count;
    int[] id;
    int[] height;
    int[] max;

    public UF(int n) {
        count = n;
        id = new int[n];
        height = new int[n];
        max = new int[n]
        for (int i = 0; i < n; i++) {
            id[i] = i;
            height[i] = 0;
            max[i] = i;
        }
    }

    public int count() {
        return count;
    }

    public int find(int i) {
        return max[findRoot(i)];
    }

    public int findRoot(int p) {
        int root = p;
        while (root != id[root]) {
            root = id[root];
        }
        while (p != root) {
            int temp = id[p];
            id[p] = root;
            p = temp;
        }
        return root;
    }

    public boolean connected(int p, int q) {
        return findRoot(p) == findRoot(q);
    }

    public void union(int p, int q) {
        int rootP = findRoot(p);
        int rootQ = findRoot(q);
        if (rootP == rootQ) {
            return;
        }

        if (height[p] < height[q]) {
            id[rootP] = rootQ;
            max[rootQ] = max[rootP] > max[rootQ] ? max[rootP] : max[rootQ];
        } else {
            id[rootQ] = rootP;
            if (height[rootP] == height[rootQ]) {
                height[rootP]++;
            }
            max[rootP] = max[rootQ] > max[rootP] ? max[rootQ] : max[rootP];
        }
        count--;
    }
}

Successor with delete. Given a set of nn integers S={0, 1, …, n−1} and a sequence of requests of the following form:

  • Remove x from S
  • Find the successor of x: the smallest y in S such that yx.

design a data type so that all operations (except construction) take logarithmic time or better in the worst case.

public class UF {
    int count;
    int[] id;
    int[] height;
    int[] max;
    boolean[] isRemoved;

    public UF(int n) {
        count = n;
        id = new int[n];
        height = new int[n];
        max = new int[n];
        isRemoved = new boolean[n];
        for (int i = 0; i < n; i++) {
            id[i] = i;
            height[i] = 0;
            max[i] = i;
            isRemoved[i] = false;
        }
    }

    public int count() {
        return count;
    }

    public int findRoot(int p) {
        int root = p;
        while (root != id[root]) {
            root = id[root];
        }
        while (p != root) {
            int temp = id[p];
            id[p] = root;
            p = temp;
        }
        return root;
    }

    public boolean connected(int p, int q) {
        return findRoot(p) == findRoot(q);
    }

    public void union(int p, int q) {
        int rootP = findRoot(p);
        int rootQ = findRoot(q);
        if (rootP == rootQ) {
            return;
        }

        if (height[p] < height[q]) {
            id[rootP] = rootQ;
            max[rootQ] = max[rootP] > max[rootQ] ? max[rootP] : max[rootQ];
        } else {
            id[rootQ] = rootP;
            if (height[rootP] == height[rootQ]) {
                height[rootP]++;
            }
            max[rootP] = max[rootQ] > max[rootP] ? max[rootQ] : max[rootP];
        }
    }

    public void remove(int x) {
        if (isRemoved[x] == true) {
            return;
        }
        isRemoved[x] = true;


        if (x < count - 1 && isRemoved[x + 1]) {
            union(x, x + 1);
        }
        if (x > 0 && isRemoved[x - 1]) {
            union(x, x - 1);
        }
    }

    public int getSuccessor(int x) {
        if (isRemoved[x] == false) {
            return x;
        }

        if (max[findRoot(x)] + 1 >= count) {
            return -1;
        }

        return max[findRoot(x)] + 1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值