union_find v0.1.1 UnionFind

UnionFind is a data structure to manipulate disjoint-set. Complexity of operations is O(log n).

Link to this section Summary

Functions

Returns a new union find struct. First, size of every sets is 1

Returns true if x and y belong to the same set

Returns size of a set that x belong to

If the sets that x and y belong to are distinct, the sets are combined

Link to this section Functions

Returns a new union find struct. First, size of every sets is 1.

Examples

iex> uf = UnionFind.new(6)
%UnionFind{tree: [-1, -1, -1, -1, -1, -1]}
Link to this function same?(union_find, x, y)

Returns true if x and y belong to the same set.

Examples

iex> uf = UnionFind.new(6)
%UnionFind{tree: [-1, -1, -1, -1, -1, -1]}
iex> uf = uf |> UnionFind.unite(1, 4)
%UnionFind{tree: [-1, -2, -1, -1, 1, -1]}
iex> uf |> UnionFind.same?(1, 4)
true
iex> uf |> UnionFind.same?(1, 2)
false
Link to this function size(union_find, x)

Returns size of a set that x belong to.

Examples

iex> uf = UnionFind.new(6)
%UnionFind{tree: [-1, -1, -1, -1, -1, -1]}
iex> uf = uf |> UnionFind.unite(1, 4)
%UnionFind{tree: [-1, -2, -1, -1, 1, -1]}
iex> uf |> UnionFind.size(1)
2
iex> uf |> UnionFind.size(3)
1
Link to this function unite(union_find, x, y)

If the sets that x and y belong to are distinct, the sets are combined.

Examples

iex> uf = UnionFind.new(6)
%UnionFind{tree: [-1, -1, -1, -1, -1, -1]}
iex> uf = uf |> UnionFind.unite(1, 4)
%UnionFind{tree: [-1, -2, -1, -1, 1, -1]}