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. HBase命令(三) -- 增删改查

    新增 //语法:put <table>,<rowkey>,<family:column>,<value>,<timestamp> //新增或 ...

  2. Log4Net使用方法

    项目里都会用到日志记录..特别是在本地跑的欢畅..一上服务器就嗝屁的时候..日志会帮你大忙.. Log4net算是一种应用的最广泛的日志记录方式了..下面来简略的说下他的用法.. 先下载log4net ...

  3. JBoss7 安装配置

    一.下载安装 1.下载地址: http://www.jboss.org/jbossas/downloads ,下载Certified Java EE 6 Full Profile版本. 2.解压 jb ...

  4. ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets(转)

    jqwidgets.js: 是一个功能完整的框架,它具有专业的可触摸的jQuery插件.主题.输入验证.拖放插件.数据适配器,内置WAI-ARIA(无障碍网页应用)可访问性.国际化和MVVM模式支持. ...

  5. JAVA浅析字节流与字符流

    [概括] 字节流是通用的,既可以操作图片又可以操作文本,但一般都用于操作图片.字符流是基于字节流的,因为字符流内部融合编码表,所以用来操作文本. 1.在字节输入流中能根据文件的大小来开辟数组空间 Fi ...

  6. Properties类的使用方法

    它提供了几个主要的方法: 1. getProperty ( String key),用指定的键在此属性列表中搜索属性.也就是通过参数 key ,得到 key 所对应的 value. 2. load ( ...

  7. Supervisor 安装与配置

    Supervisor是一个进程监控程序. 满足的需求是:我现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...

  8. [百度地图] MultiZMap 修改使用;

    MultiZMap修改说明 MultiZMap 是基于百度地图API 封装的一些常用功能类库:主要以prototype方式实现:并且实现了一些辅助的功能,比如添加 Marker 功能,事件管理等: 以 ...

  9. 清北暑假模拟day1 爱

    /* 水题 */ #include<iostream> #include<cstdio> #include<string> #include<cstring& ...

  10. js操作json与字符串相互转换

    字符串转对象(strJSON代表json字符串) var obj = eval(strJSON); var obj = strJSON.parseJSON(); var obj = JSON.pars ...