Dijkstra's algorithm as a client of abstract interfaces #
This file writes Dijkstra's single-source shortest-path algorithm Dijkstra
[CLRS09] as an adaptive client (Cslib.DataStructure.StateTransitionSpec.Client, the free monad
that OracleComp also uses) of three interfaces, combined by Cslib.DataStructure.product into
Cslib.Algorithms.Lean.Dijkstra.spec:
- the indexed min-priority queue
Cslib.DataStructure.PriorityQueue.indexedMinPQSpecon the vertices; - a map of tentative distances, the dictionary
Cslib.DataStructure.Dictionary.spec, in which a vertex is absent until it is reached; - the adjacency lists of the graph,
Cslib.DataStructure.AdjacencyList.spec, read one edge at a time.
Each answer the client sees is one vertex, distance or weight; the graph and the distances stay
behind the interfaces. The client inserts a vertex into the queue when it first reaches it and
decreases its key when its distance improves. It needs no visited set, because the distance of a
visited vertex never improves. The main loop runs at most n rounds, one per visited vertex.
Running the client against the transition functions of the specifications
(Cslib.DataStructure.StateTransitionSpec.runClient) is a pure Lean computation. Its correctness
(Cslib.Algorithms.Lean.Dijkstra.dijkstra_correct) rests on the machine-free invariant
Cslib.Algorithms.Dijkstra.Instance.Inv and on the relaxation of parallel edges
Cslib.Algorithms.Dijkstra.ListInstance.relaxEdges_eq_relax, both used unchanged. The same
induction counts the operations by kind (Cslib.Algorithms.Lean.Dijkstra.countP_kind_dijkstra_le):
at most min n (m + 1) insertions, min n (m + 2) extractions, m key decreases, 2 m + 1
distance-map queries and min (n + m) (2 m + 1) graph queries. Only reached vertices are
inserted, and each insertion after the source's is caused by an edge, so the counts need not grow
with n. CslibFrontier.Algorithms.Lean.Dijkstra.Cost turns the counts into costs.
No statement here concerns a machine. The client's continuations are Lean functions, and their computation is not charged.
Main definitions #
Cslib.Algorithms.Lean.Dijkstra.spec: the three interfaces of Dijkstra's algorithm.Cslib.Algorithms.Lean.Dijkstra.relaxEdge,Cslib.Algorithms.Lean.Dijkstra.scan,Cslib.Algorithms.Lean.Dijkstra.loop,Cslib.Algorithms.Lean.Dijkstra.dijkstra: the client, from relaxing one edge to the whole algorithm.Cslib.Algorithms.Lean.Dijkstra.init,Cslib.Algorithms.Lean.Dijkstra.HasDistances: the initial state and the final states of the interfaces.Cslib.Algorithms.Lean.Dijkstra.OpKind,Cslib.Algorithms.Lean.Dijkstra.kind: the kinds of operation, for counting.Cslib.Algorithms.Lean.Dijkstra.rateBound,Cslib.Algorithms.Lean.Dijkstra.maxCount: the bounds on the operations, weighted and by kind.
Main results #
Cslib.Algorithms.Lean.Dijkstra.dijkstra_correct: every operation of the client is admissible, and it ends with the distances in the distance map.Cslib.Algorithms.Lean.Dijkstra.sum_rate_dijkstra_le,Cslib.Algorithms.Lean.Dijkstra.countP_kind_dijkstra_le: the operation counts.
References #
The interfaces of Dijkstra's algorithm on Fin n: an indexed min-priority queue with keys
at most K, a map of tentative distances with values below V, and the adjacency lists of the
graph.
Instances For
One call to the priority queue.
The initial state for a graph G: an empty queue, an empty distance map and the adjacency
lists of G.
The kinds of operation of spec, for counting them.
- insert : OpKind
A queue insertion.
- extractMin : OpKind
A queue extraction.
- decreaseKey : OpKind
A queue key decrease.
- inspect : OpKind
A queue inspection,
isEmptyorgetKey. - distance : OpKind
A lookup or update of the distance map.
- graph : OpKind
A query to the adjacency lists.
Instances For
One call of each kind, followed by a continuation. Their answer types are the declared ones
(Option ℕ, Fin n × ℕ, …), so these equations apply where the generic
StateTransitionSpec.runClient_lift_bind' would have to unfold Sum.elim in the answer types of
product.
Tentative distances #
The main loop #
Correctness #
The final state of Dijkstra's algorithm on G: the queue is empty, the graph is unchanged,
and the distance map sends each vertex v to some d exactly when d is the distance from the
source to v. So it sends v to none exactly when v is unreachable.
Correctness of Dijkstra's algorithm against the specifications of its interfaces. Run
from an empty queue and an empty distance map on a graph whose largest weight W satisfies
n W ≤ K and n W < V, every operation the client issues is admissible, and it ends with the
distances from the source in the distance map.
Operation counts #
The total rate of the operations Dijkstra's algorithm issues on a graph with n vertices and
m edges, at most. It is the lesser of two bounds: at most n insertions and extractions, and
n + m graph queries; or at most m + 1 insertions, m + 2 extractions and 2 m + 1 graph
queries. Both allow m key decreases and 2 m + 1 distance-map queries.
Operations, weighted. Under any rate per kind of operation, the operations Dijkstra's
algorithm issues have total rate at most rateBound rate n m.
The number of operations of each kind that Dijkstra's algorithm issues on a graph with n
vertices and m edges, at most.
Operation counts. Dijkstra's algorithm issues at most min n (m + 1) insertions,
min n (m + 2) extractions and m key decreases, never inspects the queue, and issues at most
2 m + 1 distance-map queries and min (n + m) (2 m + 1) graph queries.