Documentation

CslibFrontier.Algorithms.WordRAM.KargerStein

Karger–Stein's recursive FastCut algorithm on the word RAM #

Implements the Karger–Stein recursive randomized global minimum cut algorithm ([KargerStein1996], Section 4, Figure 6, Lemmas 4.1–4.3, and the minimum-cut-value part of Theorem 4.4; paraphrase) on n-vertex weighted undirected multigraphs (n ≥ 2, IsMultigraph W) over Cslib.WordRAM.Basis.standard using r = 24 registers and an explicit iterative frame stack in O(n ^ 2) scratch words (≤ 26 * n ^ 2 stack words plus n ^ 2 input words, matching Section 4, Lemma 4.2), providing two machine-level endpoints on n :: flattenMatrix W:

  1. Single-tree recursive contraction (fastCutProgram, r = 24, [KargerStein1996], Section 4, Figure 6 and Lemmas 4.1–4.3; paraphrase): contracts recursively from k ≥ 7 down to the pure-integer threshold t = fastCutThreshold k (smallest t ≥ 2 with k * (k - 1) ≤ 2 * t * (t - 1), an integer deviation from Figure 6's ⌈k / √2 + 1⌉ that is never larger and is strictly smaller at e.g. k = 8, where t = 6 vs 7) along two independent branches, switches at k ≤ 6 to a single contraction down to 2 supervertices (the source's base case: Figure 6's Contract(G, 2) with the size-7 cutoff of equation (2); a leaf keeps a fixed minimum cut with probability ≥ 2 / (k * (k - 1)) ≥ 1 / 15, equation (2)'s base-case bound, which is where the 15 comes from), halts within fastCutTime n = 2500 * n ^ 2 * (Nat.log 2 n + 2) (O(n ^ 2 log n)) steps and fastCutNodeQueries n ≤ 6 * n ^ 2 (O(n ^ 2)) rand calls with a genuine non-trivial cut weight cutWeight W C ≥ minCut W on every supported path, modifies only [2 + n ^ 2, 2 + 27 * n ^ 2), and achieves minCut W with probability at least 1 / (Nat.log 2 n + 15) whenever 0 < minCut W (excluded only for proof convenience; when minCut W = 0, any non-trivial zero-weight cut is already optimal).
  2. Amplified high-probability global minimum cut (fastCutHighProbProgram, r = 24, [KargerStein1996], Section 4, Theorem 4.4; paraphrase, the value part only: the source finds all minimum cuts in O(m log(n ^ 2 / m)) space; we prove the returned value is minCut W except with probability ≤ 1 / (2 * n)): executes our concrete O(log ^ 2 n) trial schedule fastCutTrials n = (Nat.log 2 n + 15) * (Nat.log 2 n + 2) of independent FastCut trees reusing the same [2 + n ^ 2, 2 + 27 * n ^ 2) scratch arena, halts within fastCutHighProbTime n = 2600 * n ^ 2 * (Nat.log 2 n + 15) * (Nat.log 2 n + 2) ^ 2 (O(n ^ 2 log ^ 3 n)) steps and fastCutTrials n * (6 * n ^ 2) rand calls with a genuine non-trivial cut weight ≥ minCut W, and misses minCut W with probability at most 1 / (2 * n) whenever 0 < minCut W (and unconditionally at most 1 / n in fastCutHighProbProgram_failure_le_inv_n, without 2 ≤ n or 0 < minCut W).

The probability argument is proved once for the oracle client Cslib.Algorithms.Lean.Karger.fastCut. Native segments observe exactly that client's answer law (fastCutNode_law, fastCutTree_law), and the outer loop observes the existing independent minRuns client (fastCutAmpLoop_law, fastCutHighProbCode_law). Their certificates keep the original supported-path instruction and random-call bounds and memory frames. They do not replace those bounds with expected costs.

baseCaseAndPop_spec, branchContractAndPush_spec, fastCutTree_spec and fastCutAmpBody_spec remain law-derived compatibility statements for the existing exact-basis bridge. Its still-used ChildMinCutSurvives proposition and KSAmpInv.best_cut field are retained for that bridge; the native recursive and amplification stages consume laws directly.

