You are on page 1of 28

Lecture 9: Dijkstras Shortest Path Algorithm

CLRS 24.3 Outline of this Lecture

Recalling the BFS solution of the shortest path problem for unweighted (di)graphs.

The shortest path problem for weighted digraphs.

Dijkstras algorithm. Given for digraphs but easily modied to work on undirected graphs.

Recall: Shortest Path Problem for Graphs Let


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.
   

BFS has running time 

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

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

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.

Question: How do you design an efcient algorithm for this problem?

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.

Intuition behind Dijkstras Algorithm

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.

The Rough Idea of Dijkstras Algorithm

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.

When all vertices have been processed,  B 3 for all .

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



c i , then further relaxations cannot change

its value.)

Implementing the Idea of Relaxation


Consider an edge from a vertex to whose weight is c i . Suppose that )   we have already processed so that we know c i and also computed a current estimate for . Then

There is a (shortest) path from There is a path from

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

The Algorithm for Relaxing an Edge

Relax(u,v) ( if
B

 B ) B B    ; B
;

Remark: The predecessor pointer  mining the shortest paths.

is for deter-

11

Idea of Dijkstras Algorithm: Repeated Relaxation

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 .

Question: How do we implement this selection of vertices efciently?

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

with the key value in .

u = extractMin(): Extract the item with the minimum key value in .


decreaseKey( - .

- ): Decrease s key value to

Remark: Priority Queues can be implementad such   that each operation takes time  . See CLRS!
15

Description of Dijkstras Algorithm


Dijkstra(G,w,s) for (each ) % Initialize




) ;

white;

f)

 );

NIL; (queue with all vertices);




while (Non-Empty( )) Extract-Min  i ; for (each   ) c i if (

% Process all vertices % Find new vertex

% If estimate improves relax

;  c i i ; Decrease-Key c c  ) f ;


black;

16

Dijkstras Algorithm Example:


b inf 7 0 s 2 a inf 5 inf d 3 2 8 4 5 1 inf c

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

Dijkstras Algorithm Example:


b 7 7 0 s 2 a 2
B

inf c

8 4 inf 5

Step 1: As update information.

9 @ @ 9 , work on and and

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

Dijkstras Algorithm Example:


b 5 7 0 s 2 a 2 5 7 d 3 2 8 4 5 1 10 c

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

Dijkstras Algorithm Example:


b 5 7 0 s 2 a 2 5
@

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

Dijkstras Algorithm Example:


b 5 7 0 s 2 a 2 5 7 d 3 2 8 4 5 1 6 c

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

Dijkstras Algorithm Example:


b 5 7 0 s 2 a 2
B

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

Priority Queue: We are done.

22

Dijkstras Algorithm
Shortest Path Tree: ) 


) f

c  i , where

The array  f

c i

is used to build the tree.

b 5

6 c

0 s 2

a
Example:


f

s 0 nil

23

Correctness of Dijkstras Algorithm


Lemma:When is added to ) a  vertex  c i . queue),

(i.e., dequeued from the

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

Consider , c i Let (it

24

Correctness of Dijkstras Algorithm Continued

We now prove that when processing , so


Since



c i . We have done relaxation

is added to

c i

(1)

Since ` c c

  

earlier, by hypothesis, )   c i

(2)

h is subpath of a shortest path, by (2)   ) c i c i c i

(3)

By (1) and (3), Hence


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

Proof of the Correctness of Dijkstras Algorithm


B

By the lemma, when is added 3 A into , that is when we set black.

At the end of the algorithm, all vertices are in , then all distance estimates are correct.

26

Analysis of Dijkstras Algorithm: The initialization uses only 


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

You might also like