不相交以特殊方式设定?

时间:2016-05-15 18:35:45

标签: algorithm data-structures tree time-complexity disjoint-sets

用树实现Disjoint Data结构。在此数据结构makeset()中创建一个包含一个元素的集合,merge(i, j)合并两个集合ij的树,使得高度较低的树成为子节点第二棵树的根。如果我们以随机方式执行n makeset()次操作和n-1 merge()操作,然后执行一次查找操作。在最坏的情况下,这种查找操作的成本是多少?

I) O(n)
II) O(1)
III) O(n log n)
IV) O(log n)

答案:IV。

  

任何人都可以提到作者得到这个解决方案的好建议吗?

1 个答案:

答案 0 :(得分:1)

O(log n)find仅在您使用 union by rank (也称为加权联合)时才为真。当我们使用这种优化时,我们总是将具有较低等级的树放置在具有较高等级的树的根之下。如果两者具有相同的等级,我们任意选择,但将结果树的等级增加一。这给出了树的深度上的O(log n)。我们可以通过显示 i 级别以下的节点(相当于位于> = i 的树中)的节点位于的树中来证明这一点至少2个 i 节点(这与显示大小为 n 的树具有log n 深度的树相同)。这可以通过归纳轻松完成。

Induction hypothesis: tree size is >= 2^j for j < i.
Case i == 0: the node is the root, size is 1 = 2^0.
Case i + 1: the length of a path is i + 1 if it was i and the tree was then placed underneath
            another tree. By the induction hypothesis, it was in a tree of size >= 2^i at
            that time. It is being placed under another tree, which by our merge rules means
            it has at least rank i as well, and therefore also had >= 2^i nodes. The new tree
            therefor has >= 2^i + 2^i = 2^(i + 1) nodes.