Dijkstra's Algorithm
Dijkstra's Algorithm
006
Massachusetts Institute of Technology
Instructors: Erik Demaine, Jason Ku, and Justin Solomon Lecture 13: Dijkstra’s Algorithm
Review
• Single-Source Shortest Paths on weighted graphs
• Previously: O(|V | + |E|)-time algorithms for small positive weights or DAGs
• Last time: Bellman-Ford, O(|V ||E|)-time algorithm for general graphs with negative weights
• Today: faster for general graphs with non-negative edge weights, i.e., for e ∈ E, w(e) ≥ 0
Dijkstra’s Algorithm
• Named for famous Dutch computer scientist Edsger Dijkstra (actually Dÿkstra!)
• Idea! Relax edges from each vertex in increasing order of distance from source s
• Idea! Efficiently find next vertex in the order using a data structure
• Changeable Priority Queue Q on items with keys and unique IDs, supporting operations:
Q.build(X) initialize Q with items in iterator X
Q.delete min() remove an item with minimum key
Q.decrease key(id, k) find stored item with ID id and change key to k
• Assume vertex IDs are integers from 0 to |V | − 1 so can use a direct access array for D
• Build changeable priority queue Q with an item (v, d(s, v)) for each vertex v ∈ V
• While Q not empty, delete an item (u, d(s, u)) from Q that has minimum d(s, u)
Example
Delete d(s, v)
v from Q s a b c d 2
G a b
s 0 ∞ ∞ ∞ ∞ 10
c 10 ∞ 3 ∞
d 7 11 5 s 4 1 5 7
8
a 7 10 3
b 9 c d
2
δ(s, v) 0 7 9 3 5
Correctness
• Claim: At end of Dijkstra’s algorithm, d(s, v) = δ(s, v) for all v ∈ V
• Proof:
– If relaxation sets d(s, v) to δ(s, v), then d(s, v) = δ(s, v) at the end of the algorithm
∗ Relaxation can only decrease estimates d(s, v)
∗ Relaxation is safe, i.e., maintains that each d(s, v) is weight of a path to v (or ∞)
– Suffices to show d(s, v) = δ(s, v) when vertex v is removed from Q
∗ Proof by induction on first k vertices removed from Q
∗ Base Case (k = 1): s is first vertex removed from Q, and d(s, s) = 0 = δ(s, s)
∗ Inductive Step: Assume true for k < k 0 , consider k 0 th vertex v 0 removed from Q
∗ Consider some shortest path π from s to v 0 , with w(π) = δ(s, v 0 )
∗ Let (x, y) be the first edge in π where y is not among first k 0 − 1 (perhaps y = v 0 )
∗ When x was removed from Q, d(s, x) = δ(s, x) by induction, so:
Running Time
• Count operations on changeable priority queue Q, assuming it contains n items:
Operation Time Occurrences in Dijkstra
Q.build(X) (n = |X|) Bn 1
Q.delete min() Mn |V |
Q.decrease key(id, k) Dn |E|
• Total running time is O(B|V | + |V | · M|V | + |E| · D|V | )
• Assume pruned graph to search only vertices reachable from the source, so |V | = O(|E|)
• If graph is dense, i.e., |E| = Θ(|V |2 ), using an Array for Q0 yields O(|V |2 ) time
• If graph is sparse, i.e., |E| = Θ(|V |), using a Binary Heap for Q0 yields O(|V | log |V |) time
• A Fibonacci Heap is theoretically good in all cases, but is not used much in practice
• We won’t discuss Fibonacci Heaps in 6.006 (see 6.854 or CLRS chapter 19 for details)
• You should assume Dijkstra runs in O(|E|+|V | log |V |) time when using in theory problems
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms