Union-find node type
Instances For
Parent of a union-find node, defaults to self when the node is a root
Equations
- Std.UnionFind.parentD arr i = if h : i < Array.size arr then (Array.get arr { val := i, isLt := h }).parent else i
Instances For
Rank of a union-find node, defaults to 0 when the node is a root
Equations
- Std.UnionFind.rankD arr i = if h : i < Array.size arr then (Array.get arr { val := i, isLt := h }).rank else 0
Instances For
Union-find data structure #
The UnionFind
structure is an implementation of disjoint-set data structure
that uses path compression to make the primary operations run in amortized
nearly linear time. The nodes of a UnionFind
structure s
are natural
numbers smaller than s.size
. The structure associates with a canonical
representative from its equivalence class. The structure can be extended
using the push
operation and equivalence classes can be updated using the
union
operation.
The main operations for UnionFind
are:
empty
/mkEmpty
are used to create a new empty structure.size
returns the size of the data structure.push
adds a new node to a structure, unlinked to any other node.union
links two nodes of the data structure, joining their equivalence classes, and performs path compression.find
returns the canonical representative of a node and updates the data structure using path compression.root
returns the canonical representative of a node without altering the data structure.checkEquiv
checks whether two nodes have the same canonical representative and updates the structure using path compression.
Most use cases should prefer find
over root
to benefit from the speedup from path-compression.
The main operations use Fin s.size
to represent nodes of the union-find structure.
Some alternatives are provided:
unionN
,findN
,rootN
,checkEquivN
useFin n
with a proof thatn = s.size
.union!
,find!
,root!
,checkEquiv!
useNat
and panic when the indices are out of bounds.findD
,rootD
,checkEquivD
useNat
and treat out of bound indices as isolated nodes.
The noncomputable relation UnionFind.Equiv
is provided to use the equivalence relation from a
UnionFind
structure in the context of proofs.
- arr : Array Std.UFNode
Array of union-find nodes
- parentD_lt : ∀ {i : Nat}, i < Array.size self.arr → Std.UnionFind.parentD self.arr i < Array.size self.arr
Validity for parent nodes
- rankD_lt : ∀ {i : Nat}, Std.UnionFind.parentD self.arr i ≠ i → Std.UnionFind.rankD self.arr i < Std.UnionFind.rankD self.arr (Std.UnionFind.parentD self.arr i)
Validity for rank
Instances For
Size of union-find structure.
Equations
- Std.UnionFind.size self = Array.size self.arr
Instances For
Create an empty union-find structure with specific capacity
Equations
- Std.UnionFind.mkEmpty c = { arr := Array.mkEmpty c, parentD_lt := ⋯, rankD_lt := ⋯ }
Instances For
Empty union-find structure
Equations
Instances For
Equations
- Std.UnionFind.instEmptyCollectionUnionFind = { emptyCollection := Std.UnionFind.empty }
Parent of union-find node
Equations
- Std.UnionFind.parent self i = Std.UnionFind.parentD self.arr i
Instances For
Rank of union-find node
Equations
- Std.UnionFind.rank self i = Std.UnionFind.rankD self.arr i
Instances For
Maximum rank of nodes in a union-find structure
Equations
- Std.UnionFind.rankMax self = Array.foldr (fun (x : Std.UFNode) => max x.rank) 0 self.arr (Array.size self.arr) + 1
Instances For
Add a new node to a union-find structure, unlinked with any other nodes
Equations
- Std.UnionFind.push self = { arr := Array.push self.arr { parent := Array.size self.arr, rank := 0 }, parentD_lt := ⋯, rankD_lt := ⋯ }
Instances For
Root of a union-find node.
Equations
- Std.UnionFind.root self x = if h : (Array.get self.arr x).parent = ↑x then x else let_fun this := ⋯; Std.UnionFind.root self { val := (Array.get self.arr x).parent, isLt := ⋯ }
Instances For
Root of a union-find node.
Equations
- Std.UnionFind.rootN self x h = match n, h, x with | .(Std.UnionFind.size self), ⋯, x => Std.UnionFind.root self x
Instances For
Root of a union-find node. Panics if index is out of bounds.
Equations
- Std.UnionFind.root! self x = if h : x < Std.UnionFind.size self then ↑(Std.UnionFind.root self { val := x, isLt := h }) else Std.UnionFind.panicWith x "index out of bounds"
Instances For
Root of a union-find node. Returns input if index is out of bounds.
Equations
- Std.UnionFind.rootD self x = if h : x < Std.UnionFind.size self then ↑(Std.UnionFind.root self { val := x, isLt := h }) else x
Instances For
Auxiliary data structure for find operation
- s : Array Std.UFNode
Array of nodes
- root : Fin n
Index of root node
- size_eq : Array.size self.s = n
Size requirement
Instances For
Auxiliary function for find operation
Equations
- One or more equations did not get rendered due to their size.
Instances For
Find root of a union-find node, updating the structure using path compression.
Equations
- Std.UnionFind.find self x = let r := Std.UnionFind.findAux self x; { fst := { arr := r.s, parentD_lt := ⋯, rankD_lt := ⋯ }, snd := { val := { val := ↑r.root, isLt := ⋯ }, property := ⋯ } }
Instances For
Find root of a union-find node, updating the structure using path compression.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Find root of a union-find node, updating the structure using path compression. Panics if index is out of bounds.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Find root of a union-find node, updating the structure using path compression. Returns inputs unchanged when index is out of bounds.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Link two union-find nodes
Equations
- One or more equations did not get rendered due to their size.
Instances For
Link a union-find node to a root node.
Equations
- Std.UnionFind.link self x y yroot = { arr := Std.UnionFind.linkAux self.arr x y, parentD_lt := ⋯, rankD_lt := ⋯ }
Instances For
Link a union-find node to a root node.
Equations
- Std.UnionFind.linkN self x y yroot h = match n, h, x, y, yroot with | .(Std.UnionFind.size self), ⋯, x, y, yroot => Std.UnionFind.link self x y yroot
Instances For
Link a union-find node to a root node. Panics if either index is out of bounds.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Link two union-find nodes, uniting their respective classes.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Link two union-find nodes, uniting their respective classes.
Equations
- Std.UnionFind.unionN self x y h = match n, h, x, y with | .(Std.UnionFind.size self), ⋯, x, y => Std.UnionFind.union self x y
Instances For
Link two union-find nodes, uniting their respective classes. Panics if either index is out of bounds.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Check whether two union-find nodes are equivalent, updating structure using path compression.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Check whether two union-find nodes are equivalent, updating structure using path compression.
Equations
- Std.UnionFind.checkEquivN self x y h = match n, h, x, y with | .(Std.UnionFind.size self), ⋯, x, y => Std.UnionFind.checkEquiv self x y
Instances For
Check whether two union-find nodes are equivalent, updating structure using path compression. Panics if either index is out of bounds.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Check whether two union-find nodes are equivalent with path compression,
returns x == y
if either index is out of bounds
Equations
- Std.UnionFind.checkEquivD self x y = match Std.UnionFind.findD self x with | (s, x) => match Std.UnionFind.findD s y with | (s, y) => (s, x == y)
Instances For
Equivalence relation from a UnionFind
structure
Equations
- Std.UnionFind.Equiv self a b = (Std.UnionFind.rootD self a = Std.UnionFind.rootD self b)