-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickFind.java
51 lines (43 loc) · 1.05 KB
/
QuickFind.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
package me.stormma.chapter1.uf;
/**
* This {@code QuickFind} class provides some methods to union-find.
* <em>And the time complexity of find method is O(1), time complexity
* of union method is O(n), so call it quick-find</em>
*
* @author stormma
* @date 2017/10/18
*/
public class QuickFind {
private int[] id;
private int count;
public QuickFind(int count) {
this.count = count;
id = new int[count];
// Initialize the component
for (int i = 0; i < count; i++) {
id[i] = i;
}
}
public int count() {
return count;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int find(int p) {
return id[p];
}
public void union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot) {
return;
}
for (int i = 0; i < id.length; i++) {
if (find(i) == pRoot) {
id[i] = qRoot;
}
}
count--;
}
}