Kruskal's Minimum Spanning Forest Algorithm on the Word RAM #
This module implements Kruskal's greedy minimum spanning forest and minimum spanning tree algorithm
([Kru56]; [CLRS09], Section 23.2, MST-KRUSKAL) as a deterministic program on
Cslib.WordRAM.Basis.standard, charging every memory load, store, address calculation, edge-weight
comparison, parent-pointer traversal, union-by-rank update, loop test, and final halt.
Given a weighted undirected multigraph es : List (WeightedEdge n ℕ) (m = es.length) encoded in
standard Word-RAM input layout State.init (encode es) ([n, m, u₀, v₀, w₀, …]):
- Stable edge sort. The program copies the
medge weights into a key array and runs the stable key-indexed merge sortCslib.Algorithms.WordRAM.MergeSort.mergeSortPerm, producing an index permutationpermwithperm.filterMap (es[·]?) = sortEdges es(Cslib.Algorithms.WordRAM.MergeSort.filterMap_getElem?_eq_mergeSort). The edge triples are not moved: the scan reads edgeperm[s]at steps. - Union-by-rank disjoint-set forest. The program initializes
parent[0..n)andrank[0..n)and scans the edges in sorted order. EachfindRootfollows at most⌊log₂ n⌋parent pointers, because ranks strictly increase along parent pointers and2 ^ rank[r]is at most the size of the class of a rootr([CLRS09], Lemma 21.4 for union by rank). An edge whose endpoints have distinct roots is appended to the output and its roots are linked by rank.
Consequently kruskalProgram outputs encodeEdges (kruskalEdges es) via
Cslib.WordRAM.ExecutesList within
52 * m * ⌈log₂ m⌉ + 16 * m * ⌊log₂ n⌋ + 69 * m + 7 * n + 34 instructions at any word width w
satisfying Fits w (9 * m + 2 * n + 4) (note that InputFits w (encode es) for Data.Bounded w /
Runs.bounded_init additionally requires ∀ e ∈ es, Fits w e.weight, as witnessed by
inputFits_encode), so the pure spanning-forest, minimum-spanning-tree, and SimpleGraph
theorems of CslibFrontier.Algorithms.Lean.Kruskal.Basic and
CslibFrontier.Algorithms.Lean.Kruskal.SimpleGraph apply to its output.
Main definitions #
Cslib.Algorithms.WordRAM.Kruskal.encodeEdges: flat stride-3[u₀, v₀, w₀, …]encoding of an edge listList (WeightedEdge n ℕ).Cslib.Algorithms.WordRAM.Kruskal.encode: Word-RAM input encodingn :: es.length :: encodeEdges es.Cslib.Algorithms.WordRAM.Kruskal.Reg.out: output register of the 26-register program.Cslib.Algorithms.WordRAM.Kruskal.kruskalCode,Cslib.Algorithms.WordRAM.Kruskal.kruskalProgram: structured and compiled Word-RAM Kruskal programs.
Main results #
Cslib.Algorithms.WordRAM.Kruskal.encodeEdges_injective:encodeEdgesis injective for everyn.Cslib.Algorithms.WordRAM.Kruskal.inputFits_encode: sufficient word-width condition forInputFits w (encode es).Cslib.Algorithms.WordRAM.Kruskal.kruskalCode_exec: structured specification, including preservation of the input words.Cslib.Algorithms.WordRAM.Kruskal.kruskalProgram_spec: Word-RAMExecutesListspecification andO(m log m + m log n + n)instruction bound computingencodeEdges (kruskalEdges es), a minimum-weight spanning forest.Cslib.Algorithms.WordRAM.Kruskal.kruskalProgram_graphEdges_spec:SimpleGraph (Fin n)specialization computingencodeEdges (kruskalGraph G wt)in terms of|E| = G.edgeFinset.card.
References #
Edge-List Encoding and Injectivity #
Flat stride-3 word encoding [u₀, v₀, w₀, …] of a weighted edge list on Fin n.
Word-RAM input encoding [n, es.length, u₀, v₀, w₀, …] of a weighted multigraph on Fin n.
encodeEdges is injective for every vertex count n.
Word-width admissibility for encode es: when workspace addresses and edge weights fit in w
bits, every word of encode es (and its total length) fits in w bits.
Stride-3 Edge Memory #
Memory mem stores es in stride-3 (u, v, weight) layout starting at base.
Decode a stride-3 HoldsEdges layout from a flat Block.HoldsList (encodeEdges es)
layout.
Convert a stride-3 HoldsEdges layout into a flat Block.HoldsList (encodeEdges es)
layout.
Framing lemma for HoldsEdges over [base, base + 3 * es.length).
Data.init (encode es) stores es in stride-3 HoldsEdges layout starting at address
3.
Private Union-by-Rank Forest Invariant #
Output register holding the base address of the length-prefixed output edge list.
Structured deterministic Word-RAM Kruskal program over Basis.standard: setup, weight-key
extraction, stable index merge sort (MergeSort.mergeSortPerm), union-find initialization, and the
Kruskal scan.
Compiled Word-RAM Kruskal program returning encodeEdges (kruskalEdges es) via
ExecutesList.
Setup, Key Extraction, and Stable Index Sort #
Union-Find Initialization and findRoot #
Kruskal Scan #
End-to-End kruskalProgram_spec and SimpleGraph Specialization #
Structured ExecWithin specification of kruskalCode: halts within
52 * m * Nat.clog 2 m + 16 * m * Nat.log 2 n + 69 * m + 7 * n + 33 instructions, preserves the
input words Block.HoldsList 1 (encode es) yF.mem, and writes encodeEdges (kruskalEdges es) as a
length-prefixed list at yF.regs Reg.out.
O(m log m + m log n + n) Word-RAM Kruskal Minimum Spanning Forest Theorem
([Kru56]; [CLRS09], Section 23.2, MST-KRUSKAL): on any weighted undirected multigraph
es : List (WeightedEdge n ℕ) (m = es.length) encoded in State.init (encode es) at word width
w satisfying Fits w (9 * m + 2 * n + 4) (with ∀ e ∈ es, Fits w e.weight additionally
needed for InputFits w (encode es) / Data.Bounded w via inputFits_encode), kruskalProgram
halts within 52 * m * Nat.clog 2 m + 16 * m * Nat.log 2 n + 69 * m + 7 * n + 34 instructions
and outputs encodeEdges (kruskalEdges es) via ExecutesList, where kruskalEdges es is a
minimum-weight spanning forest of es (kruskalEdges_isMinSpanningForest), a minimum spanning
tree when (edgePartition es).numComponents = 1 (kruskalEdges_isMinSpanningTree), and
encodeEdges is injective (encodeEdges_injective).
SimpleGraph (Fin n) Specialization of Word-RAM Kruskal: on the pre-encoded canonical
|E|-edge list encode (graphEdges G wt) (|E| = G.edgeFinset.card) of G : SimpleGraph (Fin n)
weighted by wt : Sym2 (Fin n) → ℕ, at word width w satisfying Fits w (9 * |E| + 2 * n + 4)
(with ∀ e ∈ graphEdges G wt, Fits w e.weight additionally needed for InputFits /
Data.Bounded w via inputFits_encode), kruskalProgram halts within
52 * |E| * Nat.clog 2 |E| + 16 * |E| * Nat.log 2 n + 69 * |E| + 7 * n + 34 instructions
outputting encodeEdges (kruskalGraph G wt), whose spanned simple graph is a minimum spanning tree
of G when G.Connected.