Professional Documents
Culture Documents
Shortest Path PDF
Shortest Path PDF
Algorithm
CLRS 24.3
Outline of this Lecture
Dijkstras algorithm.
Given for digraphs but easily modified to work on
undirected graphs.
be a (di)graph.
Breadth-first-search is an algorithm for finding shortest (link-distance) paths from a single source vertex to all other vertices.
Let
be a weighted digraph, with weight
mapping edges to real-valued
function
weights. If
, we write
for .
length
%&
6 #*$
7 7
7 97 8 %&
+
,
# )$
2 254 : : : 1
2
0 5: ;
% ( 13
& 2 .
% &
# $
# $
G
length( <>=3?@?
A
?CBDFE
G
distance from = to B is
(
)
is a shortExample: In the following digraph, '
( )
est path. The subpath '
is also a shortest path.
%'&
# $
%&
6 #*$
7 7
7 97 8 %&
+
,
# )$
2 254 : : : 1
2
0 5: ;
% ( 13
& 2 .
% &
# $
# $
G
length( <>=3?@?
A
?CBDFE
G
distance from = to B is
Observation Extending this idea we observe the existence of a shortest path tree in which distance from
source to vertex is length of shortest path from source
to vertex in original tree.
Report the vertices in increasing order of their distance from the source vertex.
Maintain an estimate
of the length !
the shortest path for each vertex .
*
of
*
!
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 vertices one by one in some order.
The processed vertexs estimate will be validated
*
as being real shortest distance, i.e.
!
Answer to Question 1
its value.)
? E .
Consider an edge from a vertex to whose weight is
Suppose
that
we have already processed so that we know
? E and also computed a current estimate for
.
Then
to
to
with length
with length
If
, then
the old path < ?
? ? D
? E
we replace
with the new shorter path < ?
? ? D . Hence we update
? E
(originally, B
).
d[v]
v
u
d[u]
10
*
)
* *
;
*
*
*
is for deter-
11
*
Initially
, the empty set, and we set
*
and
for all others vertices . One by
"
one we select vertices from
to add to .
12
13
time.]
14
insert(
): Insert
decreaseKey(
- .
. See CLRS!
15
for (each
A
B
)
;
white;
;
NIL;
(queue with all vertices);
while (Non-Empty( ))
Extract-Min
E ;
for (each
)
? E
if (
? E ;
E ;
Decrease-Key ? ?
B
;
A
% If estimate improves
relax
black;
16
Dijkstras Algorithm
Example:
1
b inf
inf c
7
0
s
2
a
inf
inf
Step 0: Initialization.
s
0
nil
W
*
*
3
)
Priority Queue: *
s
0
"
"
"
"
nil
W
nil
W
nil
W
nil
W
"
a
"
b
"
d
"
17
Dijkstras Algorithm
Example:
1
b 7
inf c
7
0
s
2
a
inf
5
*
' (
Step 1: As
update information.
s a
+
0
nil s
B W
*
*
3
)
Priority Queue: *
a
+
, work on
s
W
b
'
and
"
"
nil
W
nil
W
c
"
and
d
"
18
Dijkstras Algorithm
Example:
1
b 5
10 c
7
3
s
2
a
s a
+
0
nil s
B B
*
*
)
Priority Queue: *
b
-
b
.
a
W
c
d
a
W
a
W
d
19
Dijkstras Algorithm
Example:
1
b 5
6 c
7
3
s
2
a
s a
+
0
nil s
B B
*
*
3
)
Priority Queue: *
d
a
B
b
W
a
W
d
20
Dijkstras Algorithm
Example:
1
b 5
6 c
7
0
s
2
a
*
*
)
Priority Queue: *
s a
+
0
nil s
B B
d
a
B
b
B
a
W
d
21
Dijkstras Algorithm
Example:
1
b 5
6 c
7
3
s
2
a
s a
+
0
nil s
B B
*
*
)
Priority Queue:
d
a
B
b
B
a
B
We are done.
22
Dijkstras Algorithm
Shortest Path Tree:
The array B
B
? E , where
? E
b 5
6 c
s
2
a
Example:
B
s
0
nil
23
is added to
Consider
,
Let ? E
(it
S
s
y
x
24
Since
is added to
Since < ?
?
E
?
? E
(1)
earlier, by hypothesis,
? E
(2)
Hence
So
(because we suppose
?
E
(3)
? E ).
to
Thus
, which means would have been added to
before , in contradiction to our assumption that is the next
vertex to be added to .
25
By the lemma,
when is added
!
)
into , that is when we set
black.
26
time.
quire
time.
27