The Union-Find Data Structure in Several Guises

The OCaml library unionFind offers several sequential implementations and one concurrent implementation of the union-find data structure.

All implementations are based on disjoint sets forests, with path compression and a balanced linking method, so as to guarantee good asymptotic complexity: every operation requires a quasi-constant number of accesses to the store.

The linking method used in the sequential algorithms is linking by rank. The linking method used in the concurrent algorithm is linking by random index.

Sequential UF Over Primitive Mutable State

This union-find data structure uses heap-allocated mutable state.

Concurrent UF Over Primitive Mutable State

This union-find data structure uses heap-allocated mutable state. It is thread-safe: the data structure can be used by several threads simultaneously.

Sequential UF Over the Store Library

This union-find data structure is layered on top of the Store library. Its operations explicitly take a store as a parameter, and update it in place. An empty store can be created via the function Store.create. The functions Store.capture and Store.restore can be used to take and restore a snapshot of the entire union-find data structure.

Sequential UF Over a User-Provided Store

This union-find data structure is parameterized over an implementation of stores. Its operations explicitly take a store as a parameter, and update it in place. It is provided by the functor UnionFind.Make. This functor is parameterized over an implementation of stores, described by the signature UnionFind.STORE. Its result signature is also UnionFind.STORE, extended with a union operation that merges two references.

Stores

The functor UnionFind.Make can be applied to many different implementations of stores, such as persistent stores, mutable stores backed by mutable arrays, and more. The following modules provide several different implementations of stores.