Documentation

CslibFrontier.Algorithms.Lean.Dijkstra.Memory

Dijkstra's algorithm with a binary heap, over the memory oracle #

The client Cslib.Algorithms.Lean.Dijkstra.dijkstra runs over three structures in one memory (framed, a Memory.Framed.prod): the indexed binary heap Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.framed, a direct-address table for the tentative distances, and the adjacency arrays of the graph. run is one memory program for every graph. It reads the word RAM's input convention (ListInstance.encode): it reads n, the source and the number m of edges, allocates the distance table and the heap just past the input, where memory is zero, and runs the linked client on the adjacency arrays of the input.

run_spec is the memory-level counterpart of Cslib.Algorithms.WordRAM.Dijkstra.LogLinearSpec: at every word size that holds 8 (n + m + 2) (W + 2), every memory operation of run is admissible, the cell G.outAddr v then holds the encoded distance from the source to v, and the run costs at most 52 (m + 1) (⌊log₂ n⌋ + 1) memory units. exists_run_logLinear exports the bound as O((m + 1) log n). Correctness is dijkstra_correct transported through the refinement (Refinement.simulates_client); the cost is the client's operation count (totalCost_dijkstra_le) at the structures' charges, transported by their amortized certificates (Refinement.Amortized.client). The proofs here mention no register, instruction or address beyond the layout of the input.

Executing memory programs on the word RAM is a separate, generic realization step, which this file does not provide.

Main definitions #

Main results #

References #

The client does not depend on its bounds #

The main loop does not depend on the key and value bounds K and V, which only restrict admissibility.

theorem Cslib.Algorithms.Lean.Dijkstra.Memory.dijkstra_eq {n : ℕ} (K V K' V' : ℕ) (src : Fin n) :

The client does not depend on the key and value bounds K and V.

The structures #

The adjacency arrays of the input convention: the n + 1 offsets at address 3, and the m edges from address n + 4.

The heap, the distance table and the adjacency arrays of the graph adj, as one framed refinement of the interfaces of Dijkstra's algorithm.

The methods do not depend on the bounds and the word size in their type.

@[reducible, inline]
abbrev Cslib.Algorithms.Lean.Dijkstra.Memory.opCharge (n K V : ℕ) :
(spec n K V).State → (spec n K V).Op → ℕ

The charge of the composite: a heap operation costs opCost n, a distance-table operation one unit and a graph query three units.

Every operation of the composite costs at most its charge, with no potential.

The program #

Dijkstra's algorithm over memory on the word RAM's input convention: read n, the source and the number m of edges, allocate the distance table and the heap past the input, and run the client through the three structures. The key bound, value bound and word size in the types of memMethods and dijkstra are 0: they only restrict admissibility, and the program does not depend on them (dijkstra_eq).

The input #

The input words of G end just below address n + 2 m + 4, the initial frontier.

In the encoded graph input, cell G.n + 3 holds the edge count G.m.

What a degree query reads from the input adjacency arrays: the offsets of u and u + 1 lie below the frontier, and differ by the out-degree of u.

theorem Cslib.Algorithms.Lean.Dijkstra.Memory.inputGraph_edge {n m : ℕ} {adj : DataStructure.AdjacencyList.State n} {s : DataStructure.Memory.State} (hA : DataStructure.AdjacencyList.Memory.Inv (inputGraph n m) adj s) (u : Fin n) {i : ℕ} (hi : i < (adj u).length) :
3 + ↑u < s.frontier ∧ n + 4 + (2 * (s.mem (3 + ↑u) + i) + 1) < s.frontier ∧ s.mem (n + 4 + 2 * (s.mem (3 + ↑u) + i)) = ↑(adj u)[i].1 ∧ s.mem (n + 4 + (2 * (s.mem (3 + ↑u) + i) + 1)) = (adj u)[i].2

What an edge query reads from the input adjacency arrays: the offset of u and both words of its edge i lie below the frontier, and the words hold the target and the weight of the edge.

The distance table and the graph after allocation. Past the input of G, a distance table allocated without clearing, together with the adjacency arrays of the input, represent an empty distance map and the graph at every later frontier F that fits a word, and all their cells lie below f + n, where f is the initial frontier. This is what both heaps' inv_input share.

The structures after allocation. Past the input of G, a distance table and then a heap, allocated without clearing, together with the adjacency arrays of the input, represent the initial state of Dijkstra's algorithm on G: an empty queue, an empty distance map and the graph. The structures share memory only while its frontier fits a word (Memory.Framed.Apart).

A cell whose decoded value is some d exactly when the distance D is d holds the encoded distance.

Correctness and cost #

The cost of Dijkstra's algorithm over memory, at most: 52 (m + 1) (⌊log₂ n⌋ + 1).

Dijkstra's algorithm over memory. On the input words of a graph G, at every word size w that holds 8 (n + m + 2) (W + 2), every memory operation of run is admissible, it costs at most memCost n m memory units, and the cell G.outAddr v then holds the encoded distance from the source to v, for every vertex v.

Dijkstra's algorithm over memory, unpacked. On the input words of a graph G, at every word size that holds 8 (n + m + 2) (W + 2), every memory operation of run is admissible, the cell G.outAddr v then holds the encoded distance from the source to v, and the run costs at most 52 (m + 1) (⌊log₂ n⌋ + 1) memory units.

theorem Cslib.Algorithms.Lean.Dijkstra.Memory.memCost_mono {n m n' m' : ℕ} (hn : n ≤ n') (hm : m ≤ m') :
memCost n m ≤ memCost n' m'

memCost grows with the graph.

The exported form: O((m + 1) log n) memory units. One memory program computes the distances from the source of every graph, at every word size that holds 8 (n + m + 2) (W + 2), admissibly and within f (n, m) memory units, for a monotone f = O((m + 1) log n) as n grows, uniformly in m. Since m + 1 ≤ n + m, this is also O((n + m) log n).