Documentation

CslibFrontier.Algorithms.Lean.Dijkstra.Cost

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:

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 #

Main results #

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.

theorem Cslib.Algorithms.Lean.Dijkstra.totalCost_dijkstra_le {K V : ℕ} (G : Dijkstra.ListInstance) (hK : G.n * G.maxWeight ≤ K) (hV : G.n * G.maxWeight < V) (cost : (spec G.n K V).State → (spec G.n K V).Op → ℕ) (rate : OpKind → ℕ) (hcost : ∀ (s : (spec G.n K V).State) (o : (spec G.n K V).Op), kind o ≠ OpKind.inspect → cost s o ≤ rate (kind o)) :
(spec G.n K V).totalCost cost (init G) ((spec G.n K V).clientHistory (init G) (dijkstra G.source)) ≤ rateBound rate G.n G.m

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.

Asymptotic bounds #

theorem Cslib.Algorithms.Lean.Dijkstra.budget_mono {cI cE cD cI' cE' cD' n m n' m' : ℕ} (hI : cI ≤ cI') (hE : cE ≤ cE') (hD : cD ≤ cD') (hn : n ≤ n') (hm : m ≤ m') :
budget cI cE cD n m ≤ budget cI' cE' cD' n' m'

budget is monotone in the charges and in the size of the graph.

theorem Cslib.Algorithms.Lean.Dijkstra.exists_costHoare_logLinear (C : ℕ) :
∃ (f : ℕ × ℕ → ℕ), Monotone f ∧ ((fun (p : ℕ × ℕ) => ↑(f p)) =O[Filter.comap Prod.fst Filter.atTop] fun (x : ℕ × ℕ) => match x with | (n, m) => (↑m + 1) * ↑(Nat.log 2 n)) ∧ ∀ (G : Dijkstra.ListInstance) (K V : ℕ), G.n * G.maxWeight ≤ K → G.n * G.maxWeight < V → ∀ (pqCharge : DataStructure.PriorityQueue.IndexedMinPQ.State G.n → DataStructure.PriorityQueue.IndexedMinPQ.Op G.n → ℕ), (∀ (q : DataStructure.PriorityQueue.IndexedMinPQ.State G.n) (o : DataStructure.PriorityQueue.IndexedMinPQ.Op G.n), pqCharge q o ≤ C * (Nat.log 2 G.n + 1)) → DataStructure.StateTransitionSpec.CostHoare (charge pqCharge) (fun (x : (spec G.n K V).State) => x = init G) (dijkstra G.source) (fun (x : Unit) => HasDistances G) fun (x : (spec G.n K V).State) => f (G.n, G.m)

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).

theorem Cslib.Algorithms.Lean.Dijkstra.exists_costHoare_linearithmic (C : ℕ) :
∃ (f : ℕ × ℕ → ℕ), Monotone f ∧ ((fun (p : ℕ × ℕ) => ↑(f p)) =O[Filter.comap Prod.fst Filter.atTop] fun (x : ℕ × ℕ) => match x with | (n, m) => ↑m + min (↑n) (↑m + 1) * ↑(Nat.log 2 n)) ∧ ∀ (G : Dijkstra.ListInstance) (K V : ℕ), G.n * G.maxWeight ≤ K → G.n * G.maxWeight < V → ∀ (pqCharge : DataStructure.PriorityQueue.IndexedMinPQ.State G.n → DataStructure.PriorityQueue.IndexedMinPQ.Op G.n → ℕ), (∀ (q : DataStructure.PriorityQueue.IndexedMinPQ.State G.n) (v : Fin G.n) (k : ℕ), pqCharge q (DataStructure.PriorityQueue.IndexedMinPQ.Op.insert v k) ≤ C) → (∀ (q : DataStructure.PriorityQueue.IndexedMinPQ.State G.n), pqCharge q DataStructure.PriorityQueue.IndexedMinPQ.Op.extractMin ≤ C * (Nat.log 2 G.n + 1)) → (∀ (q : DataStructure.PriorityQueue.IndexedMinPQ.State G.n) (v : Fin G.n) (k : ℕ), pqCharge q (DataStructure.PriorityQueue.IndexedMinPQ.Op.decreaseKey v k) ≤ C) → DataStructure.StateTransitionSpec.CostHoare (charge pqCharge) (fun (x : (spec G.n K V).State) => x = init G) (dijkstra G.source) (fun (x : Unit) => HasDistances G) fun (x : (spec G.n K V).State) => f (G.n, G.m)

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).