Documentation

CslibFrontier.Algorithms.WordRAM.DFS.CSR

CSR graph representation, scratch layout, and word-RAM CSR primitives #

Register allocation (Reg), scratch memory layout (kOffAddr..kTopAddr), compressed sparse row predicates (csrOffset, CSRAt, PartialCSRAt), and shared array/CSR subroutines (zeroBlockCode, incCurCode, prefixSumCode, backfillStepCode).

Shared with CslibFrontier.Algorithms.WordRAM.TwoSAT.

Registers and scratch memory layout #

@[reducible, inline]

Constant 0.

@[reducible, inline]

Constant 1.

@[reducible, inline]

Constant 3 (words per DFS stack frame).

@[reducible, inline]

Vertex count N.

@[reducible, inline]

Base address of the CSR offset array being traversed.

@[reducible, inline]

Base address of the CSR edge array being traversed.

@[reducible, inline]

Base address of the visited-flag array.

@[reducible, inline]

Base address of the finishing-order output array.

@[reducible, inline]

Base address of the explicit DFS stack.

@[reducible, inline]

Base address of the transpose fill cursors.

@[reducible, inline]

Base address of the transpose CSR offset array.

@[reducible, inline]

Base address of the transpose CSR edge array.

@[reducible, inline]

Base address of the forward finishing order.

@[reducible, inline]

Base address of the component-index array.

@[reducible, inline]

Outer loop counter.

@[reducible, inline]

Stack depth or inner loop counter.

@[reducible, inline]

Number of finished vertices.

@[reducible, inline]

Current component index.

@[reducible, inline]

Finished count before the current root.

@[reducible, inline]

Current vertex.

@[reducible, inline]

Current successor.

@[reducible, inline]

Current edge position.

@[reducible, inline]

End of the current edge range.

@[reducible, inline]

Address scratch register.

@[reducible, inline]

Output-list header register (end-to-end programs).

Address of the forward CSR offset array in the scratch layout based at wb.

Address of the forward CSR edge array.

Address of the transpose CSR offset array.

Address of the transpose CSR edge array.

Address of the transpose fill cursors.

Address of the visited-flag array.

Address of the forward finishing order.

Address of the second-pass finishing order.

Address of the explicit DFS stack (3 words per frame).

Address of the component-index array.

First address past the scratch layout.

Reusable CSR representation and shared array/CSR primitives #

def Cslib.Algorithms.WordRAM.DFS.csrOffset {N : ℕ} (adj : Fin N → List (Fin N)) (u : ℕ) :

Number of arcs leaving vertices below u: the CSR offset of vertex u.

@[simp]
theorem Cslib.Algorithms.WordRAM.DFS.csrOffset_zero {N : ℕ} (adj : Fin N → List (Fin N)) :
csrOffset adj 0 = 0
theorem Cslib.Algorithms.WordRAM.DFS.csrOffset_succ {N : ℕ} (adj : Fin N → List (Fin N)) {u : ℕ} (hu : u < N) :
csrOffset adj (u + 1) = csrOffset adj u + (adj ⟨u, hu⟩).length
theorem Cslib.Algorithms.WordRAM.DFS.csrOffset_mono {N : ℕ} (adj : Fin N → List (Fin N)) {a b : ℕ} (hab : a ≤ b) :
csrOffset adj a ≤ csrOffset adj b
def Cslib.Algorithms.WordRAM.DFS.CSRAt {N : ℕ} (adj : Fin N → List (Fin N)) (offBase edgeBase : ℕ) (mem : ℕ → ℕ) :

adj is stored in CSR form: offsets at offBase, successor lists at edgeBase.

def Cslib.Algorithms.WordRAM.DFS.PartialCSRAt {N : ℕ} (targetAdj part : Fin N → List (Fin N)) (curBase edgeBase : ℕ) (mem : ℕ → ℕ) :

Invariant of the backward CSR fill: the successor lists part written so far end at the cursors curBase.

theorem Cslib.Algorithms.WordRAM.DFS.partialCSRAt_init {N : ℕ} (targetAdj : Fin N → List (Fin N)) (curBase edgeBase : ℕ) (mem : ℕ → ℕ) (hcur : ∀ (w : Fin N), mem (curBase + ↑w) = csrOffset targetAdj (↑w + 1)) :
PartialCSRAt targetAdj (fun (x : Fin N) => []) curBase edgeBase mem
theorem Cslib.Algorithms.WordRAM.DFS.csrAt_of_partialCSRAt {N : ℕ} (targetAdj : Fin N → List (Fin N)) (offBase curBase edgeBase : ℕ) (mem : ℕ → ℕ) (hoff : ∀ u ≤ N, mem (offBase + u) = csrOffset targetAdj u) (hp : PartialCSRAt targetAdj targetAdj curBase edgeBase mem) :
CSRAt targetAdj offBase edgeBase mem

Zero [baseReg, baseReg + nVerts) in 4 * N + 4 instructions via Cslib.WordRAM.zeroFill.

