Professional Documents
Culture Documents
Recalling the BFS solution of the shortest path problem for unweighted (di)graphs.
Dijkstras algorithm. Given for digraphs but easily modied to work on undirected graphs.
be a (di)graph.
The shortest path between two vertices is a path with the shortest length (least number of edges). Call this the link-distance.
Breadth-rst-search is an algorithm for nding shortest (link-distance) paths from a single source vertex to all other vertices.
BFS processes vertices in increasing order of their distance from the root vertex.
Shortest Path Problem for Weighted Graphs Let be a weighted digraph, with weight mapping edges to real-valued function weights. If , we write for .
The length of a path is the sum of the weights of its constituent edges:
$
length
()
(10 ( 2
The distance from to , denoted 3 , is the length of the minimum length path if there is a path from to ; and is 4 otherwise.
G E 7' B 68 T 52 ) q length( `baQc1dcecgfhpi q distance from a to f is
72 8 596 2
Single-Source Shortest-Paths Problem The Problem: Given a digraph with positive edge weights and a distinguished source vertex, , determine the distance and a shortest path from the source vertex to every vertex in the digraph.
Single-Source Shortest-Paths Problem Important Observation: Any subpath of a shortest path must also be a shortest path. Why?
A is a shortExample: In the following digraph, 9 @ A & est path. The subpath 9 is also a shortest path. U U UW U V 7' C D A 68 52 P PSR X X X I P X Y' H S 72 8 P F 7 8 @ IQ 52 6 5 6 2 72 8 596 2 G E 7' B 68 T 52 ) q length( `baQc1dcecgfhpi q distance from a to f 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.
Construct the shortest path tree edge by edge; at each step adding one new edge, corresponding to construction of shortest path to the current new vertex.
Maintain an estimate of the length 3 the shortest path for each vertex .
of
3 Always and equals the length of a known path B if we have no paths so far). ( 4
Initially and all the other values are set to 4 . The algorithm will then process the vertices one by one in some order. The processed vertexs estimate will be validated 2 B as being real shortest distance, i.e. 3
Here processing a vertex means nding new B B for all if necpaths and updating essary. The process by which an estimate is updated is called relaxation.
The Rough Idea of Dijkstras Algorithm Question 1: How does the algorithm nd new paths and do the relaxation? Question 2: In which order does the algorithm process the vertices one by one?
Answer to Question 1
Finding new paths. When processing a vertex , B . the algorithm will examine all vertices B For each vertex , a new path from to is found (path from to + new edge).
Relaxation. If the new path from to is shorter B B than , then update to the length of this new path.
Remark: Whenever we set to a nite value, there B exists a path of that length. Therefore . 3
(Note: If
its value.)
to with length
to
with length
Combining this path to with the edge c i , we obtain from c i . another path from to with length If , then the old path ` c c c h c i we replace with the new shorter path ` c c c h . Hence we update
)
c i
(originally, f
) )
).
w u d[u]
d[v] v
10
Relax(u,v) ( if
B
B ) B B ; B
;
is for deter-
11
Dijkstras algorithm operates by maintaining a sub set of vertices, , for which we know the true B 3 distance, that is .
Initially , the empty set, and we set B and for all others vertices . One by 4 to add to . one we select vertices from
The set can be implemented using an array of vertex colors. Initially all vertices are white, and we set A black to indicate that .
12
The Selection in Dijkstras Algorithm Recall Question 2: What is the best order in which to process vertices, so that the estimates are guaranteed to converge to the true distances. That is, how does the algorithm select which vertex among the vertices of to process next? Answer: We use a greedy algorithm. For each ver , we have computed a distance estex in B timate . The next vertex processed is always a B for which vertex is minimum, that is, we take the unprocessed vertex that is closest (by our estimate) to .
13
The Selection in Dijkstras Algorithm Question: How do we perform this selection efciently? Answer: We store the vertices of in a priority B queue, where the key value of each vertex is . [Note: if we implement the priority queue using a heap, we can perform the operations Insert(), Extract Min(), and Decrease Key(), each in time.]
14
Review of Priority Queues A Priority Queue is a data structure (can be implemented as a heap) which supports the following operations:
insert(
): Insert
decreaseKey( - .
Remark: Priority Queues can be implementad such that each operation takes time . See CLRS!
15
) ;
white;
f)
);
; c i i ; Decrease-Key c c ) f ;
black;
16
Step 0: Initialization.
B B Q A
s 0 nil W
a
4 4
b
4
c
4
d nil W c
4 4
17
nil W s 0
4
nil W a
4
nil W b
Priority Queue: B
inf c
8 4 inf 5
B B Q A
s a C 0 nil s B W
b
4
c
4
d nil W
s W b
4
nil W d
4
Priority Queue: B
a
C
18
Step 2: After Step 1, 9 has the minimum key in the @ A B B 9 @ A B priority queue. As , work on , , and update information.
B B A
s a C 0 nil s B B
b
E F
d a W
a W
F
a W d
Priority Queue: B
b
E
19
6 c
8 4 7 5
Step 3: After Step 2, has the minimum key in the 9 A B @ priority queue. As , work on 9 , A and update information.
B B Q A
s a C 0 nil s B B
b
E
c b W
d a W
a B
Priority Queue: B
d
20
Step 4: After Step 3, A has the minimum key in the pri B B A B ority queue. As , work on and update information.
B B A
s a C 0 nil s B B d
b
E
c b B
d a W
a B
Priority Queue: B
21
6 c
8 4 7 5
Step 5: After Step 4, has the minimum key in the pri A B B , work on A and update ority queue. As information.
B B A
s a C 0 nil s B B
b
E
c b B
d a B
a B
22
Dijkstras Algorithm
Shortest Path Tree: )
) f
c i , where
c i
b 5
6 c
0 s 2
a
Example:
f
s 0 nil
23
Proof: Suppose to the contrary that at some point Dijkstras al) to for which rst attempts to add a vertex gorithm c i . By our observations about relaxation, c i . Consider the situation just prior to the insertion of . the true shortest path from to . Because and at some point this path must rst take a jump out of . be the edge taken where and ) ) by the path, may be that and/or ).
S s y x u
24
is added to
c i
(1)
Since ` c c
earlier, by hypothesis, ) c i
(2)
(3)
c
i i
So
(because we suppose
c i ).
Thus would have been added to before , in contradiction to our assumption that is the next vertex to be added to .
25
Now observe that since appears midway on the path from , and all subsequent edges are positive, we have c i c i , and thus ) c i c i
to
At the end of the algorithm, all vertices are in , then all distance estimates are correct.
26
time.
Each vertex is processed exactly once so Non-Empty() and Extract-Min() are called exactly once, e.g., times in total. The inner loop for (each ) is called once for each edge in the graph. Each call of the inner loop F does work plus, possibly, one Decrease-Key operation.
B
Recalling that all of the priority queue operations re quire time we have that the algorithm uses F
time.
27
Prove: Dijkstras algorithm processes vertices in nondecreasing order of their actual distance from the source vertex.
28