Karger's randomized global minimum cut algorithm on the word RAM #
Implements Karger's randomized contraction algorithm [Karger1993] on n-vertex weighted
undirected multigraphs (n ≥ 2, IsMultigraph W) over Cslib.WordRAM.Basis.standard, providing
three complete machine-level endpoints built on the workspace-relocatable subroutine
contractDownTo:
- Single-trial contraction (
singleProgram,r = 16): runs onn :: flattenMatrix W, computes row degrees at2 + n ^ 2, contracts in place down to2supervertices in at most42 * n ^ 2 + 12 * n + 21steps andn - 2randcalls, outputs a genuine non-trivial cut weightcutWeight W C ≥ minCut Won every supported branch, and achievesminCut Wwith probability at least2 / (n * (n - 1))whenever0 < minCut W. - Repeated-trial amplification (
amplifiedProgram,r = 22): runs onR :: n :: flattenMatrix W(1 ≤ R), copies the immutable input matrix from[3, 3 + n ^ 2)into[3 + n ^ 2, 3 + 2 * n ^ 2)viaCslib.WordRAM.ArrayCopybefore each ofRindependent contractions, halts withinR * (50 * n ^ 2 + 12 * n + 30) + 16steps andR * (n - 2)randcalls with a genuine non-trivial cut weight≥ minCut W, and missesminCut Wwith probability at most(1 - 2 / (n * (n - 1))) ^ Rwhenever0 < minCut W. - Explicit
O(n ^ 4 log n)high-probability global minimum cut (highProbProgram,r = 22): runs onn :: flattenMatrix W, computeshighProbTrials n = (n * (n - 1) / 2) * (⌊log₂ n⌋ + 2)in registers using shifts and multiplication (without.divor.mod), halts within33 * n ^ 4 * (Nat.log 2 n + 2)steps with a genuine non-trivial cut weight≥ minCut W, and missesminCut Wwith probability at most1 / (2 * n)whenever0 < minCut W(and unconditionally at most1 / ninhighProbProgram_failure_le_inv_n), via an elementary Bernoulli power bound withoutReal.exp.
No probability is proved on machine states. Each program simulates a client of
Cslib.Algorithms.Lean.Karger: the word it outputs has the law of the answer of
Cslib.Algorithms.Lean.Karger.karger (singleCode_law), or of the amplification client
Cslib.Algorithms.Lean.Karger.minRuns run against independent runs of karger
(amplifiedCode_law, highProbCode_law). Each trial contracts as the client does
(contractDownTo_law) and reads half the total weight of the two supervertices left
(ContractState.mem_add_one), and the loop of trials keeps the least answer as minRuns does
(ampTrialBody_law, ampLoop_law). The success and failure bounds and the cut realization are the
specification's (Cslib.Algorithms.Lean.Karger.two_div_le_toOuterMeasure_karger,
Cslib.Algorithms.Lean.Karger.minRuns_ne_minCut_le,
Cslib.Algorithms.Lean.Karger.exists_cut_of_mem_support_answerLaw,
Cslib.Algorithms.Lean.Karger.mem_support_minRuns), carried through those laws.
Source statements and notation mapping #
- Source locators (paraphrase):
[Karger1993], Section 2, Theorem 2.1 and Corollary 2.1 (paraphrase): contracting uniformly chosen edges in ann-vertex multigraph down to2meta-vertices returns any fixed global minimum cut with probability at leastC(n, 2)⁻¹ = 2 / (n * (n - 1)), and repeatingO(n ^ 2 log n)independent trials amplifies the success probability to1 - 1 / poly(n).[KargerStein1996], Section 2, Lemma 2.1, Theorem 2.2, and Corollary 2.3, Section 2.1, Corollary 2.4, and Section 3 ("Implementing the Contraction Algorithm", Section 3.1, Lemma 3.1 and Section 3.2, Corollary 3.2) (paraphrase): contracting ann-vertex weighted multigraph fromkdown totsupervertices via edge-weight-proportional selection preserves any fixed minimum cut with probability at leastC(t, 2) / C(k, 2) = t * (t - 1) / (k * (k - 1))(2 / (n * (n - 1))att = 2, k = n), and ann × nweighted adjacency matrix with a maintained row-degree array executes each trial inO(n ^ 2)total time (O(n)per step in Section 3.2).[MitzenmacherUpfal2017], Section 1.5, Theorem 1.8 (paraphrase): multiplying single-step survival bounds1 - 2 / ifromi = ndown to3yields2 / (n * (n - 1)).
- Notation mapping, implementation variants, and parameter choices:
C(n, 2)⁻¹↔2 / (n * (n - 1))(singleProgram_spec,amplifiedProgram_spec);- whereas
[KargerStein1996], Section 3.1 draws twice (uproportional toD[u]thenvproportional toW[u, v]via twoRandom-Selectcalls),contractDownTouses an equivalent single-draw variant: one unit-costranddraw in{0, …, S - 1}(S = totalDoubledWeight W) resolved through linear prefix-sum scansuScan(rowDeg) andvScan(W[u, v]), achieving the same2 * W[u, v] / Sdistribution of Lemma 3.1; and whereas Section 3.2 statesO(n)per contraction step, our swap-compaction of active slotk - 1intovgivesO(k)instructions onkactive supervertices; - the concrete trial count
highProbTrials n = (n * (n - 1) / 2) * (Nat.log 2 n + 2)and the1 / (2 * n)failure bound inhighProbProgram_specare our own parameter choice instantiating theO(n ^ 2 log n)amplification of[Karger1993], Section 2, Corollary 2.1; - the hypothesis
0 < minCut Win thespecprobability clauses (singleProgram_spec,amplifiedProgram_spec,highProbProgram_spec) is retained there for the per-trial survival ratio, whereashighProbProgram_failure_le_inv_nholds unconditionally without2 ≤ nor0 < minCut W(whenminCut W = 0and2 ≤ n, contraction preserves zero minimum cut so every run outputs0).
Main definitions #
Cslib.Algorithms.WordRAM.Karger.singleCode,Cslib.Algorithms.WordRAM.Karger.singleProgram,Cslib.Algorithms.WordRAM.Karger.singleTime: single-trial Basic Karger (r = 16).Cslib.Algorithms.WordRAM.Karger.ampCore,Cslib.Algorithms.WordRAM.Karger.amplifiedCode,Cslib.Algorithms.WordRAM.Karger.amplifiedProgram,Cslib.Algorithms.WordRAM.Karger.amplifiedTime:R-trial amplified Karger (r = 22).Cslib.Algorithms.WordRAM.Karger.highProbCode,Cslib.Algorithms.WordRAM.Karger.highProbProgram,Cslib.Algorithms.WordRAM.Karger.highProbTime:O(n ^ 4 log n)high-probability global min-cut (r = 22).
Main results #
Cslib.Algorithms.WordRAM.Karger.singleCode_law,Cslib.Algorithms.WordRAM.Karger.amplifiedCode_law,Cslib.Algorithms.WordRAM.Karger.highProbCode_law: each program's output has the law of the output ofkargeror of amplifiedkarger, within its instruction andrand-call bounds.Cslib.Algorithms.WordRAM.Karger.ampTrialBody_law,Cslib.Algorithms.WordRAM.Karger.ampLoop_law,Cslib.Algorithms.WordRAM.Karger.ampCore_law: one trial, the loop of trials and the amplification core.Cslib.Algorithms.WordRAM.Karger.singleProgram_spec: worst-caseO(n ^ 2)step bound,n - 2rand-call bound, pathwise non-trivial cut realization,Modifiesframe, and2 / (n * (n - 1))success probability lower bound forsingleProgram.Cslib.Algorithms.WordRAM.Karger.amplifiedProgram_spec: worst-caseO(R * n ^ 2)step bound,R * (n - 2)rand-call bound, pathwise non-trivial cut realization,Modifiesframe, and(1 - 2 / (n * (n - 1))) ^ Rfailure probability upper bound foramplifiedProgram.Cslib.Algorithms.WordRAM.Karger.highProbProgram_spec,Cslib.Algorithms.WordRAM.Karger.highProbProgram_failure_le_inv_n: worst-case33 * n ^ 4 * (Nat.log 2 n + 2)step bound, pathwise non-trivial cut realization,Modifiesframe, and1 / (2 * n)(unconditional1 / n) failure probability upper bound forhighProbProgram.
References #
Word-size sufficiency and Data.init matrix layout #
Under 2 ≤ n and logWidth 3 n ≤ w, workspace end-addresses up to 3 + 2 * (n * n) + n fit in
w-bit words.
Under 2 ≤ n and logWidth 3 n ≤ w, highProbTrials n fits in w-bit words.
logWidth 3 n ≤ w and Fits w (totalDoubledWeight W) suffice for
InputFits w (n :: flattenMatrix W).
logWidth 3 n ≤ w, Fits w R, and Fits w (totalDoubledWeight W) suffice for
InputFits w (R :: n :: flattenMatrix W).
In Data.init (R :: n :: flattenMatrix W), memory holds W at base 3 with stride n.
Endpoint 1: Single-trial Basic Karger (r = 16) #
Setup for singleCode: read n from mem[1] and configure (wBase, dBase, stride, activeK, targetT) = (2, 2 + n * n, n, n, 2).
Instances For
Postprocessing for singleCode: load the contracted superedge weight mem[wBase + 1] into
val1.
Instances For
Structured 16-register single-trial Karger contraction program on n :: flattenMatrix W.
Instances For
Compiled 16-register single-trial Karger contraction program.
Instances For
Worst-case instruction bound 42 * n ^ 2 + 12 * n + 21 for singleProgram.
Instances For
A single trial runs karger. At word size w ≥ logWidth 3 n with
totalDoubledWeight W < 2 ^ w, singleCode on n :: flattenMatrix W runs within
singleTime n - 1 instructions and n - 2 rand calls and writes only [2, 2 + n * n + n). The
weight it leaves in val1 has the law of the answer of Lean.Karger.karger on ⟨n, W⟩:
contractDownTo contracts as the client does (contractDownTo_law), and singlePost reads the
weight between the last two supervertices, half their total weight.
Single-trial Basic Karger specification (singleProgram).
At word size w ≥ logWidth 3 n when totalDoubledWeight W < 2 ^ w:
singleProgramhalts withinsingleTime n = 42 * n ^ 2 + 12 * n + 21steps;- executes at most
n - 2randcalls; - on every supported branch, outputs
cutWeight W C ≥ minCut Wfor a genuine non-trivial cutCofWwhile modifying memory only in[2, 2 + n * n + n); - when
0 < minCut W(excluded only for proof convenience;S = 0contraction still halts fault-free with a valid cut weight), outputsminCut Wwith probability at least2 / (n * (n - 1)).
Shared R-trial amplification engine (r = 22) #
Base address of the immutable input matrix W.
Instances For
Original vertex count n, preserved across all trials.
Instances For
Word count n * n of the flattened matrix.
Instances For
Total number R of independent trials to execute.
Instances For
Number of trials completed so far (0 .. R).
Instances For
Minimum cut weight seen across completed trials.
Instances For
Injective register map embedding Cslib.WordRAM.ArrayCopy (r = 7) into Fin 22 with
(src, dst, len, idx, addr, value, one) = (srcBase, wBase, matLen, idx, addr1, val1, one).
Instances For
Copy matLen words from srcBase to wBase via Cslib.WordRAM.ArrayCopy.program.
Instances For
Loop running numTrials independent contractions.
Instances For
Loop invariant for ampLoop at trialIdx = i ≤ R.
- src_matrix : HoldsMatrix x.mem sb n W
- modifies_ws : WordRAM.Modifies (Finset.Ico (sb + n * n) (sb + 2 * (n * n) + n)) mem₀ x.mem
Instances For
One trial runs karger. From AmpInv at trial i < R, ampTrialBody runs within
49 n² + 12 n + 27 instructions and n - 2 rand calls and keeps the invariant at trial i + 1.
Its new bestVal has the law of the answer v of Lean.Karger.karger on ⟨n, W⟩, taken as is on
the first trial and as min v bestVal after: contractDownToAt contracts the fresh copy of W as
the client does (contractDownToAt_law), and ampTrialPost reads the weight between the last two
supervertices, half their total weight.
The loop of trials runs amplified Karger. From AmpInv at trial i, with rem = R - i
trials left, ampLoop runs within rem (50 n² + 12 n + 30) + 2 instructions and rem (n - 2)
rand calls and ends at trial R. Its bestVal has the law of the amplification client run
against independent runs of Lean.Karger.karger on ⟨n, W⟩: of Lean.Karger.minRuns rem on the
first trial, and of rem more Lean.Karger.minSteps from bestVal after.
The amplification core runs amplified Karger. For 1 ≤ R trials, from a state with
srcBase = sb, nOrig = n, numTrials = R and HoldsMatrix x.mem sb n W, ampCore runs within
R (50 n² + 12 n + 30) + 8 instructions and R (n - 2) rand calls and writes only the
workspace. Its bestVal has the law of Lean.Karger.minRuns R against independent runs of
Lean.Karger.karger on ⟨n, W⟩.
Endpoint 2: Amplified R-trial Karger (r = 22) #
Setup for amplifiedCode on R :: n :: flattenMatrix W: load R = mem[1], n = mem[2], and
set srcBase = 3.
Instances For
Structured 22-register R-trial Karger program on R :: n :: flattenMatrix W.
Instances For
Compiled 22-register R-trial Karger program.
Instances For
Worst-case instruction bound R * (50 * n ^ 2 + 12 * n + 30) + 16 for amplifiedProgram.
Instances For
amplifiedCode runs amplified Karger. At word size w ≥ logWidth 3 n with 1 ≤ R,
R < 2 ^ w and totalDoubledWeight W < 2 ^ w, amplifiedCode on R :: n :: flattenMatrix W runs
within amplifiedTime R n - 1 instructions and R (n - 2) rand calls and writes only
[3 + n * n, 3 + 2 * (n * n) + n). Its bestVal has the law of Lean.Karger.minRuns R against
independent runs of Lean.Karger.karger on ⟨n, W⟩.
R-trial amplified Karger specification (amplifiedProgram).
At word size w ≥ logWidth 3 n when 1 ≤ R, R < 2 ^ w, and totalDoubledWeight W < 2 ^ w:
amplifiedProgramhalts withinamplifiedTime R n = R * (50 * n ^ 2 + 12 * n + 30) + 16steps;- executes at most
R * (n - 2)randcalls; - on every supported branch, outputs
cutWeight W C ≥ minCut Wfor a genuine non-trivial cutCofWwhile modifying memory only in[3 + n * n, 3 + 2 * (n * n) + n); - when
0 < minCut W(excluded only for proof convenience;S = 0contraction still halts fault-free with a valid cut weight), missesminCut Wwith probability at most(1 - 2 / (n * (n - 1))) ^ R.
Endpoint 3: Explicit O(n ^ 4 log n) high-probability global minimum cut (r = 22) #
Setup for highProbCode on n :: flattenMatrix W: load n = mem[1], compute
numTrials = highProbTrials n = (n * (n - 1) / 2) * (Nat.log 2 n + 2) using shifts and
multiplications, and set srcBase = 2.
Instances For
Structured 22-register O(n ^ 4 log n) high-probability Karger program on
n :: flattenMatrix W.
Instances For
Compiled 22-register O(n ^ 4 log n) high-probability Karger program.
Instances For
Worst-case instruction bound 33 * n ^ 4 * (Nat.log 2 n + 2) for highProbProgram.
Instances For
The setup's 4 log₂ n + 13 instructions, the highProbTrials n trials'
highProbTrials n (50 n² + 12 n + 30) + 8 and the final halt fit in highProbTime n.
highProbCode runs amplified Karger. At word size w ≥ logWidth 3 n with
totalDoubledWeight W < 2 ^ w, highProbCode on n :: flattenMatrix W runs within
4 log₂ n + highProbTrials n (50 n² + 12 n + 30) + 21 instructions and
highProbTrials n (n - 2) rand calls and writes only [2 + n * n, 2 + 2 * (n * n) + n). Its
bestVal has the law of Lean.Karger.minRuns (highProbTrials n) against independent runs of
Lean.Karger.karger on ⟨n, W⟩.
Explicit O(n ^ 4 log n) high-probability global minimum cut (highProbProgram).
At word size w ≥ logWidth 3 n when 2 ≤ n and totalDoubledWeight W < 2 ^ w:
highProbProgramhalts withinhighProbTime n = 33 * n ^ 4 * (Nat.log 2 n + 2)steps;- executes at most
highProbTrials n * (n - 2)randcalls; - on every supported branch, outputs
cutWeight W C ≥ minCut Wfor a genuine non-trivial cutCofWwhile modifying memory only in[2 + n * n, 2 + 2 * (n * n) + n); - when
0 < minCut W(excluded only for proof convenience;S = 0contraction still halts fault-free with a valid cut weight), missesminCut Wwith probability at most1 / (2 * n).
highProbProgram 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 contraction trial outputs 0.