-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjointSet.java
More file actions
50 lines (43 loc) · 1.42 KB
/
Copy pathDisjointSet.java
File metadata and controls
50 lines (43 loc) · 1.42 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
import java.util.HashMap;
public class DisjointSet {
HashMap<String, Node> nodeMap = new HashMap<>();
public void CreateSingletonSet(int i, int j) {// Creates a Singleton Set comprised of only i,j.
String key = Node.CreateKey(i, j);
if (!nodeMap.containsKey(key))
nodeMap.put(key, new Node(key));
}
/* a recursive aux method to assist Search method below */
private Node find(Node node) {
if (node != node.next) {
node.next = find(node.next);
}
return node.next;
}
/*
* Returns null if i,j not in any of the disjointed sets,
* otherwise returns the root of the tree its a part of,
* and makes all itself and all of its ancestors point directly at the root.
*/
public Node Search(int i, int j) {
String key = Node.CreateKey(i, j);
if (!nodeMap.containsKey(key))
return null;
return find(nodeMap.get(key));
}
public Node Union(int i1, int j1, int i2, int j2) {
Node root1 = Search(i1, j1);
Node root2 = Search(i2, j2);
if (root1 == null || root2 == null)
return null;
if (root1 == root2)
return root1;
if (root1.rank < root2.rank) {
root1.next = root2;
return root2;
}
if (root2.rank == root1.rank)
root1.rank++;
root2.next = root1;
return root1;
}
}