并查集介绍
并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题。
并查集的初始化方法为先让所有的结点自成一个独立的集合,自己作为自己的簇头(是用簇头来标识集合的);若已知两个元素属于同一集合,则将这两个元素所在的集合合并。
并操作:将两个元素所在的集合合并
查操作:找到给定元素的簇头
如下图所示就是unio(a , b)的过程,
此外在为了提高并查集的查询效率,一种很容易想到的做法为让并查集尽可能的矮,因此在查询的过程中,将查到的中间结点的都连到其簇头下面。
案例一:等式方程的可满足性(leetcod990)
问题描述:
给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a!=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。
只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。
示例 1:
输入:["a==b","b!=a"]
输出:false
解释:如果我们指定,a = 1 且 b = 1,那么可以满足第一个方程,但无法满足第二个方程。没有办法分配变量同时满足这两个方程。
示例 2:
输出:["b==a","a==b"]
输入:true
解释:我们可以指定 a = 1 且 b = 1 以满足满足这两个方程。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/satisfiability-of-equality-equations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决方案:
使用并查集求解,先处理等号情况,将等号两边元素所在的集合并到一起;后处理不等号方程:使用查操作,找到不等号两边的元素是否为同一簇,若为同一簇则证明该方程不满足,返回false,若所有的不等号方程都满足,则返回false,实现代码如下:
class Solution {
public static class UnionFindSet{
int[] set;
public UnionFindSet(){
this.set = new int[26];
for(int i = 0; i < set.length; i++){
set[i] = i;
}
}
public void unio(int item1, int item2){
int root1 = find(item1);
int root2 = find(item2);
if(root1 != root2){
set[root2] = root1;
}
}
public int find(int item){ //找到该元素的头结点
int root = item;
while(set[root] != root){
root = set[root];
}
/*优化 把遍历过程中所有的结点都连到root上*/
while(item != root){
int parent = set[item];
set[item] = root;
item = parent;
}
return root;
}
}
public boolean equationsPossible(String[] equations) {
UnionFindSet ufs = new UnionFindSet();
// 等于 并 不等于 查
for(String equa : equations){
if(equa.charAt(1) == '='){
ufs.unio(equa.charAt(0) - 'a', equa.charAt(3) - 'a');
}
}
for(String equa : equations){
if(equa.charAt(1) == '!'){
int root1 = ufs.find(equa.charAt(0) - 'a');
int root2 = ufs.find(equa.charAt(3) - 'a');
// System.out.println(root1 + " " + root2);
if(root1 == root2){
return false;
}
}
}
return true;
}
}
案例二:盆友圈问题(leetcod547)
问题描述:
班上有 N 名学生。其中有些人是朋友,有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈,是指所有朋友的集合。
给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道。你必须输出所有学生中的已知的朋友圈总数。
示例 1:
输入:
[[1,1,0],
[1,1,0],
[0,0,1]]
输出: 2
说明:已知学生0和学生1互为朋友,他们在一个朋友圈。
第2个学生自己在一个朋友圈。所以返回2。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/friend-circles
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决方案:
将每个学生看做一个元素,遍历学生关系矩阵,将是朋友的两个同学所在的集合并到一起,最终返回总的集合数即可。注:由于若M[i][j] = 1,则M[j][i] 也等于1,因此只需要遍历关系矩阵的上三角部分即可。实现代码如下:
class Solution {
public static class UFS{
int[] parent;
public UFS(int N){
parent = new int[N];
for(int i = 0; i < N; i++){
parent[i] = i;
}
}
public int find(int item){
int root = item;
while(parent[root] != root){
root = parent[root];
}
/*一点优化 把遍历过程中所有的结点都连到root上*/
while(item != root){
int temp = parent[item];
parent[item] = root;
item = temp;
}
return root;
}
public void unio(int item1, int item2){
int root1 = find(item1);
int root2 = find(item2);
if(root1 != root2){
parent[root2] = root1;
}
}
}
public int findCircleNum(int[][] M) {
UFS ufs = new UFS(M.length);
for(int i = 0; i < M.length; i++){
for(int j = i + 1; j < M[0].length; j++){
if(M[i][j] == 1){
ufs.unio(i, j);
}
}
}
int result = 0;
for(int i = 0; i < ufs.parent.length; i++){
if(ufs.parent[i] == i){
result++;
}
}
return result;
}
}