带有路径压缩和rank优化的并查集实现

public class unionfind2 implements UF {

    int[] parent;
    int[] rank;
    
    public unionfind2(int n) {
        parent=new int[n];
        rank=new int[n];
        for(int i=0;i<parent.length;i++) {
            parent[i]=i;
            rank[i]=1;
        }
    }
    
    private int find(int i) {
        while(i!=parent[i]) {
            parent[i]=parent[parent[i]];
            i=parent[i];
        }
        return i;
    }
    @Override
    public boolean isConnected(int p, int q) {
        // TODO Auto-generated method stub
        return find(p)==find(q);
    }

    @Override
    public void  union(int p,int q) {
        int proot=find(p);
        int qroot=find(q);
        if(proot==qroot) return;
        else {
            parent[proot]=parent[qroot];
        }
    }
    
    public void  unionWithRank(int p, int q) {
        // TODO Auto-generated method stub
        int proot=find(p);
        int qroot=find(q);
        if(proot==qroot) return;
        
        if(rank[proot]<rank[qroot]) {
            parent[proot]=parent[qroot];
        }else if(rank[proot]>rank[qroot]){
            parent[qroot]=parent[proot];
        }else {
            parent[qroot]=parent[proot];
            rank[proot]+=1;
        }
    }
    
    

    @Override
    public int getSize() {
        // TODO Auto-generated method stub
        return parent.length;
    }

}

 

posted @ 2018-09-18 10:43  即便那总是过去  阅读(206)  评论(0编辑  收藏  举报