Main definitions #

Main results #

References #

Word-size sufficiency (logWidth 4 n ≤ w) and input layout #

Register layout (r = 24) #

@[reducible, inline]

Constant register 0 (Reg.zero embedded into Fin 24).

@[reducible, inline]

Constant register 1 (Reg.one embedded into Fin 24).

@[reducible, inline]

Working matrix base address (Reg.wBase embedded into Fin 24).

@[reducible, inline]

Working degree-array base address (Reg.dBase embedded into Fin 24).

@[reducible, inline]

Working matrix row stride (Reg.stride embedded into Fin 24).

@[reducible, inline]

Active supervertex count k (Reg.activeK embedded into Fin 24).

@[reducible, inline]

Target supervertex count t (Reg.targetT embedded into Fin 24).

@[reducible, inline]

Active doubled edge weight S (Reg.totS embedded into Fin 24).

@[reducible, inline]

Row / auxiliary scratch register (Reg.uReg embedded into Fin 24).

@[reducible, inline]

Column / auxiliary scratch register (Reg.vReg embedded into Fin 24).

@[reducible, inline]

Loop index scratch register (Reg.idx embedded into Fin 24).

@[reducible, inline]

Loop length / remainder scratch register (Reg.rem embedded into Fin 24).

@[reducible, inline]

Primary scratch address register (Reg.addr1 embedded into Fin 24).

@[reducible, inline]

Secondary scratch address register (Reg.addr2 embedded into Fin 24).

@[reducible, inline]

Primary scratch value register (Reg.val1 embedded into Fin 24).

@[reducible, inline]

Secondary scratch value register (Reg.val2 embedded into Fin 24).

@[reducible, inline]

Base address (2) of the immutable input matrix W.

@[reducible, inline]

Root vertex count n, preserved across all trees.

@[reducible, inline]

Current stack frame base address fp.

@[reducible, inline]

Current active stack depth sp (0 = empty).

@[reducible, inline]

Cut weight returned by the most recently popped frame.

@[reducible, inline]

Total number of FastCut trees to run in fastCutHighProbCode.

@[reducible, inline]

Number of FastCut trees completed so far.

@[reducible, inline]

Minimum cut weight seen across completed FastCut trees.

Deterministic subroutines: block copy, integer threshold, and submatrix compaction #

Injective register map embedding Cslib.WordRAM.ArrayCopy (r = 7) into Fin 24 with (src, dst, len, idx, addr, value, one) = (uReg, wBase, rem, idx, addr1, val1, one).

theorem Cslib.Algorithms.WordRAM.Karger.holdsMatrix_of_copyPrefix {mem : ℕ → ℕ} {sb db k : ℕ} {W : Fin k → Fin k → ℕ} (hsrc : HoldsMatrix mem sb k W) :
HoldsMatrix (WordRAM.ArrayCopy.copyPrefix mem sb db (k * k)) db k W

Loop body of thresholdCode: advance t to t + 1 while maintaining uReg = t (= (t + 1) - 1) and val1 = 2 * (t + 1) * t without subtraction.

Preamble of thresholdCode: initialize rem = k * (k - 1) and (targetT, uReg, val2, val1) = (2, 1, 2, 4).

Compute targetT = fastCutThreshold k in O(k) instructions for k = x.regs activeK ≥ 7.

Inner column loop body of copySubmatrixCode: copy mem[idx + vReg] to mem[rem + vReg].

Outer row loop body of copySubmatrixCode: copy row uReg of length targetT and advance row pointers idx by stride and rem by targetT.

Copy the active t × t submatrix at wBase (stride stride) densely into addr2 (stride t) without .sub, .div, or .mod.

Contracting a multigraph never increases totalDoubledWeight.

Explicit iterative call-stack machine for FastCut (r = 24) #

Each active frame at base address fp for a k-supervertex subproblem (frameSize k = 4 + 2 * (k * k) + k) lays out its immutable data first and its mutable workspace second:

