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 #
memMethods,opCharge: the methods of the three structures, and their charges.inputGraph: the adjacency arrays of the input convention.framed: the three structures as one framed refinement ofDijkstra.spec.run: read the input, allocate, and run Dijkstra's algorithm.
Main results #
dijkstra_eq: the client does not depend on the key and value bounds of its interfaces.frontier_init: the initial frontier of the input words.holds_inputGraph: the input holds the graph in the adjacency arrays.inputGraph_degree,inputGraph_edge: what degree and edge queries read from those arrays.inv_input: after allocation, the structures represent the initial state.eq_encodeDist: a distance-table cell that decodes to the distance holds its encoding.run_spec: admissibility, distances and memory cost ofrun.exists_run_logLinear: the same with anO((m + 1) log n)cost function.
References #
The client does not depend on its bounds #
The structures #
The methods of the heap H, the distance table T and the adjacency arrays A.
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 composite's methods are memMethods.
The methods do not depend on the bounds and the word size in their type.
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.
The input convention holds the graph in the adjacency arrays inputGraph.
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.
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.
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).