-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeightedQuickUnion.java
55 lines (46 loc) · 1.35 KB
/
WeightedQuickUnion.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
package com.datastructure.DisjointSets;
public class WeightedQuickUnion {
private final int[] parent;
private final int[] size;
public WeightedQuickUnion(int n) {
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
public static void main(String[] args) {
WeightedQuickUnion wqu = new WeightedQuickUnion(5);
wqu.union(0, 4);
System.out.println(wqu.isConnected(0, 4));
}
public int root(int p) {
if (p < 0 || p >= parent.length)
throw new IllegalArgumentException("Index " + p + " is not valid.");
while (p != parent[p]) p = parent[p];
return p;
}
// path compress version
public int rootPathCompress(int i) {
while (parent[i] != i) i = parent[parent[i]];
return i;
}
public boolean isConnected(int p, int q) {
return root(p) == root(q);
}
// link the root of small tree to the root of larger tree
// update the size array;
public void union(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) return;
if (size[i] < size[j]) {
parent[i] = j;
size[j] += size[i];
} else {
parent[j] = i;
size[i] += size[j];
}
}
}