Consequently, executing a frame at fp (including all of its recursive descendants) modifies only Finset.Ico (fp + 2 + k * k) (fp + fastCutScratch k), leaving parentFp, k, W_k, and every ancestor frame below fp untouched.

Frame-dispatch header: load activeK = k = mem[fp + 1], compute rem = k * k, addr1 = fp + 2, and addr2 = fp + 2 + k * k, load val2 = stage = mem[addr2], and set val1 = 7.

Base-case workspace pointer setup (2 ≤ k ≤ 6): set uReg = fp + 2, wBase = fp + 4 + k * k, dBase = wBase + k * k, stride = k, and targetT = 2.

Base-case epilogue: read retVal = mem[wBase + 1] and pop the frame (fpReg ← mem[fpReg], spReg ← spReg - 1).

Base-case execution (2 ≤ k ≤ 6): copy W_k to wBase, compute degrees, contract once down to 2 supervertices, read W'[0, 1], and pop the frame.

Stage 0 update (7 ≤ k): set mem[fp + 2 + k * k] ← 1 before contracting Branch 1.

Stage 1 update (7 ≤ k): set mem[fp + 2 + k * k] ← 2 and save retVal into mem[fp + 3 + k * k] before contracting Branch 2.

Destination setup before compacting the contracted t × t submatrix into childFp + 2.

Initialize the child frame header at childFp = dBase + stride (parentFp = fp, k_child = t, stage_child = 0) and push (fpReg, spReg) ← (childFp, spReg + 1).

Deterministic tail of branchContractAndPush: compact the t × t contracted submatrix into childFp + 2 and push the child frame header at childFp.

Shared Branch 1 / Branch 2 contraction-and-push body (7 ≤ k): compute t = fastCutThreshold k, copy W_k to wBase, compute degrees, contract down to t, compact the t × t submatrix into childFp + 2, and push the child frame at childFp = fp + frameSize k.

Stage 2 epilogue (7 ≤ k): set retVal ← min savedVal1 retVal and pop the frame.

Single iteration body of the explicit-stack FastCut loop.

Outer explicit-stack while loop executing all active frames down to spReg = 0.

theorem Cslib.Algorithms.WordRAM.Karger.baseCaseAndPop_spec {n w fp parentFp b k : ℕ} {W₀ : Fin n → Fin n → ℕ} {W_k : Fin k → Fin k → ℕ} {part : Fin n → Fin k} {x : WordRAM.Data 24} (hk2 : 2 ≤ k) (hk6 : k ≤ 6) (hk_reg : x.regs KSReg.activeK = k) (hrem_reg : x.regs KSReg.rem = k * k) (ha1_reg : x.regs KSReg.addr1 = fp + 2) (ha2_reg : x.regs KSReg.addr2 = fp + 2 + k * k) (hfp : x.regs KSReg.fpReg = fp) (hsp : x.regs KSReg.spReg = b + 1) (hmem_p : x.mem fp = parentFp) (hmat : HoldsMatrix x.mem (fp + 2) k W_k) (hC : WeightedCut.IsContractionOf W₀ part W_k) (hfit : WordRAM.Fits w (fp + Karger.frameSize k)) (hsp_fit : WordRAM.Fits w b) (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W₀)) :
def Cslib.Algorithms.WordRAM.Karger.ChildMinCutSurvives {n : ℕ} (W₀ : Fin n → Fin n → ℕ) (childFp t : ℕ) (d : WordRAM.Data 24) :

Event on a child-frame entry state d at childFp asserting that d.mem holds a valid t-supervertex contraction (W₁, part₁) of W₀ together with a non-trivial cut B₁ pulling back to a minimum cut of W₀.

