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 ...
随机推荐
- xss漏洞挖掘小结
xss漏洞挖掘小结 最近,在挖掘xss的漏洞,感觉xss真的不是想象的那样简单,难怪会成为一类漏洞,我们从防的角度来讲讲xss漏洞的挖掘方法: 1.过滤 一般服务器端都是采用这种方式来防御xss攻击, ...
- Nginx重新编译添加模块
原文链接:http://www.linuxidc.com/Linux/2013-06/86438.htm 找到安装nginx的源码根目录,如果没有的话下载新的源码 http://nginx.org t ...
- Linux 高精確的時序(sleep, usleep,nanosleep) from:http://blog.sina.com.cn/s/blog_533ab41c0100htae.html
Linux 高精確的時序(sleep, usleep,nanosleep) (2010-04-14 17:18:26) 转载▼ 标签: 杂谈 分类: linux 首先, 我会说不保证你在使用者模式 ( ...
- 优化PHP程序的方法(温故知新)
1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化 ...
- oracle中的nvl(), nvl2()函数
nvl()函数是oracle/plpgsql中的一个函数,格式为:nvl(string1, replace_with) 功能:如果string1 位null,那么nvl()函数返回replace_wi ...
- CxImage
启动项目的时候显示此时 百度“无法启动程序cximage.lib” 得到http://tieba.baidu.com/p/1935208210把第二项设为启动项即可 为什么设置第二项为启动项呢 因为h ...
- Web 前端
全栈的定义是什么? 如果 前端开发/后端开发/部署/运维 都能hold住就算full stack, 我现在都overflow stack了, 需求/架构/开发/项目管理/运维 都做. 单开发这块, ...
- 7 天玩转 ASP.NET MVC — 第 1 天
0. 前言正如标题「7 天玩儿转 ASP.NET MVC」所言,这是个系列文章,所以将会向大家陆续推出 7 篇.设想一下,一天一篇,你将从一个愉快的周一开始阅读,然后在周末成为一个 ASP.NET M ...
- Linux下查看文件内容的命令
查看文件内容的命令: cat 由第一行开始显示内容,并将所有内容输出 tac 从最后一行倒序显示内容,并将所有内容输出 more 根据窗口大小,一页一页的现实文件内容 less ...
- MongoDB MapReduce学习笔记
http://cnodejs.org/topic/51a8a9ed555d34c67831fb8b http://garyli.iteye.com/blog/2079158 MapReduce应该算是 ...