Algorithms-Advanced-Union-Find-notes

Lazy Unions

The Union-Find Data Structure
FIND: Given xX x ∈ X , return na,e of x’s group.
UNION: Given x & y, merge groups containing them.
Previous solution.(for Kruskal’s MST algorithm)
Each xX points directly to the “leader” of its grou[.
O(1) FIND [just return x’s leader]
O(nlog(n)) total work for n UNIONS[when 2 groups merge,
smaller group inherits leader of large one]
Lazy Unions
New idea: Update only one pointer each merge.
In array representation:
(Where A[i] A [ i ] ↔ name of is i ′ s parent.
How to Merge
In general: When two groups merge in a UNION, make one group’s leader
[root of the tree] a child of the other one.
Pro: UNION reduces to 2 FINDS[r1 = FIND(x), r2 = FIND(y)] and O(1) O ( 1 ) extra work [link r1, r2 together]
Con: To recover leader of an object, need to follow a path of parent pointers[not just one]
Not clear if FIND still takes O(1) O ( 1 ) time.

Union-Find (Union by Rank)
The lazy Union Implementation
New implementation:
Each object xX x ∈ X has a parent field.
Invariant: Parent pointers induce a collection of directed trees on X.
(x is a root parent[x] = x)
Initially: For all x, parent[x] = x;
FIND(x): Traverse parent pointers from x until you hit the root.
UNION(x,y): s1 s 1 = FIND(x); s2=FIND(y) s 2 = F I N D ( y ) ; Reset parent of one of s1,s2 s 1 , s 2 to be the other.
Union by rank
Ranks: For each xX x ∈ X , maintain field rank[x].
[In general rank[x] = 1+ (max rank of x’s children)]
Invarant (for now): For all xX x ∈ X , rank[x] = maximum number of hops from some leaf to x.
[Initially, rank[x] = 0 for all xX x ∈ X ]
To avoid scraggly trees.Given x & y:
s1= FIND(x), s2 s 2 = FIND(y)
If rank[s1] > rank[ s2 s 2 ] then set parent[ s2 s 2 ] to s1 s 1
else set parent[ s1 s 1 ] to s2 s 2 .

Properties of Ranks
Recall: Lazy Unions.
Invariant (for now): rank[x] = max # of hops from a leaf to x.
[Note maxxrank[x] m a x x r a n k [ x ] ≈ worst-case running time of FIND].
Union by Rank: Make old root with smaller rank child of the root with larger rank.
[Choose new root arbitrarily in case of a tie, and add 1 to its rank.]
Immediate from Invariant/Rank Maintenance:
(1) For all object x, rank[x] only goes up over time
(2) Only ranks of roots can go up.
[once x a non-root, rank[x] frozen forevermore]
(3) Ranks strictly increase along a path to the root.
Rank Lemma
Rank Lemma: Consider an arbitrary sequence of UNION(+ FIND)
operations. For every r{0,1,2,...} r ∈ { 0 , 1 , 2 , . . . } , there are at most n2r n 2 r objects with rank r r .
Corollary(推论): Max rank always log2n
Corollary(推论): Worst-case running time of FIND, UNION is O(log n).
Proof of Rank Lemma:
Claim 1: If x, y have the same rank r r , then their subtrees are disjoint.
Claim 2: The subtree of a rank-r object has size 2r.
[Note Claim 1 + Claim 2 imply the Rank Lemma].

Path Compression
Idea: Why bother traversing a leaf-root path multiple times?
Path compression: After FIND(x), install shortcuts(i,e, revise pointers)
to x’s root all along the x root path.
Con: Constant-factor overhead to FIND
Pro: Speeds up subsequent FINDs.
On Ranks
Important: Maintain all rank fields EXACTLY as without path compression.
Rank initially all 0.
In UNION, new root = old root with bigger rank.
When mergeing two nodes of common rank r, reset new root’s rank to r+1 r + 1 .
Bad news, Now rank[x] is only an upper bound on the maximum number of hops on a path from a leaf to x.
Good news: Rank Lemma still holds( n2r ≤ n 2 r objects with rank r)
Also: Still always have rank[parent[x]] > rank[x] for all non-roots x.
Hopcroft-Ullman Theorem
Theorem: With Union by Rank and path compression, m Union + Find operation takes O(mlogn) O ( m l o g ∗ n ) time, where logn= l o g ∗ n = the number of times you need to apply log to n before the result it 1 ≤ 1 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值