Documentation

CslibFrontier.DataStructure.AdjacencyList.Memory

Adjacency arrays over the memory oracle #

The read-only adjacency-list interface AdjacencyList.spec n is implemented over Memory.spec w by compressed adjacency arrays: a field offsets whose cell u holds the start of the list of u in the edge array, so that the out-degree of u is the difference of two consecutive offsets, and a field edges holding each edge as two cells, its target and its weight. This is the layout in which a word RAM receives a graph as input. A degree query reads two offsets, and an edge query reads an offset and the edge's two cells (amortized).

The interface never changes its state, so a refinement represents one graph adj: its invariant says that the arrays hold adj (Holds), and its abstraction function is constant. The methods write nothing (ops_triple), and the invariant reads only the two fields, so the arrays are framed by their fields and share memory with other structures (framed).

Main definitions #

Main results #

References #

Adjacency arrays: the offset of each vertex's list, and the edges, two cells each.

  • offsets : WordRAM.Block

    Cell u holds the position of the first edge out of u; cell n the end of the last list.

  • Edge j occupies cells 2 j (its target) and 2 j + 1 (its weight).

The offset field of a graph on n vertices has n + 1 cells.

  • offsets_length : L.offsets.length = n + 1

    One offset per vertex, and the end of the last list.

The out-degree of u: the difference of two consecutive offsets.

The i-th edge out of u: its target and weight, from the edge at the offset of u plus i. A target that is not a vertex cannot occur for an admissible query; the answer is then the spec's placeholder (u, 0).

The queries as memory programs.

@[reducible, inline]

The queries as methods of AdjacencyList.spec n over Memory.spec w.

structure Cslib.DataStructure.AdjacencyList.Memory.Holds {n : ℕ} (L : Layout) (adj : State n) (m : ℕ → ℕ) :

The memory m holds the graph adj in the layout: consecutive offsets differ by the out-degrees, the edges out of u follow its offset in order, and every list ends within the edge field.

The implementation invariant for the graph adj: both fields are allocated and hold it.

theorem Cslib.DataStructure.AdjacencyList.Memory.Holds.edge_lt {n : ℕ} {L : Layout} {adj : State n} {m : ℕ → ℕ} (h : Holds L adj m) (u : Fin n) {i : ℕ} (hi : i < (adj u).length) :
2 * (m (L.offsets.base + ↑u) + i) + 1 < L.edges.length

The cells an edge query reads lie in the edge field.

theorem Cslib.DataStructure.AdjacencyList.Memory.ops_triple {n : ℕ} {L : Layout} {w : ℕ} {adj : State n} (hL : L.Valid n) {s : Memory.State} (hs : Inv L adj s) (o : Op n) (hv : valid adj o) :
Memory.Triple w s (ops L o) 3 fun (r : o.Result) (s' : Memory.State) => step adj o = (adj, r) ∧ s' = s

Each query returns the specified answer within three memory units, and writes nothing.

theorem Cslib.DataStructure.AdjacencyList.Memory.ops_run {n : ℕ} {L : Layout} {w : ℕ} {adj : State n} (hL : L.Valid n) {s : Memory.State} (hs : Inv L adj s) (o : Op n) (hv : valid adj o) :

The run of a query, by ops_triple.

The adjacency arrays implement the interface for the graph adj they hold.

theorem Cslib.DataStructure.AdjacencyList.Memory.Inv.lt_frontier {n : ℕ} {L : Layout} {adj : State n} {s : Memory.State} (hs : Inv L adj s) {a : ℕ} (ha : a ∈ L.footprint) :

The fields are allocated.

theorem Cslib.DataStructure.AdjacencyList.Memory.Inv.congr {n : ℕ} {L : Layout} {adj : State n} (hL : L.Valid n) {s s' : Memory.State} (hs : Inv L adj s) (h : ∀ a ∈ L.footprint, s'.mem a = s.mem a) (hf : s.frontier ≤ s'.frontier) :
Inv L adj s'

The invariant reads only the two fields and survives a growing frontier.

The adjacency arrays, framed by their two fields.

@[simp]

The footprint of the arrays is their two fields.

theorem Cslib.DataStructure.AdjacencyList.Memory.amortized {n : ℕ} {L : Layout} {w : ℕ} {adj : State n} (hL : L.Valid n) :
(framed adj hL).Amortized Memory.charge (fun (x : (spec n).State) (x_1 : (spec n).Op) => 3) fun (x : (Memory.spec w).State) => 0

Every query costs at most three memory units.