简体   繁体   English

通过大小和路径压缩与联盟进行脱节设置; 高树可能成为矮树的子树吗?

[英]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). 给出一个(短)UNION和FIND操作序列,其中最后一个操作是一个UNION,它使一个较高的树A成为一个较短树B的子树(即A的高度严格大于B的高度) 。

  2. Show the two trees A and B that the last UNION merges 显示最后一个UNION合并的两个树A和B.

Hint: You can start from n = 9 elements, each one in a one-node tree. 提示:您可以从n = 9个元素开始,每个元素都在单节点树中。


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: 例如,这些是您可以使用带有路径压缩的union find创建的两个树:

|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. Rank是高度的上限,可以在恒定时间内更新,并产生最佳的渐近运行时间。 A web search will yield many good explanations of that algorithm. 网络搜索将产生该算法的许多好的解释。

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

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