Workspace-relocatable randomized contraction on the word RAM #
Implements Karger's edge-weight-proportional contraction loop (contractDownTo) and its companion
row-degree initializer (computeDegreesAndTotal) as structured Cslib.WordRAM programs over
Basis.standard with 16 registers.
All memory accesses are parameterized dynamically by register-held workspace pointers
(wBase, dBase, stride, activeK, targetT, totS), with zero hardcoded memory addresses and
a Modifies frame confined to
Finset.Ico wBase (wBase + stride * k) ∪ Finset.Ico dBase (dBase + k).
This allows both repeated-trial amplification (KargerMinCut) and recursive stack-allocated calls
in Karger–Stein (FastCut) to invoke contractDownTo directly at arbitrary memory offsets.
Each contraction from m + 1 down to m active supervertices runs in O(m) worst-case
instructions and issues at most 1 rand call (0 calls when total active weight totS = 0),
yielding 36 * (k ^ 2 - t ^ 2) + 4 worst-case instructions and at most k - t rand calls from
k down to t, with survival probability at least (t * (t - 1)) / (k * (k - 1)) for any fixed
minimum cut of a connected multigraph.
The probability is not proved on machine states. contractDownTo simulates Karger's algorithm as
a client of the contraction structure Cslib.Algorithms.Lean.Karger.spec: the graph the machine
holds (heldGraph) has the law of Cslib.Algorithms.Lean.Karger.contractTimes
(contractDownTo_law). Each step's rand call draws the offset uniformly below the total weight,
as the client's coin does, and postRandStep_exec contracts the pair sampled there. The survival
bound and the contraction invariant are the specification's
(Cslib.Algorithms.Lean.Karger.survives_contractTimes,
Cslib.Algorithms.Lean.Karger.isContractionOf_of_mem_support_contractTimes), carried through that
law.
Source statements and notation mapping #
- Matrix contraction and survival bound (paraphrase):
[KargerStein1996], Section 2, Lemma 2.1, Theorem 2.2, and Corollary 2.3, and Section 2.1, Corollary 2.4 (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))([Karger1993], Section 2, Theorem 2.1 and[MitzenmacherUpfal2017], Section 1.5, Theorem 1.8 give thet = 2case2 / (n * (n - 1))).[KargerStein1996], Section 3 ("Implementing the Contraction Algorithm"), Section 3.1, Lemma 3.1 and Section 3.2, Corollary 3.2 (paraphrase): maintaining ann × nweighted adjacency matrixWand row-degree arrayD[u] = ∑ v, W[u, v]selects each edge(u, v)with probability2 * W[u, v] / ∑ w, D[w]and executes the Contraction Algorithm inO(n ^ 2)total time (O(n)per contraction step in[KargerStein1996], Section 3.2).
- Notation mapping and implementation variants:
C(t, 2) / C(k, 2)↔((t * (t - 1) : ℕ) : ℝ≥0∞) / (k * (k - 1))incontractDownTo_specandcontractDownToAt_spec;- whereas
[KargerStein1996], Section 3.1 draws twice (uproportional toD[u]thenvproportional toW[u, v]via twoRandom-Selectcalls), ourcontractDownTouses an equivalent single-draw variant: one unit-cost word-RAMrandinstruction drawing uniformly from{0, …, totS - 1}(totS = totalDoubledWeight W), resolved through linear prefix-sum scansuScanoverD[0..k)andvScanoverW[u, 0..k)to select each unordered pair{u, v}with the same probability2 * W[u, v] / totSof Lemma 3.1; - whereas
[KargerStein1996], Section 3.2 updates rows and columns inO(n)per contraction by zeroing the removed vertex in place, ourcontractDownToswap-compacts active slotk - 1intovso each contraction step onkactive supervertices runs inO(k)instructions.
Main definitions #
Cslib.Algorithms.WordRAM.Karger.flattenMatrix: the row-majorn ^ 2-word input layout of a weight matrix.Cslib.Algorithms.WordRAM.Karger.HoldsMatrix,Cslib.Algorithms.WordRAM.Karger.HoldsWorkspace,Cslib.Algorithms.WordRAM.Karger.workspaceFootprint,Cslib.Algorithms.WordRAM.Karger.ContractState: relocatable memory layout and word-bound invariants for an activek × ksupervertex adjacency matrix with row stridestrideand row degree array atdBase.Cslib.Algorithms.WordRAM.Karger.heldGraph: the graph a machine state holds, as a state ofCslib.Algorithms.Lean.Karger.spec.Cslib.Algorithms.WordRAM.Karger.computeDegreesAndTotal,Cslib.Algorithms.WordRAM.Karger.computeDegreesAndTotalAt: deterministicO(k ^ 2)initializer populatingD[0..k)andtotS.Cslib.Algorithms.WordRAM.Karger.contractDownTo,Cslib.Algorithms.WordRAM.Karger.contractDownToAt: randomized contraction loop fromactiveKdown totargetT.
Main results #
Cslib.Algorithms.WordRAM.Karger.computeDegreesAndTotal_exec:computeDegreesAndTotalpopulatesHoldsWorkspaceandtotS = totalDoubledWeight Wwithin6 * k ^ 2 + 12 * k + 6instructions, modifying onlyFinset.Ico dBase (dBase + k).Cslib.Algorithms.WordRAM.Karger.contractDownTo_law:contractDownTosimulatesk - tcontractions of the specification:ExecRandLawwithIsQueryBounded (k - t), worst-case step bound36 * (k ^ 2 - t ^ 2) + 4,Modifiesframe, and the law ofheldGraph.Cslib.Algorithms.WordRAM.Karger.contractDownToAt_law: the same on anyr ≥ 16registers, preserving the caller's registers.Cslib.Algorithms.WordRAM.Karger.ContractState.mem_add_one: on two supervertices, the weight between them is half the total weight of the held graph.Cslib.Algorithms.WordRAM.Karger.contractDownTo_spec: joint certificate ofExecRand,IsQueryBounded (k - t), worst-case step bound36 * (k ^ 2 - t ^ 2) + 4,Modifiesframe, contraction invariantIsContractionOf, and cut-survival probability lower bound((t * (t - 1) : ℕ) : ℝ≥0∞) / (k * (k - 1)), the last two from the specification.
References #
Register layout (r = 16) #
Constant 0.
Instances For
Constant 1.
Instances For
Base address of active adjacency matrix W.
Instances For
Base address of active row-degree array D.
Instances For
Current number k of active supervertices.
Instances For
Target number t of active supervertices (2 ≤ t ≤ k).
Instances For
Current doubled total weight S = totalDoubledWeight W.
Instances For
Sampled / outer row index u.
Instances For
Sampled / inner column index v.
Instances For
Loop index for merge and compaction sweeps.
Instances For
Sample remainder / scratch address register.
Instances For
Scratch pointer 1.
Instances For
Scratch pointer 2.
Instances For
Scratch value 1.
Instances For
Scratch value 2.
Instances For
Row-major input layout of the matrix #
Relocatable workspace representation and address geometry #
In Data.init (pfx ++ flattenMatrix W), the flattened matrix occupies memory starting at
1 + pfx.length with stride n.
In Data.init (n :: flattenMatrix W), memory holds W at base 2 with stride n.
The input a :: flattenMatrix W fits word size w when one fitting bound B covers both
the input length 1 + n * n and the leading word a, and the matrix total fits.
Permitted memory write footprint of an active k-supervertex workspace at (wb, db, str).
Instances For
Machine-state precondition for contractDownTo on k active supervertices targeting t.
- fits_wEnd : WordRAM.Fits w (wb + str * k)
- fits_dEnd : WordRAM.Fits w (db + k)
- fits_totS : WordRAM.Fits w (WeightedCut.totalDoubledWeight W)
- workspace : HoldsWorkspace x.mem wb db str W
Instances For
The graph that a machine state holds in the workspace at wb with row stride str: activeK
supervertices, with the weight matrix read from memory. It abstracts a ContractState to a state
of the contraction structure Lean.Karger.spec.
Instances For
A state holding W on k supervertices holds the graph ⟨k, W⟩.
On two supervertices, the weight between them, at wb + 1, is half the total weight of the
held graph.
Prefix-sum step identities for degBefore and weightBefore #
Part 1: Deterministic O(k ^ 2) row-degree and total-weight initializer #
Inner loop body accumulating W[u, v] into val1.
Instances For
Outer loop body computing D[u] = rowDeg W u and adding it to totS.
Instances For
computeDegreesAndTotal initializes D[0..k) and totS in 6 * k ^ 2 + 12 * k + 6
instructions while modifying only Finset.Ico db (db + k).
Part 2: Single contraction step and contractDownTo #
Loop body for scanning row degrees D[u] until rem ≤ D[u].
Instances For
Scan u = 0, 1, … from rem = r + 1 to locate the sampled row u and row offset.
Instances For
Loop body for scanning row W[u, v] until rem ≤ W[u, v].
Instances For
Scan v = 0, 1, … along row u to locate the sampled column v (u ≠ v) with
val1 = W u v.
Instances For
Update totS, zero W[u, v], and update D[u] and D[v] for merging v into u.
Instances For
Copy (activeK, idx) and (idx, activeK) into (vReg, idx) and (idx, vReg) for
idx ≠ v.
Instances For
Single iteration of the Phase B swap-compaction sweep over idx < m.
Instances For
Setup for Phase B when v < m: copy D[m] to D[v] and compute row pointers for m and
v.
Instances For
One supervertex contraction step: deterministic merge when totS = 0, and edge-proportional
random contraction otherwise.
Instances For
Intermediate matrix invariants for mergeLoop and compactLoop #
Deterministic execution of uScan, vScan, and postRandStep #
Deterministic execution of uScan locating row u for a sampled offset r_val.
Deterministic execution of vScan locating column v along row u.
Execution of phaseADegrees, phaseAPtrs, and mergeLoop #
Deterministic execution of phaseADegrees updating totS, W[u, v], D[u], and D[v].
Execution of Phase B (compactSetup + compactLoop) and postRandStep #
Deterministic post-sampling execution: given x.regs rem = r_val with
IsSampledPair W r_val u v, postRandStep contracts (u, v) and swap-compacts Fin.last m into
slot v within 45 * (m + 1) + 22 instructions.
Simulation of the contraction structure and contractDownTo_spec #
Target survival predicate on final Data 16: memory holds a contracted t-supervertex
multigraph W' along part' where some active cut B' pulls back to C_star.
Instances For
contractDownTo refines the contraction structure. From a state holding a multigraph W
on k ≥ t ≥ 2 supervertices, contractDownTo runs within 36 (k² - t²) + 4 instructions and
k - t rand calls, writes only the workspace and ends holding a multigraph on t supervertices.
The graph it holds (heldGraph) has the law of k - t contractions of ⟨k, W⟩ by the client
Lean.Karger.contractTimes of Lean.Karger.spec.
Master specification for contractDownTo (Code .standard 16).
Starting from any ContractState w wb db str k t W x (2 ≤ t ≤ k) where W is a contraction of
W₀ along part, contractDownTo:
- terminates fault-free on every path (
ExecRand), - issues at most
k - trandcalls (IsQueryBounded (k - t)), - executes at most
36 * (k ^ 2 - t ^ 2) + 4instructions on every path, modifying onlyworkspaceFootprint wb db str kand leaving a validt-supervertexContractStatethat is a contraction ofW₀, and - preserves any fixed minimum cut
pullbackCut part Bof a connected multigraph (0 < minCut W₀) with probability at least(t * (t - 1)) / (k * (k - 1)).
The first three parts and the law of the held graph are contractDownTo_law. The contraction in
part 3 and the bound in part 4 are the specification's
(Lean.Karger.isContractionOf_of_mem_support_contractTimes, Lean.Karger.survives_contractTimes),
carried through that law.
At t = 2, any state satisfying SurvivalPost W₀ wb db str 2 C_star y where C_star is a
non-trivial cut attaining minCut W₀ stores minCut W₀ in y.mem (wb + 1).
Register-embedding (Code.rename) wrappers for callers with r ≥ 16 #
computeDegreesAndTotal embedded on registers 0..15 of any r-register file (16 ≤ r).
Instances For
contractDownTo embedded on registers 0..15 of any r-register file (16 ≤ r).
Instances For
Specification of computeDegreesAndTotalAt hr on any r-register file (16 ≤ r), preserving
all caller registers with index ≥ 16.
contractDownToAt hr simulates k - t contractions of Lean.Karger.spec on any r-register
file (16 ≤ r), as contractDownTo_law does, and preserves all caller registers with index
≥ 16.
Specification of contractDownToAt hr on any r-register file (16 ≤ r), preserving all
caller registers with index ≥ 16.