Collections.DisjointSet (collections v0.2.0) View Source

Disjoint set implementation in Elixir

See also: Disjoint Set

Time complexity

  • &find/2 : O(ma(n))
  • &union/2 : O(ma(n))

ma is inverse Ackermann function.

Examples

  iex> ds = DisjointSet.new()
  iex> {x, ds} = DisjointSet.find(ds, 10)
  iex> x
  10
  iex> ds = ds |> DisjointSet.union(10, 20) |> DisjointSet.union(20, 30)
  iex> x = DisjointSet.find(ds, 10) |> elem(0)
  iex> y = DisjointSet.find(ds, 30) |> elem(0)
  iex> x == y
  true

Link to this section Summary

Functions

The Find operation follows the chain of parent from a specified query node x until it reaches a root element. This root element represents the set to which x belongs and may be x itself. Find returns the root element it reaches.

Create an empty disjoint set.

The Union replaces the set containing x and the set containing y with their union.

Link to this section Types

Specs

t() :: %Collections.DisjointSet{
  rank: %{optional(any()) => integer()},
  root: map()
}

Link to this section Functions

Specs

find(t(), any()) :: {any(), t()}

The Find operation follows the chain of parent from a specified query node x until it reaches a root element. This root element represents the set to which x belongs and may be x itself. Find returns the root element it reaches.

Examples

iex> DisjointSet.new()
...>   |> DisjointSet.find(10)
...>   |> elem(0)
10

Specs

new() :: t()

Create an empty disjoint set.

Examples

iex> DisjointSet.new()
%DisjointSet{root: Map.new([]), rank: Map.new([])}
Link to this function

union(disjoint_set, x, y)

View Source

Specs

union(t(), any(), any()) :: t()

The Union replaces the set containing x and the set containing y with their union.

Examples

iex> ds = DisjointSet.new() |> DisjointSet.union(20, 30)
iex> x = ds |> DisjointSet.find(20) |> elem(0)
iex> y = ds |> DisjointSet.find(30) |> elem(0)
iex> x == y
true
iex> x == 40
false