An idiomatic implementation of a weighted Union Find data structure with path compression in go.
$ go get github.com/theodesp/unionfind
// Initialize with size
uf := unionfind.New(10)
// Union a,b connects components at index a and b
uf.Union(1, 2)
uf.Union(2, 3)
uf.Union(5, 6)
uf.Union(4, 6)
fmt.PrintLn(uf.Find(2)) // Prints 1
fmt.PrintLn(uf.Connected(1, 2)) // Prints true
fmt.PrintLn(uf.Connected(1, 3)) // Prints false
Create a new Union Find Structure with size N.
Connects p and q components.
Attempts to find the Root component of p. Returns -1 if p is out of bounds.
Same as Find.
Checks if p and q are connected.
There is also a goroutine safe version of unionfind in the file safe-unionfind
.
MIT - Theo Despoudis