-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisjointSet.java
39 lines (32 loc) · 1.12 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
import java.util.HashMap;
public class DisjointSet {
private static class Node {
public int parent = Integer.MIN_VALUE, subsetRank = 0;
}
private final HashMap<Integer, Node> graph = new HashMap<Integer, Node>();
private int findParent(int n) {
if (!graph.containsKey(n)) {graph.put(n, new Node());}
Node node = graph.get(n);
if (node.parent == Integer.MIN_VALUE || node.parent == n) {
node.parent = n;
return n;
}
node.parent = findParent(node.parent);
return node.parent;
}
public void connect(int p1, int p2) {
int px = findParent(p1), py = findParent(p2);
if (px != py) {
Node node1 = graph.get(px), node2 = graph.get(py);
if (node1.subsetRank > node2.subsetRank) {node2.parent = p1;}
else if (node1.subsetRank < node2.subsetRank) {node1.parent = p2;}
else {
node2.parent = p1;
node1.subsetRank++;
}
}
}
public boolean connected(int p1, int p2) {
return findParent(p1) == findParent(p2);
}
}