Documentation

CslibFrontier.Algorithms.Lean.Dijkstra.Client

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:

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 #

Main results #

References #

@[reducible, inline]

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
    @[reducible, inline]

    One call to the priority queue.

    @[reducible, inline]
    abbrev Cslib.Algorithms.Lean.Dijkstra.getDist {n K V : ℕ} (v : Fin n) :
    (spec n K V).Client (Option ℕ)

    Look up the tentative distance of v: none while v is unreached.

    @[reducible, inline]
    abbrev Cslib.Algorithms.Lean.Dijkstra.setDist {n K V : ℕ} (v : Fin n) (d : ℕ) :
    (spec n K V).Client Unit

    Set the tentative distance of v to d.

    @[reducible, inline]
    abbrev Cslib.Algorithms.Lean.Dijkstra.degree {n K V : ℕ} (u : Fin n) :
    (spec n K V).Client ℕ

    The number of edges out of u.

    @[reducible, inline]
    abbrev Cslib.Algorithms.Lean.Dijkstra.edge {n K V : ℕ} (u : Fin n) (i : ℕ) :
    (spec n K V).Client (Fin n × ℕ)

    The i-th edge out of u: its target and its weight.

    def Cslib.Algorithms.Lean.Dijkstra.relaxEdge {n K V : ℕ} (u : Fin n) (du i : ℕ) :
    (spec n K V).Client Unit

    Relax the i-th edge out of u, a vertex at distance du. An unreached target is reached and inserted into the queue; a reached target whose distance improves has its key decreased.

    def Cslib.Algorithms.Lean.Dijkstra.scan {n K V : ℕ} (u : Fin n) (du : ℕ) :
    (spec n K V).Client Unit

    Relax every edge out of u, a vertex at distance du.

    The main loop, for at most k rounds: extract a vertex of least key from the queue and relax the edges out of it, until the queue is empty.

    Dijkstra's algorithm from the source src, as a client of spec: reach the source at distance 0, then run the main loop for n rounds. The distances are left in the distance map.

    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, isEmpty or getKey.

    • distance : OpKind

      A lookup or update of the distance map.

    • graph : OpKind

      A query to the adjacency lists.

    Instances For

      The kind of an operation of spec.

      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.

      theorem Cslib.Algorithms.Lean.Dijkstra.sum_rate_dijkstra_le {K V : ℕ} (G : Dijkstra.ListInstance) (hK : G.n * G.maxWeight ≤ K) (hV : G.n * G.maxWeight < V) (rate : OpKind → ℕ) :
      (List.map (fun (o : (spec G.n K V).Op) => rate (kind o)) ((spec G.n K V).clientHistory (init G) (dijkstra G.source))).sum ≤ rateBound rate G.n G.m

      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.

      theorem Cslib.Algorithms.Lean.Dijkstra.countP_kind_dijkstra_le {K V : ℕ} (G : Dijkstra.ListInstance) (hK : G.n * G.maxWeight ≤ K) (hV : G.n * G.maxWeight < V) (k : OpKind) :
      List.countP (fun (x : (spec G.n K V).Op) => decide (kind x = k)) ((spec G.n K V).clientHistory (init G) (dijkstra G.source)) ≤ maxCount G.n G.m k

      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.