Documentation

CslibFrontier.Algorithms.Lean.Karger.Memory.Contract

The contraction structure over memory: contraction #

contract u v merges supervertex v into u and moves the last supervertex into slot v, in the layout of Karger.Memory.Layout. It is the memory-level counterpart of the machine-level postRandStep after the scans, in three phases, each with its own weakest-precondition lemma:

  1. Degrees and total (mergeDegrees). Four reads, then the total becomes S - 2 W u v and the degree of u becomes (D u - W u v) + (D v - W u v).
  2. Merge (mergeLoop). For every c ∉ {u, v}, the entries (u, c) and (c, u) become W u c + W v c. Row and column v are left as they are: compaction overwrites them, or they fall outside the graph.
  3. Shrinking (shrink). The size becomes m = k - 1. When v is not m, the degree of m moves to slot v, and row and column m move to row and column v (compact, compactLoop).

The matrix in memory part-way through each loop is a pure function of the matrix before it (mergedTo, compactedTo), and the matrix after both is contractMatrix (mergedTo_eq_contractMatrix, compactedTo_mergedTo_eq_contractMatrix). These are the only intermediate invariants, and they are stated on matrices, not on memory. The degree and total updates are the identities of WeightedCut.Contraction (rowDeg_mergePair_u, rowDeg_mergePair_other, rowDeg_contractMatrix, totalDoubledWeight_contractMatrix), used unchanged.

A contraction of k supervertices costs at most 7 k + 7 memory units (wp_contract). The machine-level counterparts are phaseADegrees_exec, mergeLoop_exec, compactPhase_exec and postRandStep_exec in Cslib.Algorithms.WordRAM.Karger.

Main definitions #

Main results #

The matrix during the loops #

The matrix M part-way through merging v into u: the entries (u, j) and (j, u) for j < c other than u and v hold M u j + M v j, and every other entry is M's.

The matrix M part-way through moving row and column m to row and column v: the entries (v, j) and (j, v) for j < c other than v hold M m j, and every other entry is M's.

@[simp]
theorem Cslib.Algorithms.Lean.Karger.Memory.mergedTo_zero {M : ℕ → ℕ → ℕ} {u v : ℕ} (i j : ℕ) :
mergedTo M u v 0 i j = M i j
theorem Cslib.Algorithms.Lean.Karger.Memory.mergedTo_succ_of_mem {M : ℕ → ℕ → ℕ} {u v c : ℕ} (hc : c = u ∨ c = v) :
mergedTo M u v (c + 1) = mergedTo M u v c

Merging skips u and v.

theorem Cslib.Algorithms.Lean.Karger.Memory.mergedTo_succ {M : ℕ → ℕ → ℕ} {u v c : ℕ} (hu : c ≠ u) (hv : c ≠ v) (i j : ℕ) :
mergedTo M u v (c + 1) i j = if i = c ∧ j = u then M u c + M v c else if i = u ∧ j = c then M u c + M v c else mergedTo M u v c i j

One merge step writes (u, c), then (c, u).

theorem Cslib.Algorithms.Lean.Karger.Memory.mergedTo_left {M : ℕ → ℕ → ℕ} {u v c : ℕ} (hu : c ≠ u) :
mergedTo M u v c u c = M u c

The merge reads (u, c) before writing it.

theorem Cslib.Algorithms.Lean.Karger.Memory.mergedTo_right {M : ℕ → ℕ → ℕ} {u v c : ℕ} (huv : u ≠ v) (hu : c ≠ u) :
mergedTo M u v c v c = M v c

The merge reads (v, c), which it never writes.

theorem Cslib.Algorithms.Lean.Karger.Memory.mergedTo_eq_mergePair {M : ℕ → ℕ → ℕ} {k : ℕ} {W : Fin k → Fin k → ℕ} (hW : WeightedCut.IsMultigraph W) (hM : ∀ (a b : Fin k), M ↑a ↑b = W a b) {u v a b : Fin k} (ha : a ≠ v) (hb : b ≠ v) :
mergedTo M (↑u) (↑v) k ↑a ↑b = WeightedCut.mergePair W u v a b

The merge computes mergePair away from v, on a multigraph.

@[simp]
theorem Cslib.Algorithms.Lean.Karger.Memory.compactedTo_zero {M : ℕ → ℕ → ℕ} {v m : ℕ} (i j : ℕ) :
compactedTo M v m 0 i j = M i j
theorem Cslib.Algorithms.Lean.Karger.Memory.compactedTo_succ_of_eq {M : ℕ → ℕ → ℕ} {v m c : ℕ} (hc : c = v) :
compactedTo M v m (c + 1) = compactedTo M v m c

