-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisjointSet.java
More file actions
71 lines (61 loc) · 1.99 KB
/
DisjointSet.java
File metadata and controls
71 lines (61 loc) · 1.99 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
import java.util.*;
class DisjointSet {
List<Integer> parent=new ArrayList<>();
List<Integer> rank= new ArrayList<>();
List<Integer> size= new ArayList<>();
public DisjointSet(int n) {
for (int i = 0; i < n; i++) {
parent.add(i);
rank.add(0);
size.add(1);
}
}
public int findParent(int node){
if(node==parent.get(node))
return node;
int root = findParent(parent.get(node));
parent.set(node, root); // Path compression
return root;
}
public void unionByRank(int u, int v){
int rootU = findParent(u);
int rootV = findParent(v);
if (rootU == rootV) return; // They are already in the same set
// Union by rank
if (rank.get(rootU) < rank.get(rootV)) {
parent.set(rootU, rootV);
} else if (rank.get(rootU) > rank.get(rootV)) {
parent.set(rootV, rootU);
} else {
parent.set(rootV, rootU);
rank.set(rootU, rank.get(rootU) + 1);
}
}
public void unionBySize(int u, int v){
int rootU=findParent(u);
int rootV=findParent(v);
if(rootU == rootV)
return;
else if(size.get(rootU)<size.get(rootV)){
parent.set(rootU, rootV);
size.set(rootV,(size.get(rootU)+size.get(rootV)));
}
else{
parent.set(rootV,rootU);
size.set(rootU,(size.get(rootU)+size.get(rootV)));
}
}
}
class Main {
public static void main(String[] args) {
DisjointSet_Graph ds = new DisjointSet_Graph(5);
ds.unionByRank(0, 1);
ds.unionByRank(1, 2);
ds.unionByRank(3, 4);
System.out.println(ds.findParent(0)); // Output: 0
System.out.println(ds.findParent(1)); // Output: 0
System.out.println(ds.findParent(2)); // Output: 0
System.out.println(ds.findParent(3)); // Output: 3
System.out.println(ds.findParent(4)); // Output: 3
}
}