数据结构--并查集(Disjoint-Set)

1. 并查集

  • 并查集是一种树型的数据结构
  • 用于处理一些不相交集合(Disjoint Sets)的合并及查询问题

2. 操作

2.1 初始化

把每个点所在集合初始化为其自身,时间复杂度均为O(N),可用数组,哈希表等结构来实现

for(int i = 0; i < n; i++) 
	father[i] = i;

2.2 查询

查找元素所在的集合(找一个代表),即根节点
有的时候,树的高度太高,压缩树的高度,直接让底层节点的father指向root,称之路径压缩

在这里插入图片描述

int uniFind(int x)
{
    if(x == father[x]) 
    	return x;
    return father[x] = uniFind(father[x]);//等式为路径压缩操作
}

or 循环

int uniFind(int a)//循环+路径压缩
	{
        int origin = a;
		while(a != f[a])
			a = f[a];
		return f[origin] = a;//路径压缩
	}

2.3 合并

将两个元素所在的集合合并为一个集合
合并之前,先判断两个元素是否属于同一集合,用上面的uniFind操作实现

在这里插入图片描述

void merge(int x, int y)
{
    int fatherx = uniFind(x);
    int fathery = uniFind(y);
    if(fatherx != fathery) 
    	father[fatherx] = fathery;
}

2.4 孤立

father[a] = a;

相关题目 LeetCode 5941. 找出知晓秘密的所有专家(并查集)

3. 完整代码

/**
 * @Description: 并查集
 * @Author: michael ming
 * @Date: 2020/4/3 16:14
 * @Modified by: 
 * @Website: https://michael.blog.csdn.net/
 */
#include<bits/stdc++.h>
using namespace std;
const int n = 10;
int father[n] = {0,1,2,3,4,5,6,7,8,9};
void init()
{
    for(int i = 0; i < n; i++)
        father[i] = i;
};
int uniFind(int x)
{
    if(x == father[x])
        return x;
    return father[x] = uniFind(father[x]);//等式为路径压缩操作
}
void merge(int x, int y)
{
    int fatherx = uniFind(x);
    int fathery = uniFind(y);
    if(fatherx != fathery)
        father[fatherx] = fathery;
}
int main()
{
    init();
    merge(1,2);
    cout << "1的代表" << uniFind(1) << endl;
    cout << "2的代表" << uniFind(2) << endl;
    cout << "0的代表" << uniFind(0) << endl;
    merge(0,1);
    cout << "0的代表" << uniFind(0) << endl;
    return 0;
}

运行结果:

1的代表2
2的代表2
0的代表0
0的代表2

4. 相关题目

LeetCode 261. 以图判树(全部连通+边数=V-1)
LeetCode 305. 岛屿数量 II(并查集)
LeetCode 323. 无向图中连通分量的数目(并查集)
LeetCode 684. 冗余连接(并查集)
LeetCode 685. 冗余连接 II(并查集)
LeetCode 721. 账户合并(并查集)(字符串合并)
LeetCode 737. 句子相似性 II(并查集)
LeetCode 803. 打砖块(并查集 + 带size记录)*
LeetCode 839. 相似字符串组(并查集)
LeetCode 886. 可能的二分法(着色DFS/BFS/拓展并查集)
LeetCode 947. 移除最多的同行或同列石头(并查集)
LeetCode 990. 等式方程的可满足性(并查集)
LeetCode 959. 由斜杠划分区域(并查集)
LeetCode 1061. 按字典序排列最小的等效字符串(并查集)
LeetCode 1101. 彼此熟识的最早时间(排序+并查集)
LeetCode 1135. 最低成本联通所有城市(最小生成树+排序+并查集)
LeetCode 1202. 交换字符串中的元素(并查集)
LeetCode 1319. 连通网络的操作次数(BFS/DFS/并查集)
LeetCode 1489. 找到最小生成树里的关键边和伪关键边(并查集+kruskal最小生成树)
LeetCode 5510. 保证图可完全遍历(并查集)
LeetCode 5632. 检查边长度限制的路径是否存在(排序+并查集)
LeetCode 5650. 执行交换操作后的最小汉明距离(并查集)
程序员面试金典 - 面试题 17.07. 婴儿名字(并查集)
LeetCode 2076. 处理含限制条件的好友请求(并查集)
LeetCode 5941. 找出知晓秘密的所有专家(并查集)

5. 参考


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

  • 30
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
并查集Disjoint Set)是一种数据结构,用于解决集合的合并和查找问题。在Python中可以使用类来实现并查集。引用展示了一个简单的并查集类的代码实现,其中包括了初始化集合、查找集合、合并集合和判断两个元素是否在同一个集合中的方法。另外,引用和展示了对并查集代码的优化,包括路径压缩和按秩合并等技巧,以提高并查集的效率。 在Python中使用并查集可以解决一些实际问题,如求解岛屿个数、朋友圈等。通过将问题转化为集合的合并和查找操作,可以使用并查集来高效地解决这些问题。 所以,如果你需要在Python中实现并查集,可以参考以上的代码实现和优化方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python 数据结构与算法——并查集](https://blog.csdn.net/itnerd/article/details/103916115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [并查集Python版](https://blog.csdn.net/XZ2585458279/article/details/127274576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael阿明

如果可以,请点赞留言支持我哦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值