简体   繁体   中英

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.

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.

In the Cornell University lecture notes , it stores ranks.

In the example from "Algorithms", by Sedgewick and Wayne , it stores sizes.

Here , it also stores sizes ( main site ).

Cormen et al. 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.

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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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