theorem Cslib.Algorithms.WordRAM.DFS.zeroBlockCode_spec {w : ℕ} (baseReg : Fin 28) (hbr : ↑baseReg < 18) (B N : ℕ) (hfit : WordRAM.Fits w (B + N)) {x : WordRAM.Data 28} (hc0 : x.regs Reg.cZero = 0) (hc1 : x.regs Reg.cOne = 1) (hB : x.regs baseReg = B) (hN : x.regs Reg.nVerts = N) :
∃ (y : WordRAM.Data 28), WordRAM.ExecWithin w (zeroBlockCode baseReg) x y (4 * N + 4) ∧ (∀ i < N, y.mem (B + i) = 0) ∧ (∀ (a : ℕ), ¬(B ≤ a ∧ a < B + N) → y.mem a = x.mem a) ∧ ∀ (r : Fin 28), ↑r < 18 → y.regs r = x.regs r

zeroBlockCode baseReg does not read the word width.

Increment mem[curB + vReg] by 1 in 4 instructions.

theorem Cslib.Algorithms.WordRAM.DFS.incCurCode_spec {N w : ℕ} (curBase maxDeg : ℕ) (hfit : WordRAM.Fits w (curBase + N + maxDeg + 1)) (deg : Fin N → ℕ) (src : Fin N) (hdeg_le : deg src + 1 ≤ maxDeg) {x : WordRAM.Data 28} (hc1 : x.regs Reg.cOne = 1) (hcur : x.regs Reg.curB = curBase) (hsrc : x.regs Reg.vReg = ↑src) (hmem : ∀ (u : Fin N), x.mem (curBase + ↑u) = deg u) :
∃ (y : WordRAM.Data 28), WordRAM.ExecWithin w incCurCode x y 4 ∧ (∀ (u : Fin N), y.mem (curBase + ↑u) = Function.update deg src (deg src + 1) u) ∧ (∀ (a : ℕ), ¬(curBase ≤ a ∧ a < curBase + N) → y.mem a = x.mem a) ∧ ∀ (r : Fin 28), r ≠ Reg.addr → r ≠ Reg.jEndReg → y.regs r = x.regs r

Prefix sums turning out-degrees at offB into CSR offsets and fill cursors.

theorem Cslib.Algorithms.WordRAM.DFS.prefixSumCode_spec {N w : ℕ} (targetAdj : Fin N → List (Fin N)) (degBase offBase Mc : ℕ) (hdisj : offBase + N + 1 ≤ degBase) (hmc : csrOffset targetAdj N ≤ Mc) (hfit : WordRAM.Fits w (degBase + N + Mc + 2)) {x : WordRAM.Data 28} (hc0 : x.regs Reg.cZero = 0) (hc1 : x.regs Reg.cOne = 1) (hdegB : x.regs Reg.curB = degBase) (hoffB : x.regs Reg.offB = offBase) (hL : x.regs Reg.nVerts = N) (hdeg : ∀ (u : Fin N), x.mem (degBase + ↑u) = (targetAdj u).length) :
∃ (y : WordRAM.Data 28), WordRAM.ExecWithin w prefixSumCode x y (9 * N + 5) ∧ (∀ u ≤ N, y.mem (offBase + u) = csrOffset targetAdj u) ∧ (∀ (u : Fin N), y.mem (degBase + ↑u) = csrOffset targetAdj (↑u + 1)) ∧ (∀ (a : ℕ), ¬(offBase ≤ a ∧ a < offBase + N + 1 ∨ degBase ≤ a ∧ a < degBase + N) → y.mem a = x.mem a) ∧ ∀ (r : Fin 28), ↑r < 18 → y.regs r = x.regs r

Decrement curB[vReg] and write uReg into edgeB[curB[vReg]] in 6 instructions.

theorem Cslib.Algorithms.WordRAM.DFS.backfillStepCode_spec {N w : ℕ} (targetAdj part : Fin N → List (Fin N)) (curBase edgeBase Mc : ℕ) (hdisj : edgeBase + Mc ≤ curBase) (hmc : csrOffset targetAdj N ≤ Mc) (hfit : WordRAM.Fits w (curBase + N + Mc + 1)) (src dst : Fin N) (hroom : (part src).length + 1 ≤ (targetAdj src).length) {x : WordRAM.Data 28} (hc1 : x.regs Reg.cOne = 1) (hcurB : x.regs Reg.curB = curBase) (hedgeB : x.regs Reg.edgeB = edgeBase) (hsrc : x.regs Reg.vReg = ↑src) (hdst : x.regs Reg.uReg = ↑dst) (hp : PartialCSRAt targetAdj part curBase edgeBase x.mem) :
∃ (y : WordRAM.Data 28), WordRAM.ExecWithin w backfillStepCode x y 6 ∧ PartialCSRAt targetAdj (Function.update part src (dst :: part src)) curBase edgeBase y.mem ∧ (∀ (a : ℕ), ¬(curBase ≤ a ∧ a < curBase + N ∨ edgeBase ≤ a ∧ a < edgeBase + csrOffset targetAdj N) → y.mem a = x.mem a) ∧ ∀ (r : Fin 28), r ≠ Reg.addr → r ≠ Reg.jEndReg → y.regs r = x.regs r