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 ...
随机推荐
- PHP中的ob_start用法详解
用PHP的ob_start();控制您的浏览器cache Output Control 函数可以让你自由控制脚本中数据的输出.它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况.输出 ...
- MD5 (摘要加密)
MD5 约定 同样的密码,同样的加密算法,每次加密的结果是不一样 密码方案 方案一:直接 MD5 pwd = pwd.md5String; 非常不安全 方案二 MD5 + 盐 pwd = [pwd s ...
- JavaScript基础整理(2)
接下来的重点是函数.我们知道函数是特殊的对象. 函数作用域和声明提前.JavaScript中没有块级作用域,只有函数作用域:变量在声明它们的函数体以及这个函数体嵌套的任意 函数体内都要定义. func ...
- 配置 nginx server 出现nginx: [emerg] "root" directive is duplicate in /etc/nginx/server/blogs.conf:7
在配置nginx 虚拟机时,执行 sudo /usr/sbin/nginx -t 报下面的错误: nginx: [emerg] nginx: configuration file /etc/nginx ...
- R--基本统计分析方法(包及函数)
摘要:目前经典的统计学分析方法主要有回归分析,Logistic回归,决策树,支持向量机,聚类分析,关联分析,主成分分析,对应分析,因子分析等,那么对于这些经典的分析方法在R中的使用主要有那些程序包及函 ...
- .net利用NPOI导入导出Excel
NPOI在.net中的操作Excel 1.读取 using (FileStream stream = new FileStream(@"c:\客户资料.xls", FileMode ...
- CSU 1328: 近似回文词
省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1 ...
- brew gradle
cat /usr/local/Library/Taps/homebrew/homebrew-versions/gradle221.rb GRADLE_HOME=/Users/temp/gradle22 ...
- HDU 5128 The E-pang Palace(2014广州赛区现场赛B题 计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 解题报告:在一个平面上给出n个点的坐标,用这n个点作为矩形的四个顶点,作两个矩形,要求两个矩形不 ...
- iOS项目目录结构
一. 目前最为流行的目录结构是: <先根据模块后根据功能> 的文件目录结构 优点: 模块分明, 并且开发和维护时方便查阅各个功能 缺点: 可能会出现模块内随意建立文件夹, 导致局部逻辑紊乱 ...