Documentation

CslibFrontier.Algorithms.WordRAM.CSR

Compressed-sparse-row word encoding of weighted directed graphs #

Shared input word-list layout and Cslib.WordRAM.Data.init memory-readback lemmas for weighted directed multigraphs on Fin n given by adjacency lists adj : Fin n → List (Fin n × W), a source vertex source : Fin n, and an edge-weight encoder encodeWeight : W → ℕ.

The encoded word list (Cslib.Algorithms.WordRAM.CSR.encode) stores:

Used by both CslibFrontier.Algorithms.WordRAM.Dijkstra (encodeWeight := id on ℕ) and CslibFrontier.Algorithms.WordRAM.BellmanFord (encodeWeight := Cslib.WordRAM.Signed.encode on ℤ).

Main definitions #

Main results #

def Cslib.Algorithms.WordRAM.CSR.m {W : Type u_1} (n : ℕ) (adj : Fin n → List (Fin n × W)) :

Total number of directed edge entries across all adjacency lists.

def Cslib.Algorithms.WordRAM.CSR.offset {W : Type u_1} (n : ℕ) (adj : Fin n → List (Fin n × W)) (u : ℕ) :

Cumulative number of edges leaving vertices < u.

def Cslib.Algorithms.WordRAM.CSR.encode {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) :

CSR word-list encoding: n, source, the n + 1 offsets offset 0, …, offset n, and all edges in vertex order as two words (target, encodeWeight weight).

def Cslib.Algorithms.WordRAM.CSR.dbase {W : Type u_1} (n : ℕ) (adj : Fin n → List (Fin n × W)) :

First memory address past the encoded input in Data.init.

theorem Cslib.Algorithms.WordRAM.CSR.offset_add_lt_m {W : Type u_1} (n : ℕ) (adj : Fin n → List (Fin n × W)) (u : Fin n) {i : ℕ} (hi : i < (adj u).length) :
offset n adj ↑u + i < m n adj
theorem Cslib.Algorithms.WordRAM.CSR.encode_length {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) :
(encode n source adj encodeWeight).length = n + 2 * m n adj + 3

The encoded input has n + 2 * m + 3 words.

theorem Cslib.Algorithms.WordRAM.CSR.offset_succ {W : Type u_1} (n : ℕ) (adj : Fin n → List (Fin n × W)) {u : ℕ} (hu : u < n) :
offset n adj (u + 1) = offset n adj u + (adj ⟨u, hu⟩).length

The offset of u + 1 is the offset of u plus (adj u).length.

theorem Cslib.Algorithms.WordRAM.CSR.offset_n {W : Type u_1} (n : ℕ) (adj : Fin n → List (Fin n × W)) :
offset n adj n = m n adj

The offset of n is the total number of edges m.

theorem Cslib.Algorithms.WordRAM.CSR.offset_mono {W : Type u_1} (n : ℕ) (adj : Fin n → List (Fin n × W)) {u v : ℕ} (h : u ≤ v) :
offset n adj u ≤ offset n adj v

Offsets are monotone.

theorem Cslib.Algorithms.WordRAM.CSR.init_len {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {r : ℕ} :
(WordRAM.Data.init (encode n source adj encodeWeight)).mem 0 = n + 2 * m n adj + 3

Address 0 initially holds the input length n + 2 * m + 3.

theorem Cslib.Algorithms.WordRAM.CSR.init_n {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {r : ℕ} :
(WordRAM.Data.init (encode n source adj encodeWeight)).mem 1 = n

Address 1 initially holds n.

theorem Cslib.Algorithms.WordRAM.CSR.init_source {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {r : ℕ} :
(WordRAM.Data.init (encode n source adj encodeWeight)).mem 2 = ↑source

Address 2 initially holds source.

theorem Cslib.Algorithms.WordRAM.CSR.init_offset {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {r u : ℕ} (hu : u ≤ n) :
(WordRAM.Data.init (encode n source adj encodeWeight)).mem (u + 3) = offset n adj u

Address u + 3 initially holds offset u, for u ≤ n.

theorem Cslib.Algorithms.WordRAM.CSR.init_target {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {r : ℕ} (u : Fin n) {i : ℕ} (hi : i < (adj u).length) :
(WordRAM.Data.init (encode n source adj encodeWeight)).mem (2 * (offset n adj ↑u + i) + n + 4) = ↑(adj u)[i].1

The target of edge i out of u is initially at address 2 * (offset u + i) + n + 4.

theorem Cslib.Algorithms.WordRAM.CSR.init_weight {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {r : ℕ} (u : Fin n) {i : ℕ} (hi : i < (adj u).length) :
(WordRAM.Data.init (encode n source adj encodeWeight)).mem (2 * (offset n adj ↑u + i) + n + 5) = encodeWeight (adj u)[i].2

The encoded weight of edge i out of u is initially at address 2 * (offset u + i) + n + 5.

theorem Cslib.Algorithms.WordRAM.CSR.init_past {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {r a : ℕ} (ha : dbase n adj ≤ a) :
(WordRAM.Data.init (encode n source adj encodeWeight)).mem a = 0

Memory at or past dbase is initially 0.

theorem Cslib.Algorithms.WordRAM.CSR.inputFits_encode {W : Type u_1} (n : ℕ) (source : Fin n) (adj : Fin n → List (Fin n × W)) (encodeWeight : W → ℕ) {B w : ℕ} (hlen : n + 2 * m n adj + 3 ≤ B) (hw_le : ∀ (u : Fin n), ∀ e ∈ adj u, encodeWeight e.2 ≤ B) (hfit : WordRAM.Fits w B) :
WordRAM.InputFits w (encode n source adj encodeWeight)

encode satisfies InputFits w whenever n + 2 * m + 3 ≤ B, every encoded weight is at most B, and Fits w B.