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:
- Degrees and total (
mergeDegrees). Four reads, then the total becomesS - 2 W u vand the degree ofubecomes(D u - W u v) + (D v - W u v). - Merge (
mergeLoop). For everyc ∉ {u, v}, the entries(u, c)and(c, u)becomeW u c + W v c. Row and columnvare left as they are: compaction overwrites them, or they fall outside the graph. - Shrinking (
shrink). The size becomesm = k - 1. Whenvis notm, the degree ofmmoves to slotv, and row and columnmmove to row and columnv(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 #
mergedTo,compactedTo: the matrix part-way through each loop.mergeDegrees,mergeStep,mergeLoop,compactStep,compactLoop,compact,shrink,contract: the programs.
Main results #
mergedTo_eq_mergePair,compactedTo_eq,mergedTo_eq_contractMatrix,compactedTo_mergedTo_eq_contractMatrix: the loops computecontractMatrix.wp_mergeDegrees,wp_mergeLoop,wp_compactLoop,wp_compact,wp_shrink: the effect and cost of each phase.wp_contract:contract u vrealizescontractGraph, preserves the invariant, writes only the layout's cells and costs at most7 k + 7memory units.
The matrix during the loops #
Compaction reads (m, c), which it never writes.
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.
The merge alone computes contractMatrix when v is the last supervertex.
The merge and the compaction compute contractMatrix when v is not the last
supervertex.
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.
One merge step reads two entries and writes two.
One compaction step reads one entry and writes two.
Two writes of the same value inside the weight field leave every cell outside it unchanged.
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.
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.
The degree and total update reads four cells and writes two.
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.
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.
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.