Documentation

CslibFrontier.Algorithms.Lean.Karger.Memory.Layout

The contraction structure over memory: layout and queries #

This file lays out the contraction structure Karger.spec in memory and implements its three queries as programs over the memory oracle Memory.spec w. The contraction itself is a separate module.

The storage for at most capacity supervertices is one contiguous region with four named fields (Layout): a cell size holding the number k of supervertices, a cell total holding the total doubled weight S, a degree array whose entry i holds the weighted degree of supervertex i, and the weight matrix, stored row by row with row stride capacity (Layout.row). The abstraction function reads the first k rows and columns of the matrix (abs). The invariant records that the degree array and the total cell are consistent with the matrix, that the matrix is a multigraph, and that the total fits a word (Inv).

size and total read one cell each. pairAt r finds the pair sampled at offset r by two prefix-sum scans (scan): along the degree array for the row u, then along row u for the column v. It reads at most 2 k + 1 cells (wp_pairAt). The scan is the single-draw sampling of Karger.Graph, and its answer is sampledPair (IsSampledPair, sampledPair_eq).

Programs are evaluated with the weakest precondition StateTransitionSpec.wp, which carries the answer, the final state and the memory charge. No register, word bound beyond Memory.valid, or instruction count appears. The machine-level counterparts are uScan_exec and vScan_exec in Cslib.Algorithms.WordRAM.Karger.

Main definitions #

Main results #

Layout #

The storage of the contraction structure for at most capacity supervertices: one region from address base, holding the fields size, total, degree and weight in that order.

  • base : ℕ

    The first address of the region.

  • capacity : ℕ

    The largest number of supervertices, and the row stride of the matrix.

The cell holding the number of supervertices.

The cell holding the total doubled weight.

The degree array: entry i holds the weighted degree of supervertex i.

The weight matrix, row by row, with row stride capacity.

One past the last address of the region.

Entry (i, j) of the weight matrix in memory m.

The graph on k supervertices that memory m holds.

Addresses #

A matrix entry within the capacity lies in the weight field.

theorem Cslib.Algorithms.Lean.Karger.Memory.Layout.row_base_add_inj {L : Layout} {i j a b : ℕ} (hj : j < L.capacity) (hb : b < L.capacity) (h : (L.row i).base + j = (L.row a).base + b) :
i = a ∧ j = b

Distinct matrix entries within the capacity have distinct addresses.

theorem Cslib.Algorithms.Lean.Karger.Memory.Layout.mat_update {L : Layout} {m : ℕ → ℕ} {a b : ℕ} (hb : b < L.capacity) (x : ℕ) {i j : ℕ} (hj : j < L.capacity) :
L.mat (Function.update m ((L.row a).base + b) x) i j = if i = a ∧ j = b then x else L.mat m i j

Writing a matrix entry within the capacity updates that entry of the matrix view.

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

Writing outside the weight field leaves the matrix view unchanged.

theorem Cslib.Algorithms.Lean.Karger.Memory.Layout.graph_congr {L : Layout} {k : ℕ} {m m' : ℕ → ℕ} (h : ∀ (i j : ℕ), i < k → j < k → L.mat m' i j = L.mat m i j) :
L.graph k m' = L.graph k m

The matrix view of the first k rows and columns depends only on those cells.

The abstraction function: the graph on the first k rows and columns of the matrix, where k is the size cell.

The implementation invariant at word size w. The region is allocated and the size is within the capacity, which fits a word. The matrix is a multigraph, the degree array and the total cell agree with it, and the total fits a word.

Every cell of the region is allocated.

theorem Cslib.Algorithms.Lean.Karger.Memory.abs_eq_and_inv {L : Layout} {w : ℕ} {s : DataStructure.Memory.State} {k : ℕ} {W : Fin k → Fin k → ℕ} (hW : WeightedCut.IsMultigraph W) (hlim : L.limit ≤ s.frontier) (hC : WordRAM.Fits w L.capacity) (hk : k ≤ L.capacity) (hsize : s.mem L.size.base = k) (hmat : ∀ (i j : Fin k), L.mat s.mem ↑i ↑j = W i j) (hdeg : ∀ (i : Fin k), s.mem (L.degree.base + ↑i) = WeightedCut.rowDeg W i) (htotal : s.mem L.total.base = WeightedCut.totalDoubledWeight W) (hfit : WordRAM.Fits w (WeightedCut.totalDoubledWeight W)) :
abs L s = { size := k, weight := W } ∧ Inv L w s

A state holding a graph. If the size cell holds k ≤ capacity, and the matrix, the degree array and the total cell hold a multigraph W, its degrees and its total, which fits a word, then the state abstracts to W and satisfies the invariant.

