-
Notifications
You must be signed in to change notification settings - Fork 1
/
weightedquickunion.go
41 lines (36 loc) · 984 Bytes
/
weightedquickunion.go
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
package uf
// WeightedQuickUnion M union-find ops on a N objects
// worst time complexity is o(N+MlgN)
type WeightedQuickUnion struct {
store []int
sz []int
}
func NewWeightedQuickUnion(n int) *WeightedQuickUnion {
st := make([]int, n)
for i := range st {
st[i] = i
}
return &WeightedQuickUnion{store: st, sz: make([]int, n)}
}
// Find time complexity proportional to depth of i/j, depth is at most lgN
// worst case O(lgN)
func (qu *WeightedQuickUnion) Find(i, j int) bool {
return qu.root(i) == qu.root(j)
}
// Union just like Find, time complexity Worst Case O(lgN)
func (qu *WeightedQuickUnion) Union(i, j int) {
ri, rj := qu.root(i), qu.root(j)
if qu.sz[ri] < qu.sz[rj] {
qu.store[ri] = rj
qu.sz[rj] += qu.sz[ri]
} else {
qu.store[rj] = ri
qu.sz[ri] += qu.sz[rj]
}
}
func (qu *WeightedQuickUnion) root(i int) int {
for qu.store[i] != i {
i = qu.store[i]
}
return i
}