Dijkstra's algorithm: cost in priority-queue operations #
This file measures the cost of Dijkstra's algorithm, as the client
Cslib.Algorithms.Lean.Dijkstra.dijkstra, in the operations of its interfaces. A priority-queue
operation costs what an implementation charges for it, possibly amortized, and a distance-map or
graph query costs one unit (Cslib.Algorithms.Lean.Dijkstra.charge). The operation counts of the
client do not depend on the implementation. So when insertions, extractions and key decreases are
charged at most cI, cE and cD, the algorithm costs at most n cI + n cE + m cD + n + 3 m + 1
and at most (m + 1) cI + (m + 2) cE + m cD + 4 m + 2 on a graph with n vertices and m edges
(Cslib.Algorithms.Lean.Dijkstra.costHoare_dijkstra).
The same budget holds on any lower interface that implements the three interfaces with bounded
per-method charges (Cslib.Algorithms.Lean.Dijkstra.costHoare_link_dijkstra, through
Cslib.DataStructure.Module.ChargeBound and
Cslib.DataStructure.StateTransitionSpec.CostHoare.link).
Two instantiations with abstract charges give the textbook bounds:
O((m + 1) log n), henceO((n + m) log n), when every queue operation costsO(log n), as in a binary heap [CLRS09] (Cslib.Algorithms.Lean.Dijkstra.exists_costHoare_logLinear);O(m + min(n, m + 1) log n), henceO(m + n log n), when insertions and key decreases cost amortizedO(1)and extractionsO(log n), as in a Fibonacci heap [FT87] (Cslib.Algorithms.Lean.Dijkstra.exists_costHoare_linearithmic).
These are the shapes of the word-RAM statements Cslib.Algorithms.WordRAM.Dijkstra.LogLinearSpec
and Cslib.Algorithms.WordRAM.Dijkstra.FibonacciSpec, at the level of operations.
Each states ∃ f with f monotone and f = O(g) (Mathlib's Asymptotics.IsBigO) along the
filter Filter.comap Prod.fst Filter.atTop on (n, m): for all large n, uniformly in m.
These are operation-level costs. Pure Lean computation in the client's continuations is not charged, and no statement concerns a machine.
Main definitions #
Cslib.Algorithms.Lean.Dijkstra.charge: queue operations at the charges of a priority-queue implementation, queries at one unit.Cslib.Algorithms.Lean.Dijkstra.budget: the operation-level budget.
Main results #
Cslib.Algorithms.Lean.Dijkstra.totalCost_dijkstra_le: the total cost under charges bounded by kind.Cslib.Algorithms.Lean.Dijkstra.costHoare_dijkstra: correctness and cost against any priority-queue charges.Cslib.Algorithms.Lean.Dijkstra.costHoare_link_dijkstra: the same through any implementation of the interfaces.Cslib.Algorithms.Lean.Dijkstra.budget_mono: the budget is monotone.Cslib.Algorithms.Lean.Dijkstra.exists_costHoare_logLinear,Cslib.Algorithms.Lean.Dijkstra.exists_costHoare_linearithmic: the asymptotic bounds.
References #
The charge of an operation in the priority-queue currency: a queue operation costs what
pqCharge charges for it in the current queue state, and every distance-map or graph query costs
one unit.
Total cost under any charges. If every operation of each kind costs at most rate of its
kind, Dijkstra's algorithm costs at most rateBound rate n m in total.
The operation-level budget of Dijkstra's algorithm when insertions cost cI, extractions cE
and key decreases cD each, and every other query one unit: rateBound at these rates.
Dijkstra's algorithm against any priority queue. Charge each queue operation what a
priority-queue implementation charges for it, at most cI per insertion, cE per extraction and
cD per key decrease, and one unit per distance-map or graph query. Then Dijkstra's algorithm is
correct, and its total charge is at most n cI + n cE + m cD + n + 3 m + 1, and at most
(m + 1) cI + (m + 2) cE + m cD + 4 m + 2.
Through any implementation of the interfaces. Let a module M implement the interfaces
of Dijkstra's algorithm over a lower interface E, with each method body costing at most
charge pqCharge of its operation in the lower charges lower. From any lower state that
represents the initial state, the linked client costs at most the same budget on E, and ends
in a state that represents a final state with the distances.
Asymptotic bounds #
Binary-heap-style charges: O((m + 1) log n). If every queue operation is charged at
most C (⌊log₂ n⌋ + 1), as for a binary heap, Dijkstra's algorithm is correct and costs
f (n, m) 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).
Fibonacci-heap-style charges: O(m + min(n, m + 1) log n). If insertions and key
decreases are charged at most C and extractions at most C (⌊log₂ n⌋ + 1), as amortized
charges of a Fibonacci heap, Dijkstra's algorithm is correct and costs f (n, m) for a monotone
f = O(m + min(n, m + 1) log n), as n grows, uniformly in m. This is also
O(m + n log n).