theorem Cslib.Algorithms.Lean.Karger.Memory.Inv.cells {L : Layout} {w : ℕ} {s : DataStructure.Memory.State} (hs : Inv L w s) {k : ℕ} {W : Fin k → Fin k → ℕ} (habs : abs L s = { size := k, weight := W }) :
s.mem L.size.base = k ∧ (∀ (i j : Fin k), L.mat s.mem ↑i ↑j = W i j) ∧ WeightedCut.IsMultigraph W ∧ (∀ (i : Fin k), s.mem (L.degree.base + ↑i) = WeightedCut.rowDeg W i) ∧ s.mem L.total.base = WeightedCut.totalDoubledWeight W

The cells of a state satisfying the invariant hold its abstraction.

Matrix entries within the capacity belong to the region.

Degree cells within the capacity belong to the region.

The size, total and degree cells lie outside the weight field.

The size, total and degree cells are distinct.

A cell outside the region belongs to no field.

The weight field is allocated.

theorem Cslib.Algorithms.Lean.Karger.Memory.abs_congr {L : Layout} {w : ℕ} {s s' : DataStructure.Memory.State} (hs : Inv L w s) (h : ∀ a ∈ L.footprint, s'.mem a = s.mem a) :
abs L s' = abs L s

The abstraction reads only the region.

theorem Cslib.Algorithms.Lean.Karger.Memory.Inv.congr {L : Layout} {w : ℕ} {s s' : DataStructure.Memory.State} (hs : Inv L w s) (h : ∀ a ∈ L.footprint, s'.mem a = s.mem a) (hf : s.frontier ≤ s'.frontier) :
Inv L w s'

The invariant reads only the region, and survives a growing frontier.

Prefix sums #

theorem Cslib.Algorithms.Lean.Karger.Memory.degBefore_eq_sum_range {k : ℕ} (W : Fin k → Fin k → ℕ) (u : Fin k) (g : ℕ → ℕ) (hg : ∀ (j : ℕ) (hj : j < k), g j = WeightedCut.rowDeg W ⟨j, hj⟩) :
Karger.degBefore W u = ∑ j ∈ Finset.range ↑u, g j

The degrees before u as a sum over a range.

theorem Cslib.Algorithms.Lean.Karger.Memory.weightBefore_eq_sum_range {k : ℕ} (W : Fin k → Fin k → ℕ) (u v : Fin k) (g : ℕ → ℕ) (hg : ∀ (j : ℕ) (hj : j < k), g j = W u ⟨j, hj⟩) :
Karger.weightBefore W u v = ∑ j ∈ Finset.range ↑v, g j

The weights of row u before column v as a sum over a range.

theorem Cslib.Algorithms.Lean.Karger.Memory.rowDeg_eq_sum_range {k : ℕ} (W : Fin k → Fin k → ℕ) (u : Fin k) (g : ℕ → ℕ) (hg : ∀ (j : ℕ) (hj : j < k), g j = W u ⟨j, hj⟩) :
WeightedCut.rowDeg W u = ∑ j ∈ Finset.range k, g j

The weighted degree as a sum over a range.

theorem Cslib.Algorithms.Lean.Karger.Memory.totalDoubledWeight_eq_sum_range {k : ℕ} (W : Fin k → Fin k → ℕ) (g : ℕ → ℕ) (hg : ∀ (j : ℕ) (hj : j < k), g j = WeightedCut.rowDeg W ⟨j, hj⟩) :

The total doubled weight as a sum of degrees over a range.

Queries #

Prefix-sum scan. Read the entries i, i + 1, … of the field b, at most n of them, subtracting each from the offset r until an entry exceeds what remains. Answer that entry's index and the remaining offset.

The pair sampled at offset r: scan the degree array for the row, then that row for the column.

theorem Cslib.Algorithms.Lean.Karger.Memory.wp_scan {w : ℕ} {b : WordRAM.Block} {s : DataStructure.Memory.State} (n i r : ℕ) (Q : ℕ × ℕ → DataStructure.Memory.State → ℕ → Prop) :
(∀ j < i + n, b.base + j < s.frontier) → r < ∑ j ∈ Finset.Ico i (i + n), s.mem (b.base + j) → (∀ (u : ℕ), i ≤ u → u < i + n → ∑ j ∈ Finset.Ico i u, s.mem (b.base + j) ≤ r → r < ∑ j ∈ Finset.Ico i (u + 1), s.mem (b.base + j) → Q (u, r - ∑ j ∈ Finset.Ico i u, s.mem (b.base + j)) s (u + 1 - i)) → (DataStructure.Memory.spec w).wp DataStructure.Memory.charge (scan b n i r) Q s

The scan finds the sampled entry. If the offset r is below the sum of the n entries from i, the scan reads the entries i, …, u and answers the first index u whose prefix sum exceeds r, together with what remains of r after the entries before u.

The degree array as a sum over a range is the total doubled weight.

The sampling query. At an offset below the total weight, pairAt r answers the pair sampled at r, leaves memory unchanged, and reads at most 2 k + 1 cells.