forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisjointSet.java
135 lines (112 loc) · 3.44 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.dataStructures;
import java.io.Serializable;
import java.util.*;
/**
* An implementation of disjoint-set, represented by rooted trees.
* <p>
* Actually, the instance of {@link DisjointSet} is a disjoint-set forests.
*
* <p>
* Disjoint-set operations:
* <p>
* 1. quickly unites two sets into a new set, requiring O(1) time.
* <p>
* 2. quickly query two elements whether contained in the same set, requiring about O(1) time.
*
*/
public class DisjointSet<T> implements Serializable {
private static final long serialVersionUID = 3134700471905625636L;
private Map<T, Node<T>> nodeMap = new HashMap<>();
/**
* Add an element to the disjoint-set forests as a set.
*/
public void makeSet(T element) {
checkNotNull(element, "element");
nodeMap.putIfAbsent(element, new Node<>());
}
/**
* Unites the set that contains left and the set that contains right
* into a new set with the union-by-rank heuristic.
* <p>
* Rank is an upper bound on the height of node.
*/
public void union(T left, T right) {
checkNotNull(left, "element");
checkNotNull(right, "element");
Node<T> leftNode = nodeMap.get(left),
rightNode = nodeMap.get(right);
if (leftNode == null) {
throw new NoSuchElementException(left.toString());
}
if (rightNode == null) {
throw new NoSuchElementException(right.toString());
}
Node<T> leftSet = findSet(leftNode),
rightSet = findSet(rightNode);
if (leftSet == rightSet) {
return;
}
if (leftSet.rank < rightSet.rank) {
leftSet.parent = rightSet;
} else {
rightSet.parent = leftSet;
if (leftSet.rank == rightSet.rank) {
leftSet.rank++;
}
}
}
/**
* Query two elements whether contained in the same set.
*/
public boolean isConnected(T left, T right) {
if (left == null || right == null) {
return false;
}
Node<T> leftNode = nodeMap.get(left);
if (leftNode == null) {
return false;
}
Node<T> rightNode = nodeMap.get(right);
if (rightNode == null) {
return false;
}
if (leftNode == rightNode) {
return true;
}
return findSet(leftNode) == findSet(rightNode);
}
public Collection<Set<T>> toSets() {
Map<Node<T>, Set<T>> setMap = new HashMap<>();
for (Map.Entry<T, Node<T>> entry : nodeMap.entrySet()) {
setMap.computeIfAbsent(findSet(entry.getValue()), k -> new HashSet<>())
.add(entry.getKey());
}
return setMap.values();
}
public void show() {
toSets().forEach(System.out::println);
}
/**
* Find the set that contains the node, actual is the first node of the set.
* <p>
* Backtracking with path compression.
*/
private Node<T> findSet(Node<T> node) {
if (node != node.parent) {
node.parent = findSet(node.parent);
}
return node.parent;
}
private static void checkNotNull(Object obj, String msg) {
if (obj == null) {
throw new NullPointerException(msg + " must be not null");
}
}
static class Node<T> {
int rank;
Node<T> parent;
Node() {
parent = this;
}
}
}