1. 并查集原理
在一些应用问题中,需要将n个不同的元素划分成一些不相交的集合。开始时,每个元素自成一个单元素集合,然后按一定的规律将归于同一组元素的集合合并。在此过程中要反复用到查询某一个元素归属于哪个集合的运算。适合于描述这类问题的抽象数据类型称为并查集(union-findset)
比如:公司全国招10人,西安招4人,成都3人,武汉3人,10人来自不同的地区,每个人都是一个独立的小团体,现给这些学生编号{0,1,2,3,4,5,6,7,8,9},给以下数组用来存储小集体,数组中的数字代表:该小集体中有成员的个数(负号下文解释)
每个地方的人自发组成小分队一起上路,于是:
西安小分队s1={0,6,7,8},成都小分队s2={1,4,9},武汉小分队s3={2,3,5},就相互认识了,10个人形成了三个小团体,假设有三个群主0,1,2担任队长,负责大家的出行
渐渐每个小分队成员互相熟悉,成为了一个朋友圈
从上图可以看出,编号6,7,8同学属于0号分队,该分队有4人(包含队长0)编号4和9属于1号小分队,该小分队有3人(包含队长1),编号为3和5的同学属于2号小分队,该小分队有3人(包含队长1)
仔细观察数组内变化,可以得出下结论:
1.数组的下标对应集合中元素的编号
2.数组中如果为负数,那就是树的根,绝对值是集合中元素个数
3.数组中如果为非负数,就是双亲的下标
一段时间后,8号和1号走在了一起,两个小圈子互相介绍,最后成了一个小圈子
现在0号集合有7个人,2号集合有3个人,总共有两个朋友圈
通过以上例子可知,并查集一般可以解决以下问题:
1.查找元素属于哪个集合
沿着数组表示树形关系一直往上找到根,树中元素为负数的位置
2.查看两个元素是否属于同一个集合
沿着数组树形关系一直往上找到根,跟相同表示在同一个集合,否则不在
3.将两个集合归并成一个集合
将两个集合中的元素合并
讲一个集合名称改成另一个集合的名称
4.集合的个数
遍历数组,元素为负数的个数是集合的个数
2. 并查集实现
#pragma once
#include <vector>
#include <map>
#include <iostream>
// 通过名字找人
template <class T, T a = 30>
class FindSet
{
public:
FindSet(const T* ary, size_t n)
{
for (size_t i = 0; i < n; i++)
{
// 建立映射
_ary.push_back(ary[i]);
_map[ary[i]] = i;
}
}
private:
std::vector<T> _ary; // 保存值,编号找人
std::map<T, int> _map; // 记录映射,人找编号
};
class UnionFindSet
{
public:
UnionFindSet(int size)
:_set(size, -1)
{}
size_t FindRoot(int x)
{
int root = x;
while (_set[root] >= 0)
{
root = _set[root];
}
// 路径压缩
while (_set[x] >= 0)
{
int parent = _set[x];
_set[x] = root;
x = parent;
}
return root;
}
// 合并
void Union(int x1, int x2)
{
int root1 = FindRoot(x1);
int root2 = FindRoot(x2);
// x1已经与x2在同一个集合
if (root1 != root2)
{
// 数据量小合大
if (abs(_set[root1]) < abs(_set[root2]))
{
std::swap(root1, root2);
}
// 将两个集合中元素合并
_set[root1] += _set[root2];
// 将其中一个集合名称改变成另外一个
_set[root2] = root1;
}
}
bool InSet(int x1, int x2)
{
return FindRoot(x1) == FindRoot(x2);
}
// 数组中负数的个数,即为集合的个数
size_t SetCount() const
{
int count = 0;
for (auto ch : _set)
{
if (ch < 0)
{
count++;
}
}
return count;
}
void Print()
{
for (auto ch : _set)
{
std::cout << ch << " ";
}
std::cout << std::endl;
}
private:
std::vector<int> _set;
};
3. 并查集应用
1.省份数量
思路
这个题考察并查集的应用,创建一个数组保存关系,遍历给的数组,不在一个集合的合并,最后返回并查数组的数量
全
class Solution {
public:
int findCircleNum(vector<vector<int>>& isConnected) {
vector<int> ufs(isConnected.size(), -1);
auto findroot = [&](int root)
{
while (ufs[root] >= 0)
{
root = ufs[root];
}
return root;
};
for (int i = 0; i < isConnected.size(); i++)
{
for (int j =0 ; j < isConnected[i].size(); j++)
{
if (isConnected[i][j] == 1)
{
// 合并集合
int root1 = findroot(i);
int root2 = findroot(j);
if (root1 != root2)
{
ufs[root1] += ufs[root2];
ufs[root2] = root1;
}
}
}
}
int count = 0;
for (auto ch : ufs)
{
if (ch < 0)
{
count++;
}
}
return count;
}
};
2.等式方程的可满足性
思路
根据等式关系,为26个字母建立映射,使用并查集,如果是等号就在同一个集合。如果是不等号,但两个字母在同一个集合,就不满足方程了,返回false
全
class Solution {
public:
bool equationsPossible(vector<string>& equations) {
// 字母建立映射
vector<int> ufs(26, -1);
auto findroot = [&](int root) {
while (ufs[root] >= 0) {
root = ufs[root];
}
return root;
};
// 相等的加入集合
for (auto ch : equations) {
if (ch[1] == '=') {
// 合并集合
int root1 = findroot(ch[0] - 'a');
int root2 = findroot(ch[3] - 'a');
if (root1 != root2) {
ufs[root1] += ufs[root2];
ufs[root2] = root1;
}
}
}
// 不相等的判断相悖
for (auto ch : equations) {
if (ch[1] == '!') {
int root1 = findroot(ch[0] - 'a');
int root2 = findroot(ch[3] - 'a');
if (root1 == root2) {
return false;
}
}
}
return true;
}
};
3. 并查集优化
小和大
两个树合并的时候,让数据量小和到大的里面
// 数据量小合大
if (abs(_set[root1]) < abs(_set[root2]))
{
std::swap(root1, root2);
}
路径压缩
如果数据量大不断合并后孩子节点过多,找跟的时候就会很慢。可以压缩一下路径
在查找跟的时候发现层数很多,间隔层很多,可以直接把这个节点连到他找到的根上,也可以把路径上所有节点都直接连到根,这样层数就得到了减少。是在不断查找的过程中压缩的路径
UnionFindSet ufs(10);
ufs.Union(8, 9);
ufs.Union(7, 8);
ufs.Union(6, 7);
ufs.Union(5, 6);
ufs.Union(4, 5);
对于上面的案例会出现类似单支的情况
// 路径压缩
while (_set[x] >= 0)
{
int parent = _set[x];
_set[x] = root;
x = parent;
}
查找6和9压缩之后