theorem Cslib.Algorithms.WordRAM.Karger.branchContractAndPush_spec {n w fp b k : ℕ} {W₀ : Fin n → Fin n → ℕ} {W_k : Fin k → Fin k → ℕ} {part : Fin n → Fin k} {x : WordRAM.Data 24} (hk7 : 7 ≤ k) (hk_reg : x.regs KSReg.activeK = k) (hfp : x.regs KSReg.fpReg = fp) (hsp : x.regs KSReg.spReg = b + 1) (hmat : HoldsMatrix x.mem (fp + 2) k W_k) (hC : WeightedCut.IsContractionOf W₀ part W_k) (hfit : WordRAM.Fits w (fp + Karger.fastCutScratch k)) (hsp_fit : WordRAM.Fits w (b + 2)) (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W₀)) :
have t := Karger.fastCutThreshold k; have childFp := fp + Karger.frameSize k; ∃ (m : ProbComp (WordRAM.Data 24 × ℕ)), WordRAM.ExecRand w branchContractAndPush x m ∧ OracleComp.IsQueryBounded m (k - t) ∧ (∀ z ∈ OracleComp.support m, z.2 ≤ 80 * (k * k) ∧ WordRAM.Modifies (Finset.Ico (fp + 4 + k * k) (fp + Karger.fastCutScratch k)) x.mem z.1.mem ∧ z.1.regs KSReg.zero = 0 ∧ z.1.regs KSReg.fpReg = childFp ∧ z.1.regs KSReg.spReg = b + 2 ∧ z.1.mem childFp = fp ∧ z.1.mem (childFp + 1) = t ∧ z.1.mem (childFp + 2 + t * t) = 0 ∧ ∃ (W' : Fin t → Fin t → ℕ) (part' : Fin n → Fin t), WeightedCut.IsContractionOf W₀ part' W' ∧ HoldsMatrix z.1.mem (childFp + 2) t W') ∧ ∀ (B : Finset (Fin k)), 0 < WeightedCut.minCut W₀ → WeightedCut.IsNonTrivialCut B → WeightedCut.cutWeight W₀ (WeightedCut.pullbackCut part B) = WeightedCut.minCut W₀ → 1 / 2 ≤ m.eval.toOuterMeasure {z : WordRAM.Data 24 × ℕ | ChildMinCutSurvives W₀ childFp t z.1}
theorem Cslib.Algorithms.WordRAM.Karger.fastCutNode_law {n w : ℕ} {W₀ : Fin n → Fin n → ℕ} (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W₀)) (d : ℕ) {fp parentFp b k : ℕ} {W_k : Fin k → Fin k → ℕ} {part : Fin n → Fin k} {x : WordRAM.Data 24} :
2 ≤ k → Karger.fastCutDepth k ≤ d → x.regs KSReg.zero = 0 → x.regs KSReg.fpReg = fp → x.regs KSReg.spReg = b + 1 → x.mem fp = parentFp → x.mem (fp + 1) = k → x.mem (fp + 2 + k * k) = 0 → HoldsMatrix x.mem (fp + 2) k W_k → WeightedCut.IsContractionOf W₀ part W_k → WordRAM.Fits w (fp + Karger.fastCutScratch k) → WordRAM.Fits w (b + 1 + d) → WordRAM.LoopSegmentLaw w (WordRAM.Cond.lt KSReg.zero KSReg.spReg) fastCutStep x (fun (y : WordRAM.Data 24) => WordRAM.Modifies (Finset.Ico (fp + 2 + k * k) (fp + Karger.fastCutScratch k)) x.mem y.mem ∧ y.regs KSReg.zero = 0 ∧ y.regs KSReg.fpReg = parentFp ∧ y.regs KSReg.spReg = b) (Karger.fastCutNodeTime k) (Karger.fastCutNodeQueries k) (fun (y : WordRAM.Data 24) => y.regs KSReg.retVal) (Lean.Karger.fastCutLaw k { size := k, weight := W_k })

Every native recursive node has exactly the specification's answer law, with the existing supported-path instruction, random-call and memory-frame bounds.

theorem Cslib.Algorithms.WordRAM.Karger.fastCutNode_spec {n w : ℕ} {W₀ : Fin n → Fin n → ℕ} (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W₀)) (d : ℕ) {fp parentFp b k : ℕ} {W_k : Fin k → Fin k → ℕ} {part : Fin n → Fin k} {x : WordRAM.Data 24} :

Structural LoopSegment specification of recursive FastCut over depth budget d.

Root-frame setup before copying the input matrix W from srcBase into fp₀ + 2.

