简体   繁体   English

路径压缩和按秩联合如何相互补充?

[英]How do path compression and union by rank complement each other?

I have been reading about union-find problem.我一直在阅读有关联合查找问题的信息。 The two main improvements are path compression and union by rank.两个主要的改进是路径压缩和按等级联合。 As far as I understand union by rank is used to determine how to combine disjoint trees.据我了解,按等级联合用于确定如何组合不相交的树。 If we have two disjoint trees T1 and T2, then we attach the root of the tree with smaller rank to the tree with higher rank.如果我们有两个不相交的树 T1 和 T2,那么我们将具有较小秩的树的根附加到具有较高秩的树上。 If we don't use path compression then rank is just the depth of a tree.如果我们不使用路径压缩,那么等级就是树的深度。 This makes sense since we don't want to increase the depth of out tree,since it directly affects both finds and unions.这是有道理的,因为我们不想增加输出树的深度,因为它直接影响发现和联合。

My problem is when we use path compression as well.我的问题是当我们也使用路径压缩时。 I keep reading that the two optimizations complement each other, but I don't see that.我一直读到这两种优化相互补充,但我没有看到。 Because of path compression, rank is no longer depth of the tree (it becomes an upper bound on depth).由于路径压缩,秩不再是树的深度(它成为深度的上限)。 Suppose T1 has 2 branches (let rank of T1 be 3), and T2 has depth 2 and rank 2. Now suppose we perform find operation (with path compression) on the leaf of T1 marked with "*" below.假设 T1 有 2 个分支(令 T1 的秩为 3),T2 的深度为 2,秩为 2。现在假设我们对下面标有“*”的 T1 的叶子执行查找操作(带有路径压缩)。 Now if we union root of T1 and root of T2, then T2 would be attached to the root of T1(since rank is not updated by find).现在,如果我们联合 T1 的根和 T2 的根,那么 T2 将附加到 T1 的根上(因为 rank 没有被 find 更新)。 The resulting tree has depth 3. But we could have had a better performance if we attached T1 to T2.结果树的深度为 3。但是如果我们将 T1 附加到 T2,我们可以获得更好的性能。

T1:   o   (Rank = 3)    T2:   o  (Rank = 2)
     / \                      |
    o   o                     o
    |                         |
    o                         o
    |   
    *   

After a find on leaf of T1("*"), then union on roots of T1 and T2 we get在 T1("*") 的叶子上找到后,然后在 T1 和 T2 的根上并集我们得到

T1:    o       (Rank = 3)      T2: o  (Rank = 2)       
     /| |\                         |
    * o o o                        o
                                   |
                                   o
Result of T1 union T2
       o
   / | | |\
  * o o o  o   Rank = 3 and Max Depth = 3
           |
           o
           |
           o

Am i missing something here?我在这里错过了什么吗? How do path compression and union by rank complement each other?路径压缩和按秩联合如何相互补充? I know rank is just an upper bound on depth of a tree but I don't see how union by rank is improving the overall performance of the structure.我知道等级只是树深度的上限,但我没有看到按等级联合如何提高结构的整体性能。 How is this better than a union that combines the roots randomly?这比随机组合根的联合如何更好?

Thanks for the help in advance.我在这里先向您的帮助表示感谢。

Union by rank ensures that the maximum depth of the tree is log N, so it puts a worst case upper bound of O(log N) on every operation. Union by rank 确保树的最大深度为 log N,因此它在每个操作上都设置了 O(log N) 的最坏情况上限。

Path compression without any special union rules an upper bound of O(log N) on the amortized cost of each operation, but doesn't limit the worst case cost .没有任何特殊联合的路径压缩规定每个操作的摊销成本的上限为 O(log N),但不限制最坏情况成本 (There might even be a tighter bound on the amortized cost, but O(log N) is the one I know how to prove) (甚至可能对摊销成本有更严格的限制,但 O(log N) 是我知道如何证明的)

Using both together, you get the O(log N) limit on the worst case and the amortized bound improves to O(𝛼(N)), which is effectively constant.将两者结合使用,在最坏情况下得到 O(log N) 限制,并且摊销界限改进为 O(𝛼(N)),这实际上是常数。 In this way the two optimizations are complementary.通过这种方式,这两种优化是互补的。

You are right that there are sequences of operations for which union by rank is not absolutely optimal, but the guarantees are better than what you get without it, and that is what counts.您是对的,有些操作序列按等级联合并不是绝对最优的,但保证比没有它时得到的要好,这才是最重要的。 We don't usually spend effort optimizing the best case performance.我们通常不会花费精力优化最佳案例性能。 We optimize the worst or average cases.我们优化最坏平均情况。

The graph T2 in the question, cannot be formed on the first place if you are using union by rank/weight.如果您按等级/权重使用联合,则问题中的图 T2 无法首先形成。

Just try for yourself with 3 nodes and see if you can come up with graph T2.只需自己尝试使用 3 个节点,看看是否可以得出图 T2。 It's not possible.这是不可能的。 Even graph T1 cannot be formed in the first place.甚至连图 T1 也不能一开始就形成。

You can start with an example of N nodes and if you do union by rank/weight from the beginning , you will actually see that it will give an optimal merge structure( some sequences of operations might not give the best merge, but majority of the times it gives the best results).您可以从 N 个节点的示例开始,如果您从一开始就按等级/权重进​​行联合,您实际上会看到它将提供最佳合并结构(某些操作序列可能不会提供最佳合并,但大多数次它给出了最好的结果)。

In other words union by rank/weight is helping the find method(which in-turn uses path compression for further optimisation) which make find operation almost constant time operation.换句话说,按等级/权重合并有助于查找方法(反过来使用路径压缩进行进一步优化),这使得查找操作几乎是恒定时间操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM