Dijkstra's Shortest Path Algorithm: Outline of This Lecture
Dijkstra's Shortest Path Algorithm: Outline of This Lecture
• Dijkstra’s algorithm.
Given for digraphs but easily modified to work on
undirected graphs.
1
Recall: Shortest Path Problem for Graphs
2
Shortest Path Problem for Weighted Graphs
a 5 -
d
Q 8
length(ha, b, c, ei) = 6
Q
Qs
c distance from a to e is 6
2 Q
*
H
4
HH
j
?
b 1 3
? H
e
3
Single-Source Shortest-Paths Problem
4
Single-Source Shortest-Paths Problem
a 5 -
d
Q 8
length(ha, b, c, ei) = 6
Q
Qs
c distance from a to e is 6
2 Q
*
H
4
j
HH
? ?
b 1 3
H
e
5
Intuition behind Dijkstra’s Algorithm
6
The Rough Idea of Dijkstra’s Algorithm
7
The Rough Idea of Dijkstra’s Algorithm
8
Answer to Question 1
9
Implementing the Idea of Relaxation
Consider an edge from a vertex u to v whose weight is w(u, v).
Suppose that we have already processed u so that we know
d[u] = δ(s, u) and also computed a current estimate for d[v].
Then
Combining this path from s to u with the edge (u, v), we obtain
another path from s to v with length d[u] + w(u, v).
s w d[v]
v
u
d[u]
10
The Algorithm for Relaxing an Edge
Relax(u,v)
{
if (d[u] + w(u, v) < d[v])
{ d[v] = d[u] + w(u, v);
pred[v] = u;
}
}
11
Idea of Dijkstra’s Algorithm: Repeated Relaxation
12
The Selection in Dijkstra’s Algorithm
13
The Selection in Dijkstra’s Algorithm
14
Review of Priority Queues
16
Dijkstra’s Algorithm
Example:
b inf 1 inf c
7
8
0 3 2
4 5
s
2
inf inf
a 5 d
Step 0: Initialization.
v s a b c d
d[v] 0 ∞ ∞ ∞ ∞
pred[v] nil nil nil nil nil
color[v] W W W W W
v s a b c d
Priority Queue:
d[v] 0 ∞ ∞ ∞ ∞
17
Dijkstra’s Algorithm
Example:
b 7 1 inf c
7
8
0 3 2
4 5
s
2
2 inf
a 5 d
v s a b c d
d[v] 0 2 7 ∞ ∞
pred[v] nil s s nil nil
color[v] B W W W W
v a b c d
Priority Queue:
d[v] 2 7 ∞ ∞
18
Dijkstra’s Algorithm
Example:
b 5 1 10 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
v s a b c d
d[v] 0 2 5 10 7
pred[v] nil s a a a
color[v] B B W W W
v b c d
Priority Queue:
d[v] 5 10 7
19
Dijkstra’s Algorithm
Example:
b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a
color[v] B B B W W
v c d
Priority Queue:
d[v] 6 7
20
Dijkstra’s Algorithm
Example:
b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a
color[v] B B B B W
v d
Priority Queue:
d[v] 7
21
Dijkstra’s Algorithm
Example:
b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a
color[v] B B B B B
Priority Queue: Q = ∅.
We are done.
22
Dijkstra’s Algorithm
Shortest Path Tree: T = (V, A), where
A = {(pred[v], v)|v ∈ V \ {s}}.
The array pred[v] is used to build the tree.
b 5 1 6 c
0 3
s
2
2 7
a 5 d
Example:
v s a b c d
d[v] 0 2 5 6 7
pred[v] nil s a b a
23
Correctness of Dijkstra’s Algorithm
Lemma: When a vertex u is added to S (i.e., dequeued from the
queue), d[u] = δ(s, u).
y
x
24
Correctness of Dijkstra’s Algorithm – Continued
We now prove that d[y] = δ(s, y). We have done relaxation
when processing x, so
d[y] ≤ d[x] + w(x, y). (1)
Since x is added to S earlier, by hypothesis,
d[x] = δ(s, x). (2)
Since hs, . . . , x, yi is subpath of a shortest path, by (2)
δ(s, y) = δ(s, x) + w(x, y) = d[x] + w(x, y). (3)
By (1) and (3),
d[y] ≤ δ(s, y).
Hence
d[y] = δ(s, y).
So y 6= u (because we suppose d[u] > δ(s, u)).
25
Proof of the Correctness of Dijkstra’s Algorithm
26
Analysis of Dijkstra’s Algorithm:
28
Further Improvements to Dijkstra’s Algorithm:
29
Prove: Dijkstra’s algorithm processes vertices in non-
decreasing order of their actual distance from the source
vertex.
30