AlgorithmsI Exercises: UnionFind

本文详细解析了使用快速查找算法及加权快速合并算法处理并查集问题的过程,并通过具体的步骤展示每次联合操作后标识数组的变化情况。同时,探讨了算法在实际应用中的表现与限制。

Question1 

Give the id[] array that results from the following sequence of 6 unionoperations on a set of 10 items using the quick-find algorithm. 6-3 2-3 5-3 5-1 9-3 3-0 Your answer should be a sequence of 10 integers, separated by whitespace.Recall: our quick-find convention for the union operation p-q is to change id[p](and perhaps some other entries) but not id[q].

Answer: 0 0 0 0 4 0 0 7 8 0

Question Explanation
The correct answer is: 0 0 0 0 4 0 0 7 8 0

Here is the id[] array after each union operation:

0 1 2 3 4 5 6 7 8 9
6-3: 0 1 2 3 4 5 3 7 8 9
2-3: 0 1 3 3 4 5 3 7 8 9
5-3: 0 1 3 3 4 3 3 7 8 9
5-1: 0 1 1 1 4 1 1 7 8 9
9-3: 0 1 1 1 4 1 1 7 8 1
3-0: 0 0 0 0 4 0 0 7 8 0


Question 2

Give the id[] array that results from the following sequence of 9 union
operations on a set of 10 items using the weighted quick-union algorithm from lecture.

7-9 9-4 7-0 8-1 3-7 5-6 5-1 3-8 2-6

Your answer should be a sequence of 10 integers, separated by whitespace.

Recall: when joining two trees of equal size, our weighted quick union convention is to
make the root of the second tree point to the root of the first tree. Also, our weighted
quick union algorithm performs union by size (number of nodes) - not union by height -
and does not do path compression.

Answer: 7 8 7 7 7 7 5 7 5 7
Question Explanation
The correct answer is: 7 8 7 7 7 7 5 7 5 7

Here is the id[] array after each union operation:

0 1 2 3 4 5 6 7 8 9
7-9: 0 1 2 3 4 5 6 7 8 7
9-4: 0 1 2 3 7 5 6 7 8 7
7-0: 7 1 2 3 7 5 6 7 8 7
8-1: 7 8 2 3 7 5 6 7 8 7
3-7: 7 8 2 7 7 5 6 7 8 7
5-6: 7 8 2 7 7 5 5 7 8 7
5-1: 7 8 2 7 7 5 5 7 5 7
3-8: 7 8 2 7 7 7 5 7 5 7
2-6: 7 8 7 7 7 7 5 7 5 7


Question 3

Which of the following id[] array(s) could be the result of running the weighted quick union
algorithm on a set of 10 items? Check all that apply.

Recall that our weighted quick union algorithm uses union by size (number of nodes)
and not union by height.

Answer: Choose the last two.

Your Answer ScoreExplanation

    5 5 5 5 2 6 4 5 2 3 

Correct0.20The id[] array contains a cycle: 6->4->2->5->6
5 3 6 6 6 5 5 8 6 6 
Correct0.20Size of tree rooted at parent of 6 < twice the size of tree rooted at 6
8 8 1 1 2 4 1 1 8 9 
Correct0.20Height of forest = 4 > lg N = lg(10)
6 6 1 6 6 1 6 6 2 6 
Correct0.206-7 0-7 1-5 2-8 5-8 4-7 6-2 7-3 2-9
0 2 2 0 4 5 2 7 8 9 
Correct0.202-1 0-3 2-6

 

 

转载于:https://www.cnblogs.com/anne-vista/p/4837038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值