简体   繁体   English

不相交集数据结构中的联合运算应如何正确实施?

[英]How should Union operation in disjoint-set data structure properly implemented?

There are many different descriptions and examples for the disjoint-set structure available on-line. 在线有许多不同的描述和示例用于不相交集结构。

In some cases, for each set, it stores "rank". 在某些情况下,对于每个集合,它存储“等级”。 When a set is merged into another set, the rank of the former is increased by 1, if they are of the same rank. 当一个集合合并到另一个集合中时,如果前者的等级相同,则前者的等级增加1。

In other cases, for each set, it stores its size. 在其他情况下,对于每个集合,它都存储其大小。 When a set is merged into another set, their sizes are added. 当一个集合合并到另一个集合中时,将添加它们的大小。

Here it stores ranks. 在这里存储行列。

In the wikipedia article , it stores ranks. Wikipedia文章中 ,它存储等级。

In the Cornell University lecture notes , it stores ranks. 在康奈尔大学的讲义中 ,它存储行列。

In the example from "Algorithms", by Sedgewick and Wayne , it stores sizes. Sedgewick和Wayne的“算法” 示例中 ,它存储大小。

Here , it also stores sizes ( main site ). 在此 ,它还存储大小( 主站点 )。

Cormen et al. Cormen等。 write: 写:

The obvious approach would be to make the root of the tree with fewer nodes point to the root of the tree with more nodes. 一种明显的方法是使节点较少的树的根指向节点较多的树的根。 Rather than explicitly keeping track of the size of the subtree rooted at each node, we shall use an approach that eases the analysis. 与其明确地跟踪植根于每个节点的子树的大小,不如使用一种简化分析的方法。 For each node, we maintain a rank, which is an upper bound on the height of the node. 对于每个节点,我们维持一个等级,该等级是节点高度的上限。 In union by rank, we make the root with smaller rank point to the root with larger rank during a UNION operation. 在按等级并集中,我们在UNION操作中将等级较小的根指向等级较大的根。

Which is better / more proper? 哪个更好/更合适?

All the analysis(is?) indicate that both methods provide the optimal O(alpha) complexity, when combined with tree collapsing technique. 所有分析都表明,当与树折叠技术结合使用时,这两种方法都能提供最佳的Oα复杂度。

Then the only implementation specific difference comes from the size that either the size or rank variables take. 然后,唯一的实现特定差异来自大小或等级变量所取的大小。 Size can be upto size_t but rank can be encoded always in three bits. 大小最大为size_t但等级始终可以三位编码。

Occasionally those three bits can be encoded in the unused bits in the data/nodes to be processed leading to better performance (speed and size). 有时,这三个位可以编码为要处理的数据/节点中未使用的位,从而获得更好的性能(速度和大小)。

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

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