Documentation

Complexity.Algorithms.WordRAM.KargerMinCut

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:

  1. Single-trial contraction (singleProgram, r = 16): runs on n :: flattenMatrix W, computes row degrees at 2 + n ^ 2, contracts in place down to 2 supervertices in at most 42 * n ^ 2 + 12 * n + 21 steps and n - 2 rand calls, outputs a genuine non-trivial cut weight cutWeight W C ≥ minCut W on every supported branch, and achieves minCut W with probability at least 2 / (n * (n - 1)) whenever 0 < minCut W.
  2. Repeated-trial amplification (amplifiedProgram, r = 22): runs on R :: n :: flattenMatrix W (1 ≤ R), copies the immutable input matrix from [3, 3 + n ^ 2) into [3 + n ^ 2, 3 + 2 * n ^ 2) via Cslib.WordRAM.ArrayCopy before each of R independent contractions, halts within R * (50 * n ^ 2 + 12 * n + 30) + 16 steps and R * (n - 2) rand calls with a genuine non-trivial cut weight ≥ minCut W, and misses minCut W with probability at most (1 - 2 / (n * (n - 1))) ^ R whenever 0 < minCut W.
  3. Explicit O(n ^ 4 log n) high-probability global minimum cut (highProbProgram, r = 22): runs on n :: flattenMatrix W, computes highProbTrials n = (n * (n - 1) / 2) * (⌊log₂ n⌋ + 2) in registers using shifts and multiplication (without .div or .mod), halts within 33 * n ^ 4 * (Nat.log 2 n + 2) steps with a genuine non-trivial cut weight ≥ minCut W, and misses minCut W with probability at most 1 / (2 * n) (hence at most 1 / n) whenever 0 < minCut W, via an elementary Bernoulli power bound without Real.exp.

Source statements and notation mapping #

Main definitions #

Main results #

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 and intermediate n * (n - 1) and Nat.log 2 n + 2 values fit in w-bit words.

logWidth 3 n ≤ w and Fits w (totalDoubledWeight W) suffice for InputFits w (n :: flattenMatrix W).

theorem Cslib.Algorithms.WordRAM.Karger.inputFits_amplified {R n w : ℕ} {W : Fin n → Fin n → ℕ} (hn : 2 ≤ n) (hw : WordRAM.logWidth 3 n ≤ w) (hR : WordRAM.Fits w R) (htot : WordRAM.Fits w (totalDoubledWeight W)) :

