Disjoint Set
  
   Step-by-Step Animation   Animation Speed 
Elaboration:
Node
Size
?
Disjoint Set: Any two sets have no common elements. There are three different implementations:
1. Union-By-Height: When performing union on two root nodes, merge the root node with the smaller height onto the root node with the higher height
2. Union-By-Size: When performing union on two root nodes, merge the root node with smaller size onto the root node with bigger size
3. Union-By-Rank: Union is the same as Union-By-Height. When performing Find, it makes every node on the path points to its root node.
API explanation:
1. Initialize Disjoint Set: Initialize the disjoint set with size n - O(n)
2. Union: Merge two disjoint sets - O(1)
3. Find: Find the root node id given an id - O(log(n)) with Union-By-Height and Union-By-Size and O(α(n)) with Union-By-Rank with path compression
并查集定义:并查集是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题。一些常见的用途有求连通子图、求最小生成树的 Kruskal 算法和求最近公共祖先(Least Common Ancestors, LCA)等。在并查集进行合并操作时,共有三种合并方式:
1. Union-By-Height: 该方法使用秩来表示树高度的上界,在合并时,为如果两个根节点的高度度一致,第一个输入的节点会合并到第二个输入的节点下方,成为其子节点,如果不一致,总是将具有较小秩的树根指向具有较大秩的树根。简单的说,就是总是将比较矮的树作为子树,添加到较高的树中。
2. Union-By-Size: 在合并时根据两个集合中根节点的大小来进行合并,根节点大小即时它包含的所有子节点个数以及它本身,将小的集合添加到大的集合中去。
3. Union-By-Rank: 和Union-By-Height一样当执行union时候, 但是执行find的时候, 每个途径的节点将执行根节点
按键说明:
1. Initialize Disjoint Set: 在文本框中输入0-20的数字,点击按钮即可创建对应数目的节点,节点ID从0开始。
2. Union 在文本框中输入两个节点的id,注意:此处输入的节点必须是两个根节点,如果输入的节点已经成为了一个集合中的根节点的子节点,那么将无法合并。
3. Find: 在文本框中输入要查找的节点ID,下方动画就会显示出从选定节点到其根节点的路径。