Root-frame stage/stack-pointer initialization after copying W into fp₀ + 2.

theorem Cslib.Algorithms.WordRAM.Karger.fastCutTree_law {n w sb : ℕ} {W : Fin n → Fin n → ℕ} {x : WordRAM.Data 24} (hn : 2 ≤ n) (hW : WeightedCut.IsMultigraph W) (hsb : x.regs KSReg.srcBase = sb) (hnOrig : x.regs KSReg.nOrig = n) (hmat : HoldsMatrix x.mem sb n W) (hws_fit : WordRAM.Fits w (sb + 27 * (n * n))) (hsp_fit : WordRAM.Fits w (2 * Nat.log 2 n + 1)) (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W)) :

A complete native tree has exactly the recursive specification answer law.

Endpoint 1: Single-tree recursive FastCut (r = 24) #

Setup for fastCutCode on n :: flattenMatrix W: load n = mem[1] and set srcBase = 2.

Structured 24-register single-tree Karger–Stein FastCut code on n :: flattenMatrix W.

Compiled 24-register single-tree Karger–Stein FastCut program.

Worst-case O(n ^ 2 log n) instruction bound 2500 * n ^ 2 * (Nat.log 2 n + 2) for fastCutProgram.

The single-tree code observes the recursive answer law, including the charged input setup.

Single-tree Karger–Stein FastCut specification (fastCutProgram, [KargerStein1996], Section 4, Figure 6 and Lemmas 4.1–4.3; paraphrase). At word size w ≥ logWidth 4 n when 2 ≤ n and totalDoubledWeight W < 2 ^ w:

  1. fastCutProgram halts within fastCutTime n = 2500 * n ^ 2 * (Nat.log 2 n + 2) steps;
  2. executes at most fastCutNodeQueries n (≤ 6 * (n * n)) rand calls;
  3. on every supported branch, outputs cutWeight W C ≥ minCut W for a genuine non-trivial cut C of W while modifying memory only in [2 + n * n, 2 + 27 * (n * n));
  4. when 0 < minCut W (excluded only for proof convenience), outputs minCut W with probability at least 1 / (Nat.log 2 n + 15).

Endpoint 2: Amplified O(n ^ 2 log ^ 3 n) high-probability FastCut (r = 24) #

Setup for fastCutHighProbCode on n :: flattenMatrix W: load n = mem[1], compute numTrials = fastCutTrials n = (Nat.log 2 n + 15) * (Nat.log 2 n + 2) without subtraction or division, and initialize srcBase = 2, trialIdx = 0, bestVal = 0.

Postprocessing after each fastCutTree trial: update bestVal with retVal (unconditionally on trial 0 and via minimum thereafter) and increment trialIdx.

Outer amplification loop running numTrials independent FastCut trees.

Structured 24-register O(n ^ 2 log ^ 3 n) high-probability Karger–Stein program on n :: flattenMatrix W.

Compiled 24-register O(n ^ 2 log ^ 3 n) high-probability Karger–Stein program.

Worst-case instruction bound 2600 * n ^ 2 * (Nat.log 2 n + 15) * (Nat.log 2 n + 2) ^ 2 (O(n ^ 2 log ^ 3 n)) for fastCutHighProbProgram.

structure Cslib.Algorithms.WordRAM.Karger.KSAmpInv (sb n R i : ℕ) (W : Fin n → Fin n → ℕ) (mem₀ : ℕ → ℕ) (x : WordRAM.Data 24) :

Loop invariant for fastCutAmpLoop at trialIdx = i ≤ R.

