-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.java
More file actions
55 lines (54 loc) · 1.78 KB
/
UnionFind.java
File metadata and controls
55 lines (54 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
public class UnionFind {
int parent[] ;
public UnionFind(int n){
parent = new int[n];
for(int i = 0; i < n; i++){
parent[i] = i;
}
}
public int find(int x){
if(parent[x] != x){
parent[x] = find(parent[x]);
}
return parent[x];
}
public void union(int x, int y){
int rootX = find(x);
int rootY = find(y);
if(rootX != rootY){
parent[rootX] = rootY;
}
}
public static void main(String[] args){
List<List<Integer>> disJointGraph = new ArrayList<>();
for(int i = 0; i < 5; i++){
disJointGraph.add(new ArrayList<>());
}
disJointGraph.get(0).add(1);
disJointGraph.get(1).add(2);
disJointGraph.get(3).add(4);
System.out.println("Graph representation:");
for(int i = 0; i < disJointGraph.size(); i++){
for(int neighbor : disJointGraph.get(i)){
System.out.println(i + " -> " + neighbor);
}
}
System.out.println("Union Find");
UnionFind uf = new UnionFind(disJointGraph.size());
for(int i = 0; i < disJointGraph.size(); i++){
for(int neighbor : disJointGraph.get(i)){
uf.union(i, neighbor);
}
}
// Connecting to different component - uf.union(2, 4);
for(int i = 0; i < disJointGraph.size(); i++){
System.out.println("Node " + i + " has parent: " + uf.find(i));
}
Set<Integer> uniqueParents = new HashSet<>();
for(int i = 0; i < disJointGraph.size(); i++){
uniqueParents.add(uf.find(i));
}
System.out.println("Number of connected components: " + uniqueParents.size());
}
}