logWidth 3 n ≤ w, Fits w R, and Fits w (totalDoubledWeight W) suffice for InputFits w (R :: n :: flattenMatrix W).

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.

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
            theorem Cslib.Algorithms.WordRAM.Karger.singlePost_exec {w wb : ℕ} {x : WordRAM.Data 16} (hwBase : x.regs Reg.wBase = wb) (h1 : WordRAM.Fits w 1) (hwb1 : WordRAM.Fits w (wb + 1)) :
            ∃ (y : WordRAM.Data 16), WordRAM.Exec w singlePost x y 3 ∧ y.mem = x.mem ∧ y.regs Reg.val1 = x.mem (wb + 1)

            Structured ProbComp specification of singleCode.

            Single-trial Basic Karger specification (singleProgram). At word size w ≥ logWidth 3 n when totalDoubledWeight W < 2 ^ w:

            1. singleProgram halts within singleTime n = 42 * n ^ 2 + 12 * n + 21 steps;
            2. executes at most n - 2 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, 2 + n * n + n);
            4. when 0 < minCut W (excluded only for proof convenience; S = 0 contraction still halts fault-free with a valid cut weight), outputs minCut W with probability at least 2 / (n * (n - 1)).

            Shared R-trial amplification engine (r = 22) #

            @[reducible, inline]

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

            Instances For
              @[reducible, inline]

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

              Instances For
                @[reducible, inline]

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

                Instances For
                  @[reducible, inline]

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

                  Instances For
                    @[reducible, inline]

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

                    Instances For
                      @[reducible, inline]

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

                      Instances For
                        @[reducible, inline]

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

                        Instances For
                          @[reducible, inline]

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

                          Instances For
                            @[reducible, inline]

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

                            Instances For
                              @[reducible, inline]

                              Loop counter / remainder scratch register (Reg.rem embedded into Fin 22).

                              Instances For
                                @[reducible, inline]

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

                                Instances For
                                  @[reducible, inline]

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

                                  Instances For
                                    @[reducible, inline]

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

                                    Instances For
                                      @[reducible, inline]

                                      Base address of the immutable input matrix W.

                                      Instances For
                                        @[reducible, inline]

                                        Original vertex count n, preserved across all trials.

                                        Instances For
                                          @[reducible, inline]

                                          Word count n * n of the flattened matrix.

                                          Instances For
                                            @[reducible, inline]

                                            Total number R of independent trials to execute.

                                            Instances For
                                              @[reducible, inline]

                                              Number of trials completed so far (0 .. R).

                                              Instances For
                                                @[reducible, inline]

                                                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

                                                    Postprocessing at the end of each trial: extract mem[wBase + 1], update bestVal (replacing it unconditionally on trial 0 and taking the minimum thereafter), and increment trialIdx.

                                                    Instances For

                                                      Single amplification trial: copy [srcBase, srcBase + n * n) into wBase, initialize row degrees at dBase, contract down to 2 supervertices, and update bestVal.

                                                      Instances For

                                                        Loop running numTrials independent contractions.

                                                        Instances For

                                                          Shared amplification core assuming srcBase, nOrig, and numTrials are initialized in registers.

                                                          Instances For
                                                            structure Cslib.Algorithms.WordRAM.Karger.AmpInv (w sb n R i : ℕ) (W : Fin n → Fin n → ℕ) (mem₀ : ℕ → ℕ) (x : WordRAM.Data 22) :

                                                            Loop invariant for ampLoop at trialIdx = i ≤ R.

                                                            Instances For
                                                              theorem Cslib.Algorithms.WordRAM.Karger.ampTrialBody_spec {w sb n R i : ℕ} {W : Fin n → Fin n → ℕ} {mem₀ : ℕ → ℕ} {x : WordRAM.Data 22} (hn : 2 ≤ n) (hW : IsMultigraph W) (hiR : i < R) (h1 : WordRAM.Fits w 1) (hdEnd : WordRAM.Fits w (sb + 2 * (n * n) + n)) (hR_fit : WordRAM.Fits w R) (htot : WordRAM.Fits w (totalDoubledWeight W)) (hinv : AmpInv w sb n R i W mem₀ x) :
                                                              ∃ (m : ProbComp (WordRAM.Data 22 × ℕ)), WordRAM.ExecRand w ampTrialBody x m ∧ OracleComp.IsQueryBounded m (n - 2) ∧ (∀ z ∈ OracleComp.support m, z.2 ≤ 49 * n ^ 2 + 12 * n + 27 ∧ AmpInv w sb n R (i + 1) W mem₀ z.1 ∧ (0 < i → x.regs AmpReg.bestVal = minCut W → z.1.regs AmpReg.bestVal = minCut W)) ∧ (0 < minCut W → m.eval.toOuterMeasure {z : WordRAM.Data 22 × ℕ | z.1.regs AmpReg.bestVal ≠ minCut W} ≤ 1 - 2 / ↑(n * (n - 1)))

                                                              Specification of a single trial ampTrialBody from AmpInv.

                                                              theorem Cslib.Algorithms.WordRAM.Karger.ampLoop_spec {w sb n R : ℕ} {W : Fin n → Fin n → ℕ} {mem₀ : ℕ → ℕ} (hn : 2 ≤ n) (hW : IsMultigraph W) (h1 : WordRAM.Fits w 1) (hdEnd : WordRAM.Fits w (sb + 2 * (n * n) + n)) (hR_fit : WordRAM.Fits w R) (htot : WordRAM.Fits w (totalDoubledWeight W)) (rem i : ℕ) (x : WordRAM.Data 22) :
                                                              i + rem = R → AmpInv w sb n R i W mem₀ x → ∃ (m : ProbComp (WordRAM.Data 22 × ℕ)), WordRAM.ExecRand w ampLoop x m ∧ OracleComp.IsQueryBounded m (rem * (n - 2)) ∧ (∀ z ∈ OracleComp.support m, z.2 ≤ rem * (50 * n ^ 2 + 12 * n + 30) + 2 ∧ AmpInv w sb n R R W mem₀ z.1 ∧ (0 < i → x.regs AmpReg.bestVal = minCut W → z.1.regs AmpReg.bestVal = minCut W)) ∧ (0 < minCut W → m.eval.toOuterMeasure {z : WordRAM.Data 22 × ℕ | z.1.regs AmpReg.bestVal ≠ minCut W} ≤ (1 - 2 / ↑(n * (n - 1))) ^ rem)

                                                              Specification of ampLoop over rem = R - i remaining trials.

                                                              theorem Cslib.Algorithms.WordRAM.Karger.ampCore_spec {w sb n R : ℕ} {W : Fin n → Fin n → ℕ} {x : WordRAM.Data 22} (hR : 1 ≤ R) (hn : 2 ≤ n) (hW : IsMultigraph W) (hsb : x.regs AmpReg.srcBase = sb) (hnOrig : x.regs AmpReg.nOrig = n) (hnumT : x.regs AmpReg.numTrials = R) (h1 : WordRAM.Fits w 1) (hdEnd : WordRAM.Fits w (sb + 2 * (n * n) + n)) (hR_fit : WordRAM.Fits w R) (htot : WordRAM.Fits w (totalDoubledWeight W)) (hsrc_mat : HoldsMatrix x.mem sb n W) :
                                                              ∃ (m : ProbComp (WordRAM.Data 22 × ℕ)), WordRAM.ExecRand w ampCore x m ∧ OracleComp.IsQueryBounded m (R * (n - 2)) ∧ (∀ z ∈ OracleComp.support m, z.2 ≤ R * (50 * n ^ 2 + 12 * n + 30) + 8 ∧ WordRAM.Modifies (Finset.Ico (sb + n * n) (sb + 2 * (n * n) + n)) x.mem z.1.mem ∧ ∃ (C : Finset (Fin n)), IsNonTrivialCut C ∧ z.1.regs AmpReg.bestVal = cutWeight W C) ∧ (0 < minCut W → m.eval.toOuterMeasure {z : WordRAM.Data 22 × ℕ | z.1.regs AmpReg.bestVal ≠ minCut W} ≤ (1 - 2 / ↑(n * (n - 1))) ^ R)

                                                              Specification of ampCore for 1 ≤ R trials from any state with srcBase = sb, nOrig = n, numTrials = R, and HoldsMatrix x.mem sb 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
                                                                      theorem Cslib.Algorithms.WordRAM.Karger.amplifiedCode_spec {R n w : ℕ} {W : Fin n → Fin n → ℕ} (hR : 1 ≤ R) (hn : 2 ≤ n) (hW : IsMultigraph W) (hw : WordRAM.logWidth 3 n ≤ w) (hR_fit : WordRAM.Fits w R) (htot : WordRAM.Fits w (totalDoubledWeight W)) :

                                                                      Structured ProbComp specification of amplifiedCode.

                                                                      R-trial amplified Karger specification (amplifiedProgram). At word size w ≥ logWidth 3 n when 1 ≤ R, R < 2 ^ w, and totalDoubledWeight W < 2 ^ w:

                                                                      1. amplifiedProgram halts within amplifiedTime R n = R * (50 * n ^ 2 + 12 * n + 30) + 16 steps;
                                                                      2. executes at most R * (n - 2) 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 [3 + n * n, 3 + 2 * (n * n) + n);
                                                                      4. when 0 < minCut W (excluded only for proof convenience; S = 0 contraction still halts fault-free with a valid cut weight), misses minCut W with probability at most (1 - 2 / (n * (n - 1))) ^ R.

                                                                      Endpoint 3: Explicit O(n ^ 4 log n) high-probability global minimum cut (r = 22) #

                                                                      Body of logLoop: halve rem via right-shift by 1 and increment idx.

                                                                      Instances For

                                                                        Loop computing idx = Nat.log 2 n from rem = n (n ≥ 1), idx = 0, one = 1.

                                                                        Instances For

                                                                          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
                                                                                  theorem Cslib.Algorithms.WordRAM.Karger.logLoop_exec {w n : ℕ} {x : WordRAM.Data 22} (hn : 1 ≤ n) (hone : x.regs AmpReg.one = 1) (hrem : x.regs AmpReg.rem = n) (hidx : x.regs AmpReg.idx = 0) (hnOrig : x.regs AmpReg.nOrig = n) (hn_fit : WordRAM.Fits w n) (hlg_fit : WordRAM.Fits w (Nat.log 2 n)) :

                                                                                  Structured ProbComp specification of highProbCode.

                                                                                  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:

                                                                                  1. highProbProgram halts within highProbTime n = 33 * n ^ 4 * (Nat.log 2 n + 2) steps;
                                                                                  2. executes at most highProbTrials n * (n - 2) 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 + 2 * (n * n) + n);
                                                                                  4. when 0 < minCut W (excluded only for proof convenience; S = 0 contraction still halts fault-free with a valid cut weight), misses minCut W with probability at most 1 / (2 * n).

                                                                                  Corollary of highProbProgram_spec: when 0 < minCut W, highProbProgram misses minCut W with probability at most 1 / n.