This is an implementation of the UnionFind (disjoint-set) data structure, as described, for example, here: http://algs4.cs.princeton.edu/15uf .
The Union()
and Connected()
operations take O(log* N) “log-star” time, which is close to O(1).
$ go get github.com/pietv/unionfind