Compaction skips v.

theorem Cslib.Algorithms.Lean.Karger.Memory.compactedTo_succ {M : ℕ → ℕ → ℕ} {v m c : ℕ} (hc : c ≠ v) (i j : ℕ) :
compactedTo M v m (c + 1) i j = if i = c ∧ j = v then M m c else if i = v ∧ j = c then M m c else compactedTo M v m c i j

One compaction step writes (v, c), then (c, v).

theorem Cslib.Algorithms.Lean.Karger.Memory.compactedTo_last {M : ℕ → ℕ → ℕ} {v m c : ℕ} (hm : m ≠ v) (hc : c ≠ v) :
compactedTo M v m c m c = M m c

Compaction reads (m, c), which it never writes.

theorem Cslib.Algorithms.Lean.Karger.Memory.compactedTo_eq {M : ℕ → ℕ → ℕ} {v m : ℕ} (hsym : ∀ i < m, i ≠ v → M m i = M i m) (hdiag : M v v = M m m) {i j : ℕ} (hi : i < m) (hj : j < m) :
compactedTo M v m m i j = M (if i = v then m else i) (if j = v then m else j)

Compaction moves row and column m into slot v, for a matrix symmetric in row and column m whose entries (v, v) and (m, m) agree.

theorem Cslib.Algorithms.Lean.Karger.Memory.val_embedCompact {m : ℕ} (v : Fin (m + 1)) (i : Fin m) :
↑(WeightedCut.embedCompact v i) = if ↑i = ↑v then m else ↑i

The index of embedCompact v i: m at the removed slot, i elsewhere.

theorem Cslib.Algorithms.Lean.Karger.Memory.mergedTo_eq_contractMatrix {M : ℕ → ℕ → ℕ} {m : ℕ} {W : Fin (m + 1) → Fin (m + 1) → ℕ} (hW : WeightedCut.IsMultigraph W) (hM : ∀ (a b : Fin (m + 1)), M ↑a ↑b = W a b) {u v : Fin (m + 1)} (hv : ↑v = m) (i j : Fin m) :
mergedTo M (↑u) (↑v) (m + 1) ↑i ↑j = WeightedCut.contractMatrix W u v i j

The merge alone computes contractMatrix when v is the last supervertex.

theorem Cslib.Algorithms.Lean.Karger.Memory.compactedTo_mergedTo_eq_contractMatrix {M : ℕ → ℕ → ℕ} {m : ℕ} {W : Fin (m + 1) → Fin (m + 1) → ℕ} (hW : WeightedCut.IsMultigraph W) (hM : ∀ (a b : Fin (m + 1)), M ↑a ↑b = W a b) {u v : Fin (m + 1)} (huv : u ≠ v) (hv : ↑v < m) (i j : Fin m) :
compactedTo (mergedTo M (↑u) (↑v) (m + 1)) (↑v) m m ↑i ↑j = WeightedCut.contractMatrix W u v i j

The merge and the compaction compute contractMatrix when v is not the last supervertex.

theorem Cslib.Algorithms.Lean.Karger.Memory.mergePair_left {m : ℕ} {W : Fin (m + 1) → Fin (m + 1) → ℕ} {u v c : Fin (m + 1)} (huv : u ≠ v) (hcu : c ≠ u) (hcv : c ≠ v) :
WeightedCut.mergePair W u v u c = W u c + W v c

The merged row u away from u and v.

theorem Cslib.Algorithms.Lean.Karger.Memory.mergePair_le_totalDoubledWeight {m : ℕ} {W : Fin (m + 1) → Fin (m + 1) → ℕ} (hW : WeightedCut.IsMultigraph W) {u v a b : Fin (m + 1)} (huv : u ≠ v) (ha : a ≠ v) (hb : b ≠ v) :

Entries of the merged matrix away from v are at most the total weight.

Degrees of the merged matrix away from v are at most the total weight.

The loops #

Merge index c: the entries (u, c) and (c, u) become W u c + W v c.

Merge v into u at the indices c, …, c + n - 1, skipping u and v.

Compact index c: the entries (v, c) and (c, v) become W m c.

Move row and column m into slot v at the indices c, …, c + n - 1, skipping v.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_mergeStep {L : Layout} {w : ℕ} {s : DataStructure.Memory.State} {u v c : ℕ} (hu : u < L.capacity) (hv : v < L.capacity) (hc : c < L.capacity) (hw : L.weight.base + L.weight.length ≤ s.frontier) (hfit : WordRAM.Fits w (L.mat s.mem u c + L.mat s.mem v c)) {Q : Unit → DataStructure.Memory.State → ℕ → Prop} (hQ : Q () { mem := Function.update (Function.update s.mem ((L.row u).base + c) (L.mat s.mem u c + L.mat s.mem v c)) ((L.row c).base + u) (L.mat s.mem u c + L.mat s.mem v c), frontier := s.frontier } 4) :

