项目作者: theodesp

项目描述 :
An idiomatic implementation of a weighted Union Find data structure with path compression in Go.
高级语言: Go
项目地址: git://github.com/theodesp/unionfind.git
创建时间: 2017-06-06T21:47:32Z
项目社区:https://github.com/theodesp/unionfind

开源协议:MIT License

下载


unionfind

An idiomatic implementation of a weighted Union Find data structure with path compression in go.

Install

$ go get github.com/theodesp/unionfind

Usage

  1. // Initialize with size
  2. uf := unionfind.New(10)
  3. // Union a,b connects components at index a and b
  4. uf.Union(1, 2)
  5. uf.Union(2, 3)
  6. uf.Union(5, 6)
  7. uf.Union(4, 6)
  8. fmt.PrintLn(uf.Find(2)) // Prints 1
  9. fmt.PrintLn(uf.Connected(1, 2)) // Prints true
  10. 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