简体   繁体   English

不相交集算法中按大小联合与按等级联合

[英]union by size vs union by rank in disjoint-set algorithm

The question in short: does union by rank work only in some circumstance?简而言之,问题是:等级联盟仅在某些情况下有效吗?

For example, the input is: (1, 2), (3,4), (3,5) , after we run a disjoint-set algorithm on them:例如,输入是: (1, 2), (3,4), (3,5) ,在我们对它们运行不相交集算法之后:

one of the correct output should be (1,2), (3,4,5) .正确的 output 之一应该是(1,2), (3,4,5) And the roots (the representatives of the 2 sets) are 2 and 4 , respectively.根(2 个集合的代表)分别是24 The forest should look like this:森林应该是这样的:

2                 4
 \               / \
  1             3   5

Here comes the problem, if I need to sort the output by its size in descending order(so larger size comes first):问题来了,如果我需要按 output 的大小按降序排序(所以首先是更大的大小):

If I use union by size (simply its number of descendants including the node itself), size(2) == 2 , size(4) == 3 , so the right set should come first.如果我按大小使用联合(只是它的后代数量,包括节点本身), size(2) == 2size(4) == 3 ,所以应该首先出现正确的集合。 All good;都好;

If I use union by rank (an upper bound for its height), rank(2) == 1 , rank(4) == 1 , left and right set are equal and this is not what I want.如果我按等级使用联合(其高度的上限), rank(2) == 1rank(4) == 1 ,左右集是相等的,这不是我想要的。 So union by rank fails this case, NO good !所以按等级联合在这种情况下失败了,不好

Why is that?这是为什么? Does that mean union by rank has its limitations?这是否意味着按等级联合有其局限性? If so, under what circumstances we can use union by rank ?如果是这样,在什么情况下我们可以使用union by rank Is union by size more general?按大小联合是否更普遍?

The point of both the union-by-rank heuristic and the union-by-size heuristic is to minimize the expected running time of the Merge operation. union-by-rank 启发式和 union-by-size 启发式的重点是最小化Merge操作的预期运行时间。 They are not intended for any other purpose.它们不用于任何其他目的。

Your use case apparently involves sorting the sets by size, an unusual but not unheard-of thing to want.您的用例显然涉及按大小对集合进行排序,这是一种不寻常但并非闻所未闻的事情。 If you use the union-by-size heuristic, you can do this without additional work.如果您使用 union-by-size 启发式,则无需额外工作即可完成此操作。 (That doesn't redound to the asymptotic complexity, as the size of each set could be trivially maintained even while doing union-by-rank.) So for this use case it sounds like union-by-size is more convenient for you. (这并不会影响渐近复杂性,因为即使在按等级进行联合时,每个集合的大小也可以轻松维护。)因此,对于这个用例来说,按大小联合听起来对您来说更方便。 But both of them work in the context they were designed for.但它们都在它们设计的环境中工作

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

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