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的更多相关文章

  1. [慢查优化]建索引时注意字段选择性 & 范围查询注意组合索引的字段顺序

    文章转自:http://www.cnblogs.com/zhengyun_ustc/p/slowquery2.html 写在前面的话: 之前曾说过"不要求每个人一定理解 联表查询(join/ ...

  2. [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优 ...

  3. 8.2.1.4 Index Merge Optimization 索引合并优化:

    8.2.1.4 Index Merge Optimization 索引合并优化: 索引合并方法是用于检索记录 使用多个 范围扫描和合并它们的结果集到一起 mysql> show index fr ...

  4. MySQL Index Merge Optimization

    Index Merge用在通过一些range scans得到检索数据行和合并成一个整体.合并可以通过 unions,intersections,或者unions-intersection运用在底层的扫 ...

  5. [Swift]LeetCode990. 等式方程的可满足性 | Satisfiability of Equality Equations

    Given an array equations of strings that represent relationships between variables, each string equa ...

  6. mysql 调优 来自5.6版本官方手册

    注意:下面示例中的key1和key2代表两个索引,key_part1和key_part2代表一个复合索引的第一列和第二列.non_key代表非索引列. 优化SQL语句 where语句优化: mysql ...

  7. Mysql优化(出自官方文档) - 第一篇(SQL优化系列)

    Mysql优化(出自官方文档) - 第一篇 目录 Mysql优化(出自官方文档) - 第一篇 1 WHERE Clause Optimization 2 Range Optimization Skip ...

  8. Algorithm partI 第2节课 Union−Find

    发展一个有效算法的具体(一般)过程: union-find用来解决dynamic connectivity,下面主要讲quick find和quick union及其应用和改进. 基本操作:find/ ...

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

    相同是查找一个图是否有环的算法,可是这个算法非常牛逼,构造树的时候能够达到O(lgn)时间效率.n代表顶点数 原因是依据须要缩减了树的高度,也叫压缩路径(Path compression),名字非常高 ...

  10. 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 ...

随机推荐

  1. 使用BLADE构建c++工程管理

    使用BLADE构建c++工程管理 字数764 阅读2753 评论2 喜欢4 一. c++工程依赖管理 之前在百度一直使用comake2构建c++项目,十分方便.免去了手写Makefile的痛苦,很多项 ...

  2. @Autowired与@Resource的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  3. python 多线程就这么简单

    原文地址:http://www.cnblogs.com/fnng/p/3670789.html

  4. Python之路【目录】 2

    http://www.cnblogs.com/wupeiqi/articles/4938499.html

  5. QT实现贪吃蛇

    board.h #ifndef BOARD_H #define BOARD_H #define MAX_X 40 #define MAX_Y 30 #define NORMAL_LABEL 0//普通 ...

  6. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  7. 单选框的回显c:if

    <input type="radio" name="sex" value="boy" <c:if test="${te ...

  8. [CentOS]安装命令行终端Terminator工具

    摘要 Terminator是一款跨平台的终端工具,使用的是 GPL 许可证,提供了很多高级的功能.它没有 Guake 和 Yakuake 那样光鲜,但绝对是一款重型武器.它提供的功能包括界面分块,将自 ...

  9. 如何在JDK1.8中愉快地处理日期和时间

    如何在JDK1.8中愉快地处理日期和时间 JDK1.8新增了LocalDate和LocalTime接口,为什么要搞一套全新的处理日期和时间的API?因为旧的java.util.Date实在是太难用了. ...

  10. 我常用的eclipse快捷键

    重写父类方法:右键点击, 进source,进override/implement 单行注释(或多行注释) Ctrl+/ 文档注释:shift+alt+J 整块注释: Ctrl+shift+/ 取消整块 ...