美文网首页
并查集Union Find

并查集Union Find

作者: SeekerLinJunYu | 来源:发表于2019-03-12 22:36 被阅读0次
对于并查集的理解?

a.并查集用于处理连接问题,可以非常快地判断出网络中节点的连接状态.能够快速实现数学中的集合类的并操作.
b.并查集其实也是一种树的数据结构,不过其与常规的BST不同,并查集的树结构是从子节点到父节点的一个访问顺序.

并查集的设计思路

并查集涉及到的核心方法是连接判断和并集运算.

并查集的实现:

1.直接基于数组实现. 实现Quick Find


数组实现原理图

实现代码:

/**
 * class Union Find based on array
 * @author Administrator
 *
 */
public class UnionFindEditon_QuickFind implements UnionFind{
    private int[] id;
    
    public UnionFindEditon_QuickFind(int size) {
        id = new int[size];     
        for (int i = 0; i < size; i++) {
            id[i] = i;
        }
    }
    
    @Override
    public int getSize() {
        return id.length;
    }
    
    @Override
    public void unionElements(int p, int q) {
        if (find(p) == find(q)) {
            return;
        }
        /**
         * assign the type of q to p
         */
        int pID = id[p];
        id[p] = id[q];
        /**
         * the step make the Degree-complex of the operation rise to O(n)
         */
        for (int i = 0; i < id.length; i++) {
            if (id[i] == pID) {
                id[i] = id[q];
            }
        }
    }
    
    @Override
    public boolean isConnected(int p, int q) {
        return find(p)==find(q);
    }
    
    
    public int find(int index) {
        if (index < 0 || index >= id.length ) {
            throw new IllegalArgumentException("index is out of boundary");
        }
        return id[index];
    }
}

2.基于数组实现,但数组元素之间存在对应关系.实现Quick Union


节点之间的关系 在数组中子父节点用parent[index]表达

代码实现:

/**
 * class UnionFind based on tree module
 * @author Administrator
 *
 */
public class UnionFindEditon_QuickUnion implements UnionFind {
    private int[] parent;
    
    public UnionFindEditon_QuickUnion(int size) {
        parent = new int[size];
        for (int i = 0; i < size; i++) {
            parent[i] = i;
        }
    }
    
    @Override
    public int getSize() {
        return parent.length;
    }
    
    
    /**
     * determines whether two elements are connected
     */
    @Override
    public boolean isConnected(int p, int q) {
        if (find(p) == find(q)) {
            return true;
        }
        return false;
    }
    
    /**
     * method --> union the two elements
     */
    @Override
    public void unionElements(int p, int q) {
        if (find(p) == find(q)) {
            return;
        }
        else {
            parent[find(p)] = find(q);
        }
    }
    
    /**
     * get the root of the target index
     * @param index
     * @return
     */
    public int find(int index) {
          if(index < 0 || index >= parent.length)
              throw new IllegalArgumentException("p is out of bound.");
        if (parent[index] != index ) {
            index = parent[index];
        }
        return index;
    }
}

相关文章

  • 从并查集Union Find算法谈算法解决实际问题的过程

    从并查集Union Find算法谈算法解决实际问题的过程算法并查集算法寻找路径 从并查集Union Find算法谈...

  • 神奇的数据结构---并查集

    并查集(Union Find)何为集,集合,用树表示;何为并,集合的合并(union);何为查,查询元素所属集合(...

  • [并查集]并查集的升级路线(四)

    路径压缩并查集 并查集在经过了quick-find、quick-union、加权quick-union的演化之后,...

  • 深入理解并查集

    并查集是一种树形结构,它是由并查集算法进行维护的。而并查集算法(Union-find-algorithm),顾名思...

  • union find 并查集

    一、数据的预处理 要找一个数是不是在数组中,不可能用遍历的方法实现,这样时间复杂度就超过O(n)了。而要降低时间复...

  • 并查集 | find & union

    并查集 Disjoint Setb站大佬的讲解视频以下截屏来自⬆️讲解视频 应用 检查无向图中是否有环 Krusk...

  • 并查集(Union Find)

    并查集也叫做不相交集合(Disjoint Set),这种数据结构主要用来快速的判断两个集合是否有交集的问题,或者说...

  • 并查集 Union Find

    什么是并查集 一种很不一样得到树形结构 连接问题image.png

  • 并查集--Union Find

    并查集的概念 一种可以用来判断相互关联(同属一个集合)的元素属于几个集合,也可以用来判断图结构中的两点是否是联通的...

  • 并查集Union Find

    对于并查集的理解? a.并查集用于处理连接问题,可以非常快地判断出网络中节点的连接状态.能够快速实现数学中的集合类...

网友评论

      本文标题:并查集Union Find

      本文链接:https://www.haomeiwen.com/subject/ndgopqtx.html