简体   繁体   中英

Disjoint set with Union by Size and Path Compression; possible for taller tree to become subtree of shorter tree?

Hi this is my first time posting here,

I have been trying to work out a question for studying but haven't been able to figure it out:

We consider the forest implementation of the disjoint-set abstract data type, with Weighted Union by size and Path Compression. Initially, each element is in a one-node tree.

Starting from the above initial state:

  1. give a (short) sequence of UNION and FIND operations in which the last operation is a UNION that causes a taller tree A to become the subtree of a shorter tree B (ie. the height of A is strictly larger than the height of B).

  2. Show the two trees A and B that the last UNION merges

Hint: You can start from n = 9 elements, each one in a one-node tree.


I'm not sure how that would work since the smaller tree always gets merged with the larger tree because of union by size?

Thanks.

I don't want to answer your homework, but this question is old enough that your semester is likely over, and in any case a hint should help enough.

There's a distinction between union by size and union by height , primarily because of path compression. Specifically, path compression can result in very high degree nodes, and thus trees with many nodes but a very short height. For example, these are two trees you can create with union find with path compression:

|T1:   o    (n=5, h=2)
|    /| |\ 
|   o o o o
|    
|T2:   o    (n=4, h=3)
|     /|
|    o o
|      |
|      o

If the next operation is a merge of these two trees, the "union by rank" and "union by height" algorithms would select different parents.

In practice, "union by rank" is usually used. Rank is an upper bound for height which can be updated in constant time, and yields the best asymptotic running time. A web search will yield many good explanations of that algorithm.

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