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   Score Explanation

5 5 5 5 2 6 4 5 2 3

Correct 0.20 The id[] array contains a cycle: 6->4->2->5->6
5 3 6 6 6 5 5 8 6 6 
Correct 0.20 Size 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 
Correct 0.20 Height of forest = 4 > lg N = lg(10)
6 6 1 6 6 1 6 6 2 6 
Correct 0.20 6-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 
Correct 0.20 2-1 0-3 2-6
时间: 2024-10-12 18:32:55

AlgorithmsI Exercises: UnionFind的相关文章

AlgorithmsI Exercises: Analysis of Algorithms

Question 1 Suppose that you time a program as a function of N and producethe following table. N seconds------------------- 1024 0.000 2048 0.001 4096 0.007 8192 0.029 16384 0.121 32768 0.519 65536 2.156 131072 9.182 262144 38.784 524288 164.585 10485

UVA - 11987 - Almost Union-Find (又是并查集~)

UVA - 11987 Almost Union-Find Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem A Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to implement som

并查集:Union-Find(1)

Disjoint Sets: 我们都知道Sets(集合)是什么,就是一组非重复元素组成的结构. 先让我们来看一下Disjoint Sets(非相交集合) : Disjoint Sets的意思是一堆集合们,它们相互之间都没有交集.没有交集是指:各个集合之间没有拥有共同.相同的元素.中文称作「分离集」. Disjoint Sets 的性质相当特殊.信息学家仔细观察其特性后,精心设计出一套优雅美观的资料结构,可以快速的做集合运算. 由于每个 Disjoint Sets 指的就是集合们都没有交集,我们就

并查集 (Union-Find Sets)及其应用

定义 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的元素所在的集合合并. 主要操作 初始化 把每个点所在集合初始化为其自身. 通常来说,这个步骤在每次使用该数据结构时只需要执行一次,无论何种实现方式,时间复杂度均为O(N). 查找 查找元素所在的集合,即根节点. 合并 将两个元素所在的集合合并为一个集合. 通常来说,合并之前,应先判断两个元素是否属于

Geeks - Union-Find Algorithm - Detect Cycle in a an Undirected Graph算法

利用Union Find的方法查找图中是否有环. 在于构建一个图数据结构,和一般图的数据结构不同的是这个图是记录了边的图,并在查找过程中不断把边连接起来,形成一个回路. 原文地址: http://www.geeksforgeeks.org/union-find/ #pragma once #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> class

union-find算法

一些概念: 问题的输入是接收一列整数对,其中每个整数都表示一个某种类型的对象.一对整数pq可以理解为p和q是相连的.假设相连是一种等价关系,等价关系能够将对象分为多个等价类. 可以将对象称之为触点 将整数对称为 连接,将等价类称为联通分量,或分量. 当且仅当两个对象是相连的,他们才属于同一个等价类.  问题 编译程序实现,程序从输入中读取了整数对pq时,如果一直的所有整数都不能说明pq是相连的,那么则将这一对整数写入到输出中. Union-find算法的API 构造函数UF()用来初始化N个触点

Geeks Union-Find Algorithm Union By Rank and Path Compression 图环算法

同样是查找一个图是否有环的算法,但是这个算法很牛逼,构造树的时候可以达到O(lgn)时间效率.n代表顶点数 原因是根据需要缩减了树的高度,也叫压缩路径(Path compression),名字很高深,不过其实不难理解,简单来说就是每次查找一个节点的时候,都把这一路径中的所有节点都赋予根节点作为路径. 原文没指出的地方: 也因为需要压缩,所以初始化的时候注意,不能如前面简单实用Union Find的算法那样初始化所有顶点的父母节点为-1,应该是初始化所有节点的父母节点为本身(自己繁殖自己?),然后

算法导论 Exercises 22.5(转载)

Exercises 22.5 - 算法导论.英文第3版 最近看书的同时, 感觉一些练习缺少参考, 所以按部分总结了自己的解答, 也能够强化学习过程. 如有不足或疑问, 欢迎指正. Exercises 22.5-1 How can the number of strongly connected components of a graph change if a new edge is added? 可以将每个强连通组件当作一个顶点, 组成强连通图, 图内顶点数量即强连通组件数量. 如果新增加的边

UVa 11987 Almost Union-Find(支持删除操作的并查集)

传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something similar, but not identical. The data structure you need to write is also a collection of disjoint sets, supporting 3 operations: 1 p q