1. Dynamic Connectivity Problem :
a) Union command : connect two objects
b) Find/connected query: is there a path connecting the two objects?
2. Quick-find
a) Integer array id[] of size N
b) Interpretation: p and q are connected iff they have the same id.
c) Find: Check if p and q have the same id.
d) Union : To merge components containing p and q, change all entries whose id equals id[p] to id[q].
Find is quick which only takes 2 array access. Union is expensive , it cost N array access and N union will cost N^2
3. Quick-union
a) Integer array id[] of size N.
b) Interpretation: id[i] is parent of i.
c) Root of i is id[id[id[...id[i]...]]].
d) Find: Check if p and q have the same root.
e) Union: To merge components containing p and q, set the id of p's root to the id of q's root.
Both Union and Find can be expensive in worst case.
4. Quick-find defect:
a) Union too expensive (N array accesses).
b) Trees are flat, but too expensive to keep them flat.
Quick-union defect.
a) Trees can get tall.
b) Find too expensive (could be N array accesses).
5. Weighted quick-union.
a) Modify quick-union to avoid tall trees.
b) Keep track of size of each tree (number of objects).
c) Balance by linking root of smaller tree to root of larger tree.
Proposition: Depth of any node x is at most lg N.
Pf. When does depth of x increase?
Increases by 1 when tree T1 containing x is merged into another tree T2.
1) The size of the tree containing x at least doubles since | T 2 | ≥ | T 1 |.
2) Size of tree containing x can double at most lg N times. (the total # of nodes is at most N)
6. Quick union with path compression: Just after computing the root of p, set the id of each examined node to point to that root.
Two-pass implementation: add second loop to root() to set the id[] of each examined node to the root.
Simpler one-pass variant: Make every other node in path point to its grandparent (thereby halving path length).
分享到:
相关推荐
基于Union-Find数据结构实现Kruskal求最小生成树,代码设计及变量命名附详细注释。基于Union-Find数据结构实现Kruskal求最小生成树,代码设计及变量命名附详细注释。
Union-Find: A Data Structure for Disjoint Set Operations
前端大厂最新面试题-union-find.docx
利用Union-find算法实现路径压缩最小生成树,使用c编写
联合查找 这是 Node.js 中 union-find 数据结构的一个实现。 在做个人项目时,我没有找到适合我需求的 NPM 模块并创建了它。 如果有人对此模块感兴趣,或者想要... 使用 UnionFind#inSameGroup 确定两个节点是否在同一
并查集.并查集(Union-Find)是一种特殊的数据结构,它可以高效地管理一系列不交集的合并和查询操作。这种数据结构通常用树形结构来表示,并且常常在实际应用中以森林的形式存在。
data.union-find 使用 Tarjan 的联合查找算法的持久不相交集森林的 Clojure 实现。 可通过在莱宁根: [org.jordanlewis/data.union-find "0.1.0"] 用法 创建一个新的 union-find 数据结构,包含其作为单例集的...
数据结构并查集的相关资料,包括几篇并查集的论文,还有POJ上面几道关于并查集的题目的源代码
数据结构、算法相关的资源 数据结构、算法相关的资源 数据结构、算法相关的资源 数据结构、算法相关的资源 数据结构、算法相关的资源 数据结构、算法相关的资源 数据结构、算法相关的资源 数据结构、算法相关的资源 ...
这是UNION-FIND数据结构的小型独立C ++ 11实现,具有按等级压缩和按路径合并以及一些附加功能。它支持并发find() , same()和unite()调用,如本文所述 联合发现问题的免等待并行算法,作者:Richard J. Anderson和...
Disjoint-Sets-using-Union-Find:使用联合查找和树进行路径压缩的不相交集
带权重的union_find可以有效降低树的高度,...public class UnionFind { private int count;//连通分量的个数 private int[] pid;//保存父亲连接节点的id private int[] sonSize;//保存各个节点作为根节点的分量大小
很好地算法设计资料,你值得拥有哦,里面各种算法设计很详细的哦。