并查集算法:
1、用集合中的某个元素来代表这个集合,则该元素称为此集合的代表元;
2 、一个集合内的所有元素组织成以代表元为根的树形结构;
3 、对于每一个元素 x,pre[x] 存放 x 在树形结构中的父亲节点(如果 x 是根节点,则令pre[x] = x);
4 、对于查找操作,假设需要确定 x 所在的的集合,也就是确定集合的代表元。可以沿着pre[x]不断在树形结构中向上移动,直到到达根节点。
因此,基于这样的特性,并查集的主要用途有以下两点:
1、维护无向图的连通性(判断两个点是否在同一连通块内,或增加一条边后是否会产生环);
2、用在求解最小生成树的Kruskal算法里。
一般来说,一个并查集对应三个操作:
1、初始化( Init()函数 )
2、查找函数( findParent()函数 )
3、合并集合函数( union()函数 )
在学习Kruscal算法的时候,了解到该算法的大致流程是对图中的边进行排序,每次取出权值最小的边加入图中,且加入之后图中不能出现环,此时就需要运用到并查集算法来判断加入一条边后是否成环。(PS:查阅网上资料后,发现并没有比较完整的答案或者可以直接应用到算法题的解析,于是参考网上大佬的思路,自己实现了一个Kruscal算法)。代码如下:
题目:给你n个点,m条边,后面为边的权值,实现kruskal算法。
如:
6
10
0 1 6
0 2 1
0 3 5
2 1 5
2 3 5
2 4 5
2 5 4
1 4 3
4 5 6
5 3 2
//定义边
class Edge{
int x;
int y;
int weight;
Edge(){};
public Edge(int x, int y, int weight) {
this.x = x;
this.y = y;
this.weight = weight;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Edge[] edges = new Edge[m];
for(int i = 0 ; i < m ; i++){
edges[i] = new Edge();
edges[i].x = sc.nextInt();
edges[i].y = sc.nextInt();
edges[i].weight = sc.nextInt();
}
sc.close();
List<Edge> result = function(edges,n);
for(Edge edge : result){
System.out.print(edge.x);
System.out.print(" " + edge.y);
System.out.println(" " + edge.weight);
}
}
private static List<Edge> function(Edge[] edges,int n){
//小顶堆对边的权值进行排序
PriorityQueue<Edge> queue = new PriorityQueue(new Comparator<Edge>() {
@Override
public int compare(Edge o1, Edge o2) {
return o1.weight - o2.weight;
}
});
for(Edge edge : edges){
queue.offer(edge);
}
UnionCollection uc = new UnionCollection(n);
List<Edge> result = new ArrayList<>();
while(!queue.isEmpty()){
Edge edge = queue.poll();
int node1 = edge.x;
int node2 = edge.y;
if(uc.union(node1,node2)){
result.add(edge);
}
}
return result;
}
//并查集模板
class UnionCollection{
int n ;
int[] size;
int[] parent;
int count;
UnionCollection(int n){
this.n = n;
this.size = new int[n];
this.parent = new int[n];
this.count = n;
Arrays.fill(size,1);
for(int i = 0 ; i < n ; i++){
parent[i] = i;
}
}
public int findParent(int x){
if(parent[x] == x){
return x;
}else{
parent[x] = findParent(parent[x]);
return parent[x];
}
}
public boolean union(int x , int y){
int parentx = findParent(x);
int parenty = findParent(y);
if(parentx == parenty){
return false;
}else{
if(size[parentx] > size[parenty]){
int temp = parentx;
parentx = parenty;
parenty = temp;
}
parent[parentx] = parenty;
count--;
size[parenty] += size[parentx];
return true;
}
}
public boolean isConnected(int x , int y){
int parentx = findParent(x);
int parenty = findParent(y);
return parenty == parentx;
}
}
附上两道leetcode上关于并查集的练习题:
上面并查集算法只适用于判断无向图是否成环?那如果题目要求是有向图呢?
判断有向图是否成环的方法:
①深度优先遍历
例子:给你一个字符串,“1->4,2->5,3->6,3->7,4->8,5->8,5->9,6->9,6->11,7->11,8->12,9->12,9->13,10->13,10->14,11->10,11->14” ,节点范围在0< x < 200之间。
问该图是否存在环?(由leetcode周赛顺丰科技提供)
class Solution {
boolean isDAG = true;
public boolean hasCycle(String graph) {
//用深度优先遍历算法判断是否成环
String[] s = graph.split(",");
int n = s.length;
int[][] edges = new int[n][2];
for(int i = 0 ; i < n ; i++){
String temp = s[i];
String[] strs = temp.split("->");
edges[i][0] = Integer.parseInt(strs[0]);
edges[i][1] = Integer.parseInt(strs[1]);
}
UnionCollection union = new UnionCollection(200);
for(int i = 0 ; i < n ; i++){
union.add(edges[i]);
}
for(int i = 0 ; i < 200 ; i++){
union.DFS(i);
if(!isDAG){
break;
}
}
return isDAG == false;
}
class UnionCollection{
int[][] graph;
int[] color;
int size;
UnionCollection(int n){
graph = new int[n][n];
color = new int[n];
size = n;
}
public void add(int[] edge){
graph[edge[0]][edge[1]] = 1;
}
public void DFS(int i){
//结点i变为访问过的状态
color[i] = 1;
for(int j=0;j < size;j++){
//如果当前结点有指向的结点
if(graph[i][j] != 0){
//并且已经被访问过
if(color[j] == 1){
isDAG = false;//有环
break;
}else if(color[j] == -1){
//当前结点后边的结点都被访问过,直接跳至下一个结点
continue;
}else{
DFS(j);//否则递归访问
}
}
}
//遍历过所有相连的结点后,把本节点标记为-1
color[i] = -1;
}
}
}