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 #
Layout,Layout.size,Layout.total,Layout.degree,Layout.weight,Layout.row: the storage and its fields.Layout.mat,Layout.graph,abs: the matrix view of memory and the abstraction function.Inv: the implementation invariant.scan,pairAt: the prefix-sum scan and the sampling query.
Main results #
wp_scan: a scan finds the first entry whose prefix sum exceeds the offset.wp_pairAt:pairAt ranswerssampledPairand reads at most2 k + 1cells.degBefore_eq_sum_range,weightBefore_eq_sum_range,totalDoubledWeight_eq_sum_range,rowDeg_eq_sum_range: the prefix sums ofKarger.Graphas sums over ranges.
Layout #
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.
Row i of the weight matrix.
One past the last address of the region.
The cells of the region.
Addresses #
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.
The region is allocated.
- capacity_fits : WordRAM.Fits w L.capacity
The capacity fits a word, hence so does every size.
The size is within the capacity.
The matrix is a multigraph.
- degree_eq (i : Fin (abs L s).size) : s.mem (L.degree.base + ↑i) = WeightedCut.rowDeg (abs L s).weight i
The degree array holds the weighted degrees.
The total cell holds the total doubled weight.
The total fits a word.
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.
Prefix sums #
Queries #
Read the number of supervertices.
Read the total doubled weight.
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.
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.