theorem Cslib.Algorithms.WordRAM.Karger.fastCutAmpBody_spec {w sb n R i : ℕ} {W : Fin n → Fin n → ℕ} {mem₀ : ℕ → ℕ} {x : WordRAM.Data 24} (hn : 2 ≤ n) (hW : WeightedCut.IsMultigraph W) (hiR : i < R) (h1 : WordRAM.Fits w 1) (hws_fit : WordRAM.Fits w (sb + 27 * (n * n))) (hsp_fit : WordRAM.Fits w (2 * Nat.log 2 n + 1)) (hR_fit : WordRAM.Fits w R) (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W)) (hinv : KSAmpInv sb n R i W mem₀ x) :
theorem Cslib.Algorithms.WordRAM.Karger.fastCutAmpLoop_law {w sb n R : ℕ} {W : Fin n → Fin n → ℕ} {mem₀ : ℕ → ℕ} (hR : 1 ≤ R) (hn : 2 ≤ n) (hW : WeightedCut.IsMultigraph W) (h1 : WordRAM.Fits w 1) (hws_fit : WordRAM.Fits w (sb + 27 * (n * n))) (hsp_fit : WordRAM.Fits w (2 * Nat.log 2 n + 1)) (hR_fit : WordRAM.Fits w R) (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W)) (remaining i : ℕ) (x : WordRAM.Data 24) :
i + remaining = R → KSAmpInv sb n R i W mem₀ x → WordRAM.ExecRandLaw w fastCutAmpLoop x (KSAmpInv sb n R R W mem₀) (remaining * (2500 * n ^ 2 * (Nat.log 2 n + 2)) + 2) (remaining * (6 * (n * n))) (fun (y : WordRAM.Data 24) => y.regs KSReg.bestVal) (OracleComp.evalWith (fun (x : Unit) => Lean.Karger.fastCutLaw n { size := n, weight := W }) (if i = 0 then Lean.Karger.minRuns remaining else OracleComp.iterate remaining Lean.Karger.minStep (x.regs KSReg.bestVal)))

The native loop has the law of independent trials of the existing amplification client. The positive total trial count excludes the empty-client convention at an arbitrary initial word.

theorem Cslib.Algorithms.WordRAM.Karger.fastCutHighProbCode_law {n w : ℕ} {W : Fin n → Fin n → ℕ} (hn : 2 ≤ n) (hW : WeightedCut.IsMultigraph W) (hw : WordRAM.logWidth 4 n ≤ w) (htot : WordRAM.Fits w (WeightedCut.totalDoubledWeight W)) :
WordRAM.ExecRandLaw w fastCutHighProbCode (WordRAM.Data.init (n :: flattenMatrix W)) (fun (y : WordRAM.Data 24) => WordRAM.Modifies (Finset.Ico (2 + n * n) (2 + 27 * (n * n))) (WordRAM.Data.init (n :: flattenMatrix W)).mem y.mem) (4 * Nat.log 2 n + 14 + (Karger.fastCutTrials n * (2500 * n ^ 2 * (Nat.log 2 n + 2)) + 2)) (Karger.fastCutTrials n * (6 * (n * n))) (fun (y : WordRAM.Data 24) => y.regs KSReg.bestVal) (Lean.Karger.fastCutHighProbLaw n { size := n, weight := W })

The full native high-probability code observes the existing independent-trial law, paying for the original setup, every loop-control instruction and the same random-call budget.

Amplified O(n ^ 2 log ^ 3 n) high-probability Karger–Stein (fastCutHighProbProgram, the minimum-cut-value part of [KargerStein1996], Section 4, Theorem 4.4; paraphrase). At word size w ≥ logWidth 4 n when 2 ≤ n and totalDoubledWeight W < 2 ^ w:

  1. fastCutHighProbProgram halts within fastCutHighProbTime n = 2600 * n ^ 2 * (Nat.log 2 n + 15) * (Nat.log 2 n + 2) ^ 2 steps;
  2. executes at most fastCutTrials n * (6 * (n * n)) rand calls;
  3. on every supported branch, outputs cutWeight W C ≥ minCut W for a genuine non-trivial cut C of W while modifying memory only in [2 + n * n, 2 + 27 * (n * n));
  4. when 0 < minCut W (excluded only for proof convenience), misses minCut W with probability at most 1 / (2 * n).

fastCutHighProbProgram misses minCut W with probability at most 1 / n, unconditionally without 2 ≤ n or 0 < minCut W: for n < 2 the bound 1 / n is at least 1, and for 2 ≤ n with minCut W = 0 every recursive trial outputs 0.