algorithms/fundamentals/exrecises/union_find/quick_union.go

45 lines
678 B
Go
Raw Permalink Normal View History

2021-11-22 23:39:33 +02:00
package unionfind
type quickUnion struct {
id []int
count int
}
func NewQuickUnion(count int) UnionFind {
id := make([]int, count)
for i := range id {
id[i] = i
}
return &quickUnion{id, count}
}
func (qf *quickUnion) Find(site int) int {
for site != qf.id[site] {
site = qf.id[site]
}
return site
}
func (qf *quickUnion) Union(aSite, bSite int) {
aComponent := qf.Find(aSite)
bComponent := qf.Find(bSite)
if aComponent == bComponent {
return
}
qf.id[aComponent] = bComponent
qf.count--
}
func (qf *quickUnion) Connected(aSite, bSite int) bool {
return qf.Find(aSite) == qf.Find(bSite)
}
func (qf *quickUnion) Count() int {
return qf.count
}