Alias of Batteries.panicWith
.
Panic with a specific default value v
.
Equations
Instances For
Union-find node type
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.
Array of union-find nodes
Validity for parent nodes
- rankD_lt {i : Nat} : parentD self.arr i ≠ i → rankD self.arr i < rankD self.arr (parentD self.arr i)
Validity for rank
Instances For
Create an empty union-find structure with specific capacity
Equations
- Batteries.UnionFind.mkEmpty c = { arr := Array.mkEmpty c, parentD_lt := ⋯, rankD_lt := ⋯ }
Instances For
Empty union-find structure
Instances For
Equations
- Batteries.UnionFind.instEmptyCollection = { emptyCollection := Batteries.UnionFind.empty }
Parent of union-find node
Equations
- self.parent i = Batteries.UnionFind.parentD self.arr i
Instances For
Rank of union-find node
Equations
- self.rank i = Batteries.UnionFind.rankD self.arr i
Instances For
Maximum rank of nodes in a union-find structure
Equations
- self.rankMax = Array.foldr (fun (x : Batteries.UFNode) => max x.rank) 0 self.arr + 1
Instances For
Find root of a union-find node, updating the structure using path compression.
Equations
Instances For
Find root of a union-find node, updating the structure using path compression. Panics if index is out of bounds.
Equations
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
Instances For
Link a union-find node to a root node. Panics if either index is out of bounds.
Equations
Instances For
Link two union-find nodes, uniting their respective classes.
Equations
Instances For
Link two union-find nodes, uniting their respective classes. Panics if either index is out of bounds.
Equations
Instances For
Check whether two union-find nodes are equivalent, updating structure using path compression.
Equations
Instances For
Check whether two union-find nodes are equivalent, updating structure using path compression.
Equations
- self.checkEquivN x_2 y_2 ⋯ = self.checkEquiv x_2 y_2
Instances For
Check whether two union-find nodes are equivalent, updating structure using path compression. Panics if either index is out of bounds.
Equations
- self.checkEquiv! x y = if h : x < self.size ∧ y < self.size then self.checkEquiv ⟨x, ⋯⟩ ⟨y, ⋯⟩ else Batteries.panicWith (self, false) "index out of bounds"
Instances For
Check whether two union-find nodes are equivalent with path compression,
returns x == y
if either index is out of bounds