Documentation

CslibFrontier.Algorithms.WordRAM.Kruskal

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₀, …]):

  1. Stable edge sort. The program copies the m edge weights into a key array and runs the stable key-indexed merge sort Cslib.Algorithms.WordRAM.MergeSort.mergeSortPerm, producing an index permutation perm with perm.filterMap (es[·]?) = sortEdges es (Cslib.Algorithms.WordRAM.MergeSort.filterMap_getElem?_eq_mergeSort). The edge triples are not moved: the scan reads edge perm[s] at step s.
  2. Union-by-rank disjoint-set forest. The program initializes parent[0..n) and rank[0..n) and scans the edges in sorted order. Each findRoot follows at most ⌊log₂ n⌋ parent pointers, because ranks strictly increase along parent pointers and 2 ^ rank[r] is at most the size of the class of a root r ([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 #

Main results #

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.

theorem Cslib.Algorithms.WordRAM.Kruskal.inputFits_encode {w n : ℕ} (es : List (Lean.Kruskal.WeightedEdge n ℕ)) (hmem : WordRAM.Fits w (9 * es.length + 2 * n + 4)) (hw : ∀ e ∈ es, WordRAM.Fits w e.weight) :

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.

theorem Cslib.Algorithms.WordRAM.Kruskal.holdsEdges_frame {n base : ℕ} {es : List (Lean.Kruskal.WeightedEdge n ℕ)} {m₀ m₁ : ℕ → ℕ} (h : HoldsEdges base es m₀) (heq : ∀ (a : ℕ), base ≤ a → a < base + 3 * es.length → m₁ a = m₀ a) :
HoldsEdges base es m₁

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 #

Register File (Fin 26) and Structured Sub-Programs #

@[reducible, inline]

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.