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

By using the graph, we can easily find whether two components are connected or not, if there is no such graph, how do we know whether two components are connected or not?
Answer: For all connected components, we set their "root" to be the same.
So, we use an array to record the root of each component, if their roots are the same, return true, otherwise, return false.
Example:




So, how to implement it?

How is the time complexity of the operations?

So, Union operation is kind of expensive, can we decrease it?
Yes, instead of making all the components use the same root id, we can just set the parent id of root component in one set to the root id of another set. (Why we cannot just set the parent id of the root component in one set to the id of the connected component in another set???)
Example:


But the problem is when we check whether two components have the same root, the worst case time complexity is O(n). n refers to the size of the components, and this happens when we have a thin tree (all components are in the same tree, but this tree has no branches.)

Time complexity:

So, the approach above cannot decrease the union operation time complexity, rather, it increases the find operation time complexity.
If we have a closer look, we can find the reason why quick-union approach is not performing well is because the height of the tree could be very tall. So, the question becomes how to decrease the height of th tree?
There are two approaches:
First, when we marge two trees, the root of the smaller tree (with less # of components) will be connected to the root of larger tree.


The benefit of doing this can decrease the height of the tree.


Another approach is called path compression. The idea is every time when we get the root of a component, we always set its parent id to the root id.
Example:



So, this approach can also decrease the height of the tree.
Reference:https://www.cs.duke.edu/courses/cps100e/fall09/notes/UnionFind.pdf (普林斯顿的这位老爷爷讲得真的很清楚,youtube上可以收到他的视频。)
Union-Find Algorithm的更多相关文章
- [慢查优化]建索引时注意字段选择性 & 范围查询注意组合索引的字段顺序
文章转自:http://www.cnblogs.com/zhengyun_ustc/p/slowquery2.html 写在前面的话: 之前曾说过"不要求每个人一定理解 联表查询(join/ ...
- [MySQL Reference Manual] 8 优化
8.优化 8.优化 8.1 优化概述 8.2 优化SQL语句 8.2.1 优化SELECT语句 8.2.1.1 SELECT语句的速度 8.2.1.2 WHERE子句优化 8.2.1.3 Range优 ...
- 8.2.1.4 Index Merge Optimization 索引合并优化:
8.2.1.4 Index Merge Optimization 索引合并优化: 索引合并方法是用于检索记录 使用多个 范围扫描和合并它们的结果集到一起 mysql> show index fr ...
- MySQL Index Merge Optimization
Index Merge用在通过一些range scans得到检索数据行和合并成一个整体.合并可以通过 unions,intersections,或者unions-intersection运用在底层的扫 ...
- [Swift]LeetCode990. 等式方程的可满足性 | Satisfiability of Equality Equations
Given an array equations of strings that represent relationships between variables, each string equa ...
- mysql 调优 来自5.6版本官方手册
注意:下面示例中的key1和key2代表两个索引,key_part1和key_part2代表一个复合索引的第一列和第二列.non_key代表非索引列. 优化SQL语句 where语句优化: mysql ...
- Mysql优化(出自官方文档) - 第一篇(SQL优化系列)
Mysql优化(出自官方文档) - 第一篇 目录 Mysql优化(出自官方文档) - 第一篇 1 WHERE Clause Optimization 2 Range Optimization Skip ...
- Algorithm partI 第2节课 Union−Find
发展一个有效算法的具体(一般)过程: union-find用来解决dynamic connectivity,下面主要讲quick find和quick union及其应用和改进. 基本操作:find/ ...
- Geeks Union-Find Algorithm Union By Rank and Path Compression 图环算法
相同是查找一个图是否有环的算法,可是这个算法非常牛逼,构造树的时候能够达到O(lgn)时间效率.n代表顶点数 原因是依据须要缩减了树的高度,也叫压缩路径(Path compression),名字非常高 ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- Autofac.Integration.Mvc分析
Autofac.Integration.Mvc static ILifetimeScope LifetimeScope { get { return (ILifetimeScope)HttpConte ...
- JNDI全面总结
JNDI全面总结原理: 在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...
- thinkphp succes error跳转模板 设置
执行成功 换成自己想要的模块 下面的路径是默认的模板\ThinkPHP\Tpl\dispatch_jump.tpl D:\wamp\www\ThinkPHP\Tpl\dispatch_jump.tpl ...
- EF: Raw SQL Queries
Raw SQL Queries Entity Framework allows you to query using LINQ with your entity classes. However, t ...
- php 利用ffmpeg将amr转MP3
原文地址: http://www.jianshu.com/p/895d5568ce70 http://www.cnblogs.com/wanghetao/p/3386311.html http://w ...
- 几种 Java 序列化方案的性能比较
较结果: create ser deser total size +dfl java-built-in 62 5608 29649 35257 889 514 hessian 65 3812 6708 ...
- CLion注册码算法逆向分析实录
Link is : http://www.cnblogs.com/foreach-break/p/CLion_License_Fake_Crack.html
- IOS表情存入MYSQL数据库失败
从 MySQL 5.5.3 开始,MySQL 支持一种 utf8mb4 的字符集,这个字符集能够支持 4 字节的 UTF8 编码的字符. utf8mb4 字符集能够完美地向下兼容 utf8 字符串.在 ...
- 【干货理解】理解javascript中实现MVC的原理
理解javascript中的MVC MVC模式是软件工程中一种软件架构模式,一般把软件模式分为三部分,模型(Model)+视图(View)+控制器(Controller); 模型:模型用于封装与应用程 ...
- CSS3:flex布局应用
想把先前的整理的东西贴出来,怎奈总是有额外事情发生,额,教训电脑要离水杯远点~~ 推荐一本书,<编写可维护的Javascript>这是Nicbolas C.Zakas写的,他的<Ja ...