Union and Find(并查集)

The Functionality

有时候,我们需要把相似的元素 group到一起。而每一组的元素是没有区别的,i.e. evey member of a group can be a representative (怎么感觉和数学里面的 quotient set串戏了)。举个例子:
Odd = {1,3,5,…}
Even = {2,4,6…}
在这里面,Odd的 representative可以是任何奇数。
如果我们用一颗颗tree来表示这些元素,那就差不多是 Union-and-Find Forest了。
!在这里插入图片描述
Union-Find Data Structure 的操作有:

  1. Find(x): returns the root / cluster-id of the element x
  2. Union(x, y): merge two clusters / trees

Complexity Analysis

显然,如果我们只是单纯地把元素一个一个像吃烧烤一样串起来,Find(x)的复杂度将是 O(n)。但是如果我们采用两种优化办法,则可以达到均摊复杂度 O(1) *

  1. Path Compression: make the tree flat.
  2. Union by rank: merge the cluster with lower rank to the higher one.

具体来说, path compression就是在 call Find(x)的时候,顺便将元素的 parent改为 root。上图如果使用 Find(8),就会将 8的 parent改为1.
Union by rank的目的也是为了尽可能减小 Find的耗时。Ranks 可以看做是对混乱度的一个measure

Pseudo Code

class UnionFindSet {
	function UnionFindSet(n) {
		parents = [1,2,...,n]
		ranks = [0,...0] (n zeros)
		}
	
	function Find(x) {
		if (x!= parents[x] ) {
			parents[x] = Find(parents[x]) 
			}
		return parents[x]
	}
	
	function Union(x, y) {
		px, py = Find(x), Find(y)
		if (ranks[px] > ranks[py]) parents[py] = px
		if (ranks[py] > ranks[px]) parents[px] = py
		// 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值