One merge step reads two entries and writes two.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_compactStep {L : Layout} {w : ℕ} {s : DataStructure.Memory.State} {v m c : ℕ} (hv : v < L.capacity) (hm : m < L.capacity) (hc : c < L.capacity) (hw : L.weight.base + L.weight.length ≤ s.frontier) (hfit : WordRAM.Fits w (L.mat s.mem m c)) {Q : Unit → DataStructure.Memory.State → ℕ → Prop} (hQ : Q () { mem := Function.update (Function.update s.mem ((L.row v).base + c) (L.mat s.mem m c)) ((L.row c).base + v) (L.mat s.mem m c), frontier := s.frontier } 3) :

One compaction step reads one entry and writes two.

theorem Cslib.Algorithms.Lean.Karger.Memory.update_update_of_not_owns {L : Layout} {m : ℕ → ℕ} {i j i' j' : ℕ} (hi : i < L.capacity) (hj : j < L.capacity) (hi' : i' < L.capacity) (hj' : j' < L.capacity) (x : ℕ) {a : ℕ} (ha : ¬L.weight.Owns a) :
Function.update (Function.update m ((L.row i).base + j) x) ((L.row i').base + j') x a = m a

Two writes of the same value inside the weight field leave every cell outside it unchanged.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_mergeLoop {L : Layout} {w : ℕ} {M : ℕ → ℕ → ℕ} {u v k : ℕ} (hu : u < k) (hv : v < k) (huv : u ≠ v) (hk : k ≤ L.capacity) (hfit : ∀ c < k, c ≠ u → c ≠ v → WordRAM.Fits w (M u c + M v c)) (n c : ℕ) (s : DataStructure.Memory.State) :
c + n ≤ k → L.weight.base + L.weight.length ≤ s.frontier → (∀ (i j : ℕ), i < L.capacity → j < L.capacity → L.mat s.mem i j = mergedTo M u v c i j) → (DataStructure.Memory.spec w).wp DataStructure.Memory.charge (mergeLoop L u v n c) (fun (x : Unit) (s' : (DataStructure.Memory.spec w).State) (t : ℕ) => s'.frontier = s.frontier ∧ (∀ (a : ℕ), ¬L.weight.Owns a → s'.mem a = s.mem a) ∧ (∀ (i j : ℕ), i < L.capacity → j < L.capacity → L.mat s'.mem i j = mergedTo M u v (c + n) i j) ∧ t ≤ 4 * n) s

The merge loop. From the matrix M merged up to index c, merging n more indices within k leaves it merged up to c + n, changes no cell outside the weight field, and costs at most 4 n memory units.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_compactLoop {L : Layout} {w : ℕ} {M : ℕ → ℕ → ℕ} {v m : ℕ} (hv : v < m) (hm : m < L.capacity) (hfit : ∀ c < m, c ≠ v → WordRAM.Fits w (M m c)) (n c : ℕ) (s : DataStructure.Memory.State) :
c + n ≤ m → L.weight.base + L.weight.length ≤ s.frontier → (∀ (i j : ℕ), i < L.capacity → j < L.capacity → L.mat s.mem i j = compactedTo M v m c i j) → (DataStructure.Memory.spec w).wp DataStructure.Memory.charge (compactLoop L v m n c) (fun (x : Unit) (s' : (DataStructure.Memory.spec w).State) (t : ℕ) => s'.frontier = s.frontier ∧ (∀ (a : ℕ), ¬L.weight.Owns a → s'.mem a = s.mem a) ∧ (∀ (i j : ℕ), i < L.capacity → j < L.capacity → L.mat s'.mem i j = compactedTo M v m (c + n) i j) ∧ t ≤ 3 * n) s

The compaction loop. From the matrix M compacted up to index c, compacting n more indices within m leaves it compacted up to c + n, changes no cell outside the weight field, and costs at most 3 n memory units.

Contraction #

Update the total and the degree of u for merging v into u.

Move the last supervertex m into slot v: its degree, then its row and column.

Shrink to m supervertices, moving supervertex m into slot v unless v is m.

Contraction: update the total and the degree of u, merge v into u, and move the last supervertex into slot v.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_mergeDegrees {L : Layout} {w : ℕ} {s : DataStructure.Memory.State} (hs : Inv L w s) {u v : ℕ} (hu : u < L.capacity) (hv : v < L.capacity) {S D : ℕ} (hS : s.mem L.total.base - 2 * s.mem ((L.row u).base + v) = S) (hD : s.mem (L.degree.base + u) - s.mem ((L.row u).base + v) + (s.mem (L.degree.base + v) - s.mem ((L.row u).base + v)) = D) (hSf : WordRAM.Fits w S) (hDf : WordRAM.Fits w D) {Q : Unit → DataStructure.Memory.State → ℕ → Prop} (hQ : Q () { mem := Function.update (Function.update s.mem L.total.base S) (L.degree.base + u) D, frontier := s.frontier } 6) :

The degree and total update reads four cells and writes two.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_compact {L : Layout} {w : ℕ} {s : DataStructure.Memory.State} {M : ℕ → ℕ → ℕ} {v m : ℕ} (hv : v < m) (hm : m < L.capacity) (hlim : L.limit ≤ s.frontier) (hd : WordRAM.Fits w (s.mem (L.degree.base + m))) (hfit : ∀ c < m, c ≠ v → WordRAM.Fits w (M m c)) (hmat : ∀ (i j : ℕ), i < L.capacity → j < L.capacity → L.mat s.mem i j = M i j) :
(DataStructure.Memory.spec w).wp DataStructure.Memory.charge (compact L v m) (fun (x : Unit) (s' : (DataStructure.Memory.spec w).State) (t : ℕ) => s'.frontier = s.frontier ∧ (∀ (a : ℕ), ¬L.weight.Owns a → a ≠ L.degree.base + v → s'.mem a = s.mem a) ∧ s'.mem (L.degree.base + v) = s.mem (L.degree.base + m) ∧ (∀ (i j : ℕ), i < L.capacity → j < L.capacity → L.mat s'.mem i j = compactedTo M v m m i j) ∧ t ≤ 3 * m + 2) s

The compaction phase. It copies the degree of m to slot v, moves row and column m into slot v, changes no other cell outside the weight field, and costs at most 3 m + 2.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_shrink {L : Layout} {w : ℕ} {s s₂ : DataStructure.Memory.State} (hs : Inv L w s) {m : ℕ} {W : Fin (m + 1) → Fin (m + 1) → ℕ} (hW : WeightedCut.IsMultigraph W) (hWm : ∀ (a b : Fin (m + 1)), L.mat s.mem ↑a ↑b = W a b) {u v : Fin (m + 1)} (huv : u ≠ v) (hk : m + 1 ≤ L.capacity) (hS : WordRAM.Fits w (WeightedCut.totalDoubledWeight W)) (hf : s₂.frontier = s.frontier) (hfr : ∀ a ∉ L.footprint, s₂.mem a = s.mem a) (htot : s₂.mem L.total.base = WeightedCut.totalDoubledWeight (WeightedCut.contractMatrix W u v)) (hdeg : ∀ (a : Fin (m + 1)), a ≠ v → s₂.mem (L.degree.base + ↑a) = WeightedCut.rowDeg (WeightedCut.mergePair W u v) a) (hmat : ∀ (i j : ℕ), i < L.capacity → j < L.capacity → L.mat s₂.mem i j = mergedTo (L.mat s.mem) (↑u) (↑v) (m + 1) i j) :
(DataStructure.Memory.spec w).wp DataStructure.Memory.charge (shrink L (↑v) m) (fun (x : Unit) (s' : (DataStructure.Memory.spec w).State) (t : ℕ) => s'.frontier = s.frontier ∧ (∀ a ∉ L.footprint, s'.mem a = s.mem a) ∧ abs L s' = { size := m, weight := WeightedCut.contractMatrix W u v } ∧ Inv L w s' ∧ t ≤ 3 * m + 3) s₂

The tail of a contraction. From the state after the merge, with the total and the degrees of the merged graph in place, shrinking to m supervertices ends in a state satisfying the invariant that abstracts to contractMatrix, and costs at most 3 m + 3.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_contract {L : Layout} {w : ℕ} {s : DataStructure.Memory.State} (hs : Inv L w s) {u v : ℕ} (hvalid : spec.valid (abs L s) (Op.contract u v)) :
(DataStructure.Memory.spec w).wp DataStructure.Memory.charge (contract L u v) (fun (x : Unit) (s' : (DataStructure.Memory.spec w).State) (t : ℕ) => s'.frontier = s.frontier ∧ (∀ a ∉ L.footprint, s'.mem a = s.mem a) ∧ abs L s' = contractGraph (abs L s) u v ∧ Inv L w s' ∧ t ≤ 7 * (abs L s).size + 7) s

Contraction over memory. On two distinct supervertices u and v, contract u v ends in a state satisfying the invariant that abstracts to contractGraph. It changes no cell outside the layout and costs at most 7 k + 7 memory units.