blob: 45f8ba7be1812c8bb513477e34b75988e829ee5c (
plain)
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
|
/*
* Weighted Quick Union algorithm
*
* Adds weight to the quick union.
*
* Find: Check if p and q have the same root
*
* Union. Merge trees, but the merge is done depending on the size of each tree.
* The smaller one will be merged into the larger one.
*
* The downside is we need to keep a new extra array to keep track of the
* number of elements on each node.
*
*/
public class WeightedQuickUnionUF {
private int[] id;
private int[] sz;
/* Constructor */
public WeightedQuickUnionUF(int N) {
id = new int[N];
sz = new int[N];
for (int i = 0; i < N; i++)
id[i] = i;
/* Every node starts with a single item, itself */
for (int i = 0; i < N; i++)
sz[i] = 1;
}
/* Return root of 'i' */
private int root(int i) {
while (i != id[i]) {
/* Compress paths, by pointing the current node to the
* root of its parent */
id[i] = id[id[i]];
i = id[i];
}
return i;
}
/* Are the objects connected */
public boolean connected(int p, int q) {
return root(p) == root(q);
}
/* Connect two objects */
public void union(int p, int q) {
int proot = root(p);
int qroot = root(q);
if (sz[proot] < sz[qroot]) {
id[proot] = qroot;
sz[proot] += sz[qroot];
} else {
id[qroot] = proot;
sz[qroot] += sz[proot];
}
}
}
|