详解并查集模板——理论和代码的实现

并查集的理解与实现

这里通过一个例子来讲解一下并查集
  • 问题
    我们要判断一个无向图中是否存在环,如果存在的话就返回Ture,否则的话就返回False。
    在这里插入图片描述
代码的实现
  • 进行路径路径优化是通过增加一个rank数组来记录根结点对应的数的高度,以此来判断当找到两个要合并的两个根结点的时候,应该将哪一个作为新的根结点,哪一个作为其子树。
public class Main {
    public static void main(String[] args) {
        //并查集,外加压缩路径进行优化
        final int VERTICES=6;
        int[] parent=new int[VERTICES];
        int[] rank=new int[VERTICES];
        int[][] edges={{0,1},{1,2},{1,3},{3,4},{2,4},{2,5}};
        Arrays.fill(rank,0);
        Arrays.fill(parent,-1);
        int i;
        for(i=0;i<6;++i){
            int x=edges[i][0];
            int y=edges[i][1];
            if(union_vertices(x,y,parent,rank)==0){
                System.out.println("存在环,结束!");
                return;
            }
        }
        System.out.println("不存在环!");
    }
    public static int find_root(int x,int[] parent){
        int x_root=x;
        while (parent[x_root]!=-1){
            x_root=parent[x_root];
        }
        return x_root;
    }
    /**
     * 返回1代表合并成功,返回0代表存在环失败
     * */
    public static int union_vertices(int x, int y, int[] parent, int[] rank) {
        int x_root=find_root(x,parent);
        int y_root=find_root(y,parent);
        if(x_root==y_root){
            return 0;
        }else {
            //parent[x_root]=y_root;   //这是优化之前的代码
            if(rank[x_root]>rank[y_root]){
                parent[y_root]=x_root;
            }else if(rank[x_root]<rank[y_root]){
                parent[x_root]=y_root;
            }else {
                parent[x_root]=y_root;
                rank[y_root]++;
            }
            return 1;
        }
    }
}

并查集模板

// 并查集模板
class UnionFind {
    int[] parent;
    int[] size;
    int n;
    // 当前连通分量数目
    int setCount;

    public UnionFind(int n) {
        this.n = n;
        this.setCount = n;
        this.parent = new int[n];
        this.size = new int[n];
        Arrays.fill(size, 1);
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
    }
    
    public int findset(int x) {
        return parent[x] == x ? x : (parent[x] = findset(parent[x]));
    }
    
    public boolean unite(int x, int y) {
        x = findset(x);
        y = findset(y);
        if (x == y) {
            return false;
        }
        if (size[x] < size[y]) {
            int temp = x;
            x = y;
            y = temp;
        }
        parent[y] = x;
        size[x] += size[y];
        --setCount;
        return true;
    }
    
    public boolean connected(int x, int y) {
        x = findset(x);
        y = findset(y);
        return x == y;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西瓜程序设计

您的打赏将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值