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

AlgorithmsI Exercises: UnionFind的更多相关文章

  1. AlgorithmsI Exercises: Analysis of Algorithms

    Question 1 Suppose that you time a program as a function of N and producethe following table. N seco ...

  2. Union-Find 检测无向图有无环路算法

    不相交集合数据结构(Disjoint-set data structure)是一种用于跟踪集合被分割成多个不相交的子集合的数据结构,每个集合通过一个代表来标识,代表即集合中的某个成员. Union-F ...

  3. UVA - 11987 Almost Union-Find[并查集 删除]

    UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...

  4. 并查集(union-find)算法

    动态连通性 . 假设程序读入一个整数对p q,如果所有已知的所有整数对都不能说明p和q是相连的,那么将这一整数对写到输出中,如果已知的数据可以说明p和q是相连的,那么程序忽略p q继续读入下一整数对. ...

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

    传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...

  6. 有一种算法叫做“Union-Find”?

    前言: 不少搞IT的朋友听到“算法”时总是觉得它太难,太高大上了.今天,跟大伙儿分享一个比较俗气,但是却非常高效实用的算法,如标题所示Union-Find,是研究关于动态连通性的问题.不保证我能清晰的 ...

  7. Union-Find Algorithm

    Union-Find Algrithm is used to check whether two components are connected or not. Examples: By using ...

  8. 并查集 Union-Find

    并查集能做什么? 1.连接两个对象; 2.查询两个对象是否在一个集合中,或者说两个对象是否是连接在一起的. 并查集有什么应用? 1. Percolation问题. 2. 无向图连通子图个数 3. 最近 ...

  9. 最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind

    最小支撑树树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind 最小支撑树树 前几节中介绍的算法都是针对无权图的,本节将介绍带权图的最小 ...

随机推荐

  1. (转载)linux那点事儿(上)

    原文地址:http://www.cnblogs.com/fnng/archive/2012/03/19/2407162.html 本文只是转载供自己学习之用 本文算是学linux的学习笔记吧!其实li ...

  2. [转] CSS transition

    https://css-tricks.com/almanac/properties/t/transition/ The transition property is a shorthand prope ...

  3. [转]Flex 布局教程:语法篇

    网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中 ...

  4. linux中echo的用法 分类: 学习笔记 linux ubuntu 2015-07-14 14:27 21人阅读 评论(0) 收藏

    1.echo命令我们常用的选项有两个,一个是-n,表示输出之后不换行,另外一个是-e,表示对于转义字符按相应的方式处理,如果不加-e那么对于转义字符会按普通字符处理. 2.echo输出时的转义字符 \ ...

  5. MVC+MEF+UnitOfWork+EF架构,网站速度慢的原因总结!(附加ANTS Memory Profiler简单用法)

    (最近使用内存分析工具ANTS Memory Profiler,以及其他网友提供的意见发现最终导致内存泄漏的就是MEF,在此特地更新下,与大家分享!最下面红色字体) 最近参考使用了郭明峰的一套架构来做 ...

  6. iBatis 的条件查询

    之类以传入ID进行举例 Student.xml 在里面设置一个ID的标志位,设置类型 <select id="selectAllStudentByid" parameterC ...

  7. CTE-递归[2]

    在此之前写过一个CTE的递归,取出了所有的子节点,基本上可以满足大多数的需求,这里我们来延伸一下:首先我们回顾下原来的场景 图片的上半部分递归查出某个节点的所有子节点,这个我们已经通过CTE实现了,可 ...

  8. Vijos1675 NOI2005 聪聪和可可 记忆化搜索

    简单题,结果因为理解错题意懵逼了好久…… moveTo[x][y]表示聪聪在节点x,可可在节点y时,聪聪下一步应到达哪一个节点 dp[x][y]表示聪聪在节点x,可可在节点y,且轮到可可行动时,所需时 ...

  9. SGU 101.Domino (欧拉路)

    时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个   方形,两边 ...

  10. IO流基础

    IO流,也称为数据流,用于处理设备之间的数据传输. JAVA对数据的操作就是通过流的方式,而流分为两种:字符流,字节流 字符流: 可以内部制定码表,处理文字很方便,字符流里的基类是Reader,Wri ...