Dijkstra
Dijkstra
Algorithm
CLRS 24.3
Dijkstra’s algorithm.
Given for digraphs but easily modified to work on
undirected graphs.
1
Recall: Shortest Path Problem for Graphs
Let be a (di)graph.
BFS has running time .
2
Shortest Path Problem for Weighted Graphs
Let be a weighted digraph, with weight
function mapping edges to real-valued
weights. If , we write for .
The distance from to , denoted ! , is the
length of the minimum length path if there is a
path from to ; and is " otherwise.
%'& - %&
# $ / 6 #*$ G
7 7 length( <>=3?@? A ?CBDFE
+ 7 97 8 %& , G
# )$ distance from = to B is
2 2 254 : : : 1
% ( 13
& 2 . 0 5: ;
% &
# $ # $
3
Single-Source Shortest-Paths Problem
4
Single-Source Shortest-Paths Problem
(
Example: In the following digraph, ' )
is a short-
( )
est path. The subpath ' is also a shortest path.
%'& - %&
# $ / 6 #*$ G
7 7 length( <>=3?@? A ?CBDFE
+ 7 97 8 %& , G
# )$ distance from = to B is
2 2 254 : : : 1
% ( 13
& 2 . 0 5: ;
% &
# $ # $
5
Intuition behind Dijkstra’s Algorithm
6
The Rough Idea of Dijkstra’s Algorithm
*
Maintain an estimate of the length ! of
the shortest path for each vertex .
* *
Always ! and equals the length
of a known path
*
( " if we have no paths so far).
* *
Initially and all the other values are
set to " . The algorithm will then process the ver-
tices one by one in some order.
The processed vertex’s estimate will be validated
*
as being real shortest distance, i.e. !
Here “processing a vertex ” means finding new
* *
paths and updating for all if nec-
7
The Rough Idea of Dijkstra’s Algorithm
8
Answer to Question 1
Finding new paths. When processing a vertex ,
*
the algorithm will examine all vertices .
*
*
Remark: Whenever we set to a finite value, there
*
exists a path of that length. Therefore ! .
(Note: If ? E , then further relaxations cannot change
its value.)
9
Implementing the Idea of Relaxation
Consider an edge from a vertex to whose weight is ? E .
that we have already processed so that we know
Suppose
? E and also computed a current estimate for .
Then
There is a (shortest) path from to with length .
There is a path from to with length .
Combining this path from edge ? E , we obtain
to withthe
another path from to with length ? E .
If ? E
, then we replace the old path < ? ? ? D
with the new shorter path < ? ? ? D . Hence we update
? E
B (originally, B ).
s w d[v]
v
u
d[u]
10
The Algorithm for Relaxing an Edge
Relax(u,v)
* *
if ( )
* *
;
*
;
*
Remark: The predecessor pointer is for deter-
mining the shortest paths.
11
Idea of Dijkstra’s Algorithm: Repeated Relaxation
*
Initially , the empty set, and we set
*
and " for all others vertices . One by
one we select vertices from to add to .
12
The Selection in Dijkstra’s Algorithm
13
The Selection in Dijkstra’s Algorithm
Answer: We store the vertices of in a priority
*
queue, where the key value of each vertex is .
14
Review of Priority Queues
insert( ): Insert with the key value in .
decreaseKey( - ): Decrease ’s key value to
- .
15
Description of Dijkstra’s Algorithm
Dijkstra(G,w,s)
% Initialize
for (each )
;
A white;
;
B NIL;
(queue with all vertices);
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.
s a b c d
*
0 " " " "
*
3 nil nil nil nil nil
)
W W W W W
s a b c d
Priority Queue: *
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
s a b c d
* +
0 " "
*
3 nil s s nil nil
)
B W W W W
a b c d
Priority Queue: * +
" "
18
Dijkstra’s Algorithm
Example:
b 5 1 10 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
s a b c d
* + - .
0
*
nil s a a a
) B B W W W
b c d
Priority Queue: * - .
19
Dijkstra’s Algorithm
Example:
b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
(
Step 3: After Step 2, has the minimum key in the
* ( ' )
priority queue. As , work on ' , ) and
update information.
s a b c d
* + -
0
*
3 nil s a b a
)
B B B W W
c d
Priority Queue: *
20
Dijkstra’s Algorithm
Example:
b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
s a b c d
* + -
0
*
nil s a b a
)
B B B B W
d
Priority Queue: *
21
Dijkstra’s Algorithm
Example:
b 5 1 6 c
7
8
0 3 2
4 5
s
2
2 7
a 5 d
*
Step 5: After Step 4, has the minimum key in the pri-
* * )
ority queue. As , work on ) and update
information.
s a b c d
* + -
0
*
nil s a b a
)
B B B B B
Priority Queue: .
We are done.
22
Dijkstra’s Algorithm
Shortest Path Tree: ? E , where
B ? E
The array B is used to build the tree.
b 5 1 6 c
0 3
s
2
2 7
a 5 d
Example:
s a b
G
c d
0
B nil s a b a
23
Correctness of Dijkstra’s Algorithm
Lemma:When
a vertex is added to (i.e., dequeued from the
queue), ? E .
al-
Proof: Suppose to the contrary that at some point Dijkstra’s
gorithm first attempts to add a vertex to
for which
? E . By our observations about relaxation, ? E .
S u
y
x
24
Correctness of Dijkstra’s Algorithm – Continued
We now prove that ? E . We have done relaxation
when processing , so
?
E (1)
Since
is added to earlier, by hypothesis,
? E (2)
Since < ? ?
? D is subpath of a shortest path, by (2)
? E
? E ? E
? E (3)
By (1) and (3),
? E
Hence
? E
So (because we suppose
? E ).
? E ? E
Thus , which means would have been added to
before , in contradiction to our assumption that is the next
vertex to be added to .
25
Proof of the Correctness of Dijkstra’s Algorithm
*
By the lemma, ! when is added
)
into , that is when we set black.
26
Analysis of Dijkstra’s Algorithm:
The initialization uses only time.
*
The inner loop for (each ) is called once
for each edge in the graph. Each call of the inner loop
.
does work plus, possibly, one Decrease-Key
operation.
time.
27