-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisjointSet.java
48 lines (41 loc) · 1.5 KB
/
DisjointSet.java
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
import java.util.*;
public class DisjointSet<V> {
private Map<Vertex<V>, Vertex<V>> p;
private Map<Vertex<V>, Integer> rank;
// Constructs a new set
public DisjointSet() {
this.p = new HashMap<>();
this.rank = new HashMap<>();
}
// Creates a new set whose only member and representative is the given x
// This single node has an initial rank of zero
public void makeSet(Vertex<V> x) {
p.put(x, x);
rank.put(x, 0);
}
// Combines the sets that contain x and y into a new set that is the union of these two sets
public void union(Vertex<V> x, Vertex<V> y) {
link(findSet(x), findSet(y));
}
// For roots that have equal rank, one of the roots will be chosen as the parent
// and its rank will be incremented
// For roots that have unequal rank, the root with the higher rank
// will become the parent of the root with the lower rank and all ranks remain the same
public void link(Vertex<V> x, Vertex<V> y) {
if (rank.get(x) > rank.get(y)) {
p.put(y, x);
} else {
p.put(x, y);
if (rank.get(x).equals(rank.get(y))) {
rank.put(y, rank.get(y) + 1); // y.rank += 1;
}
}
}
// Returns pointer to representative of the unique set containing x
public Vertex<V> findSet(Vertex<V> x) {
if (p.get(x) != x) {
p.put(x, findSet(p.get(x))); // x.p = findSet(x.p);
}
return p.get(x);
}
}