Back
Featured image of post Union-Find

Union-Find

Union-Find for kruskal , graph

Union-Find 并查集

Union-Find 并查集算法的关键在于unionconnected的效率 , 若只是简单的连接节点

**find , union , connected **三个函数的时间复杂度会是O(n) ,而通过平衡树,可使复杂度降低到O(logn)

再而通过路径压缩,可使得union和connected的时间复杂度达到O(1)

这是优化版的Union-Find , 防止了树退化成链表而降低了效率

public class UnionFind {
    private int count;//连通分量个数
    private int[] parent;//存储一棵树 , 记录每个节点的父节点,相当于指向父节点的指针
    private int[] size;//记录每棵树重量

    public UnionFind() {
    }

    public UnionFind(int n) {
        this.count = n;
        parent = new int[n];
        size = new int[n];

        for (int i = 0; i < n; i++) {
            parent[i] = i;//节点指向自己
            size[i] = 1;
        }
    }

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

        //get the balanced tree
        // 将两棵树合并为一棵树,小树接到大树下面,较平衡
        // 使其不会退化成链表而降低效率
        if (size[rootP] > size[rootQ]) {
            parent[rootQ] = rootP;
            size[rootP] += size[rootQ];
        } else {
            parent[rootP] = rootQ;
            size[rootQ] += size[rootP];
        }

        count--;
    }

    private int find(int x) {
        //路径压缩
        while (parent[x] != x) {
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        return x;
    }

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

    public int count() {
        return count;
    }
}
Welcome to the world of Minezeratul