forked from Tiwarishashwat/Java-Plus-DSA-Placement-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjointSet.java
More file actions
88 lines (75 loc) · 2.63 KB
/
DisjointSet.java
File metadata and controls
88 lines (75 loc) · 2.63 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class DisjointSet {
int parent[];
int rank[];
int size[];
int components;
DisjointSet(int nodes){ //0 based (5) 0 to 4
this.parent = new int[nodes];
this.rank = new int[nodes];
this.size = new int[nodes];
this.components = nodes;
for(int i=0;i<nodes;i++){
this.parent[i] = i;
this.rank[i] = 0;
this.size[i] = 1;
}
}
public int findRootParent(int node){
if(node == parent[node]){
return node;
}
parent[node] = findRootParent(parent[node]);
return parent[node];
}
public void unionByRank(int node1, int node2){
//1. find the root parent
int rootParent1 = findRootParent(node1);
int rootParent2 = findRootParent(node2);
if(rootParent1==rootParent2){
return;
}
components--;
// 2, union of components
if(rank[rootParent1]<rank[rootParent2]){
parent[rootParent1] = rootParent2;
}else if(rank[rootParent2] < rank[rootParent1]){
parent[rootParent2] = rootParent1;
}else{
parent[rootParent2] = rootParent1;
rank[rootParent1]++;
}
}
public void unionBySize(int node1, int node2){
//1. find the root parent
int rootParent1 = findRootParent(node1);
int rootParent2 = findRootParent(node2);
if(rootParent1==rootParent2){
return;
}
components--;
// 2, union of components
if(size[rootParent1]<size[rootParent2]){
parent[rootParent1] = rootParent2;
size[rootParent2] += size[rootParent1];
}else {
parent[rootParent2] = rootParent1;
size[rootParent1] += size[rootParent2];
}
}
public static void main(String[] args) {
// union by rank
DisjointSet dsu1 = new DisjointSet(4);
System.out.println("total components -> " + dsu1.components);
System.out.println(dsu1.findRootParent(0) == dsu1.findRootParent(3));
dsu.unionByRank(0,3);
System.out.println("total components -> " + dsu1.components);
System.out.println(dsu1.findRootParent(0) == dsu1.findRootParent(3));
// union by size
DisjointSet dsu = new DisjointSet(4);
System.out.println("total components -> " + dsu.components);
System.out.println(dsu.findRootParent(0) == dsu.findRootParent(3));
dsu.unionBySize(0,3);
System.out.println("total components -> " + dsu.components);
System.out.println(dsu.findRootParent(0) == dsu.findRootParent(3));
}
}