MAKE-SET.x/ creates a new set whose only member (and thus representative)

is x. Since the sets are disjoint, we require that x not already be in some other

set.

UNION.x; y/ unites the dynamic sets that contain x and y, say Sx and Sy, into a

new set that is the union of these two sets. We assume that the two sets are disjoint

prior to the operation. The representative of the resulting set is any member

of Sx [ Sy, although many implementations of UNION specifically choose the

representative of either Sx or Sy as the new representative. Since we require

the sets in the collection to be disjoint, conceptually we destroy sets Sx and Sy,

removing them from the collection S. In practice, we often absorb the elements

of one of the sets into the other set.

FIND-SET.x/ returns a pointer to the representative of the (unique) set containing

x.

linklist implement

In the worst case, the above implementation of the UNION procedure requires an

average of ‚.n/ time per call because we may be appending a longer list onto

a shorter list; we must update the pointer to the set object for each member of

the longer list. Suppose instead that each list also includes the length of the list

(which we can easily maintain) and that we always append the shorter list onto the

longer, breaking ties arbitrarily. With this simple weighted-union heuristic, a single

UNION operation can still take _.n/ time if both sets have _.n/ members. As

the following theorem shows, however, a sequence of m MAKE-SET, UNION, and

FIND-SET operations, n of which are MAKE-SET operations, takes O.m C n lg n/

time.

Tree implement

Heuristics to improve the running time

So far, we have not improved on the linked-list implementation. A sequence of

n 1 UNION operations may create a tree that is just a linear chain of n nodes. By

using two heuristics, however, we can achieve a running time that is almost linear

in the total number of operations m.

The first heuristic, union by rank, is similar to the weighted-union heuristic we

used with the linked-list representation. The obvious approach would be to make

the root of the tree with fewer nodes point to the root of the tree with more nodes.

Rather than explicitly keeping track of the size of the subtree rooted at each node,

we shall use an approach that eases the analysis. For each node, we maintain a

rank, which is an upper bound on the height of the node. In union by rank, we

make the root with smaller rank point to the root with larger rank during a UNION

operation.

The second heuristic, path compression, is also quite simple and highly effective.

As shown in Figure 21.5, we use it during FIND-SET operations to make each

node on the find path point directly to the root. Path compression does not change

any ranks(more nodes linked to the root will cause much possibility to find it).

When we use both union by rank and path compression, the worst-case running
time is O.m ˛.n//, where ˛.n/ is a very slowly growing function, which we define
in Section 21.4. In any conceivable application of a disjoint-set data structure,
˛.n/ 4; thus, we can view the running time as linear in m in all practical situations.
Strictly speaking, however, it is superlinear. In Section 21.4, we prove this
upper bound.

 package disjoint_sets;
// there have two ways,one is the linkedlist,the other is the tree,use the tree here
public class disjoint_set {
private static class Node{
private Node p;
private int rank;
private String name;
public Node(String na){
p = this; rank = 0;name = na;
}
}
public static void union(Node x,Node y){
link(findset(x),findset(y));
}
public static void link(Node x,Node y){
if(x.rank > y.rank){
y.p = x;
}
else if(y.rank > x.rank){
x.p = y;
}
else{
y.p = x;
x.rank = x.rank + 1;
}
}
public static Node findset(Node x){
if(x != x.p){
x.p = findset(x.p); //path compression
}
return x.p;
}
public static void print(Node x){ System.out.println(x.name);
if(x != x.p){
x = x.p;
print(x);
}
return;
}
public static void main(String[] args) {
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
Node d = new Node("d");
union(a,b);
union(b,c);
union(a,d);
print(d); } }

disjoint set的更多相关文章

  1. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

  3. 数据结构与算法分析 – Disjoint Set(并查集)

    什么是并查集?并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题. 并查集的主要操作1.合并两个不相交集合2.判断两个元素是否属于同一集合 主要操作的解释 ...

  4. 并查集(Disjoint Set)

    在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂, ...

  5. Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  6. LeetCode-Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  7. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

  8. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  9. 352. Data Stream as Disjoint Intervals

    Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...

  10. 数据结构 之 并查集(Disjoint Set)

    一.并查集的概念:     首先,为了引出并查集,先介绍几个概念:     1.等价关系(Equivalent Relation)     自反性.对称性.传递性.     如果a和b存在等价关系,记 ...

随机推荐

  1. Oarcle之视图

    视图 什么是视图? (在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列,就像一个真实的表.视图中的字段就是来自一个或多个数据库中的真实的表中的字段.我们可以向视图添加 S ...

  2. Flutter 控件之 Routes 和 Navigator. [PopupRoute]

    一个 App 通常会有多个界面,每个界面实现不同的功能,并在多个界面之间跳转.在 Flutter 中多个界面的跳转是通过 Navigator 来实现的. 在 Flutter 中定义了一个 Overla ...

  3. PHP保留两位小数并且四舍五入及不四舍五入的方法

    php保留两位小数并且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数并且不四舍五入 $num = ...

  4. Docker Swarm volume 数据持久化

    Docker Swarm volume 数据持久化 volume 是将宿主级的目录映射到容器中,以实现数据持久化. 可以用两种方式来实现: volume 默认模式:工作节点宿主机数据同步到容器内. v ...

  5. react 表格扩展与编辑

    项目里有个需求是点击表格某行的工料机,显示对应定额下的工料机的表格数据,并能对两个表格进行增删改查,效果如下: 代码如下: // 引入 Component 组件 import React, { Com ...

  6. freeswitch 修改系统最大呼叫量

    freeswitch 中有2个参数限制系统的最大呼叫量,以防止资源耗尽. max_session控制最大并发数.默认值1000:sps控制最大每秒呼叫量,默认值30 命令临时生效:fsctl max_ ...

  7. 批处理学习之Bat命令——获取当前盘符、当前目录、上级目录

    命令 当前盘符:%~d0 当前路径:%cd% 当前执行命令行:%0 当前bat文件路径:%~dp0 当前bat文件短路径:%~sdp0 测试 下载testBatPath.bat测试文件,双击.bat运 ...

  8. SQLyog 字体设置

    新公司连接数据库的工具是SQLyog,我以前使用的一直是Navicat,抱着多学个工具也不亏的想法我也安装了试下,但是SQLyog那紧凑的字体对于近视200度的我来说看着着实难受,在询问了公司前辈之后 ...

  9. LINQ之路16:LINQ Operators之集合运算符、Zip操作符、转换方法、生成器方法

    本篇将是关于LINQ Operators的最后一篇,包括:集合运算符(Set Operators).Zip操作符.转换方法(Conversion Methods).生成器方法(Generation M ...

  10. 安卓外派(Android外派)提供安卓程序员外派业务(北京动点,可签合同)

    北京动点飞扬长年提供安卓工程师外派业务. 平均技术情况如下: 1.2~3年以上Android平台开发经验 2.熟练掌握java技术,熟悉面向对象编程设计 3.熟悉Android应用开发框架及Activ ...