Lecture: Dijkstra’s Algorithm
Readings: CLRS3 Chapter 24: pages 643-650 and Section 24.3 (we only study Dijkstra’s algorithm)
Slides: http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/04GreedyAlgorithmsII.pdf
Instructions
- Slides 1-12 of
04GreedyAlgorithmsII.pdf;04DemoDijkstra.pdf
Dijkstra’s Algorithm
- single-pair shortest path problem
- given a digraph
G = (V, E), edge lengthse ≥ 0, sources ∈ V, and destinationt ∈ V, find a shortest directed path fromstot
- given a digraph
- single-source shortest paths problem
- given a digraph
G = (V, E), edge lengthse ≥ 0, sources ∈ V, find a shortest directed path fromsto every node
- given a digraph
- notice we assume the weights of the edges is
>=0as we’ll be studying Dijkstra’s algorithm - shortest path algorithms typically rely on optimal substructure to make a greedy choice
- if the shortest path from
stotiss->u->v->t, then the shortest path formstoviss->u->v
- if the shortest path from
- in general, we want to know not only the weight of the shortest path, but also the vertices on it, thus we use the following representation:
v.πis a predecessor ofv(which isNILat the start of the algorithm)v.dis an upper bound on the weight of a shortest path from sourcestov
- in general, single source algorithms start by initializing
v ∈ Vtov.π=NILandv.d=+Inf. Thens.dis set to0 - the process of relaxing an edge
(u,v)consists of whether we can improve the shortest path tovfound so far by going throughu. If so, we update bothv.πandv.d - Dijkstra’s algorithm
- maintain a set of explored nodes
Sfor which the algorithm has determinedd[u]= length of a shortests↝upath- initialize
S ← { s },d[s] ← 0 - repeatedly choose unexplored node
v ∉ Swhich minimizesd(i.e update all frontier neighborsπanddif necessary, and choose the one with the leastd)
- initialize
- invariant. For each node
u ∈ S:d[u]= length of a shortests↝upath - the original implementation of Dijkstra’s algorithm does not mention the use of a min priority queue and it re-computes the weight
dof eachv ∉ Sin the frontier - instead, use a min-oriented priority queue ordered by
d. Only needs to updatedandπleaving the recently addedu ∈ S
- maintain a set of explored nodes
- Dijkstra’s algorithm time complexity is dependent on the implementation of the min priority queue
- array implementation optimal for dense graphs (
Θ(n^2)edges) - binary heap much faster for sparse graphs (
Θ(n)edges) - 4-way heap worth the trouble in performance-critical solutions
- Fibonacci heap best in theory, but probably not worth implementing
- array implementation optimal for dense graphs (