https://github.com/theodesp/unionfind
An idiomatic implementation of a weighted Union Find data structure with path compression in Go.
https://github.com/theodesp/unionfind
algorithms data-structures golang union-find
Last synced: 7 days ago
JSON representation
An idiomatic implementation of a weighted Union Find data structure with path compression in Go.
- Host: GitHub
- URL: https://github.com/theodesp/unionfind
- Owner: theodesp
- License: mit
- Created: 2017-06-06T21:47:32.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-07-26T19:37:56.000Z (about 4 years ago)
- Last Synced: 2025-04-22T19:11:31.141Z (6 months ago)
- Topics: algorithms, data-structures, golang, union-find
- Language: Go
- Homepage: https://godoc.org/github.com/theodesp/unionfind
- Size: 6.84 KB
- Stars: 21
- Watchers: 2
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unionfind
An idiomatic implementation of a weighted Union Find data structure with path compression in go.
## Install
`$ go get github.com/theodesp/unionfind`
## Usage
```go
// 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
```
## API
#### `uf := unionfind.New(N)`
Create a new Union Find Structure with size N.
#### `uf.Union(p, q)`
Connects p and q components.
#### `uf.Find(p)`
Attempts to find the Root component of p. Returns -1 if p is out of bounds.
#### `uf.Root(p, q)`
Same as Find.
#### `uf.Connected(p, q)`
Checks if p and q are connected.
## EXTRA
There is also a goroutine safe version of unionfind in the file `safe-unionfind`.
## Licence
MIT - Theo Despoudis