Dijkstra`s Shortest Path Algorithm

CSE 241 Algorithms and Data Structures
11/11/2015
Dijkstra’s Shortest Path Algorithm
Given a weighted directed graph G = (V, E), such that each edge (u, v) has a non-negative weight w(u, v),
and a starting vertex s, Dijkstra’s algorithm finds a path of minimum length (shortest sum of edge weights)
from s to every vertex of G. Liken BFS, its stores for each vertex v the shortest-path distance from s to v
and a parent pointer that can be used in reconstructing the path from s to v.
Dijkstra’s algorithm uses a priority queue Q to keep track of the vertex closest to s that has not yet
been processed. If Q is implemented with a standard binary heap, the algorithm requires time O(m log n);
if we instead use a Fibonacci heap (which has amortized constant-time insertion and decreaseKey), the time
drops to O(n log n + m).
1
Dijkstra(G, s)
for u ∈ V do
u.distance ← ∞
u.parent ← null
Q.insert(u, ∞)
. initialize
s.distance ← 0
Q.decreaseKey(s, 0)
while Q is not empty do
u ← Q.extractMin()
if u.distance = ∞
stop
. cannot reach any more vertices from s
for v ∈ Adj[u] do
if Q.decreaseKey(v, u.distance + w(u, v))
v.distance ← u.distance + w(u, v)
v.parent ← u
2