You are on page 1of 59

Introduction to Algorithms

6.046J/18.401J LECTURE 17
Shortest Paths I Properties of shortest paths Dijkstras algorithm Correctness Analysis Breadth-first search Prof. Erik Demaine
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.1

Paths in graphs
Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v1 v2 L vk is defined to be

w( p ) = w(vi , vi +1 ) .
i =1

k 1

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.2

Paths in graphs
Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v1 v2 L vk is defined to be w( p ) = w(vi , vi +1 ) .
i =1 k 1

Example:

v v11

v v22

v v33

v v44

v v55 w(p) = 2
L17.3

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Shortest paths
A shortest path from u to v is a path of minimum weight from u to v. The shortestpath weight from u to v is defined as (u, v) = min{w(p) : p is a path from u to v}.
Note: (u, v) = if no path from u to v exists.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.4

Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.5

Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.

Proof. Cut and paste:

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.6

Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.

Proof. Cut and paste:

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.7

Triangle inequality
Theorem. For all u, v, x V, we have (u, v) (u, x) + (x, v).

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.8

Triangle inequality
Theorem. For all u, v, x V, we have (u, v) (u, x) + (x, v).

Proof. u u
(u, x) (u, v)

v v
(x, v)

x x
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.9

Well-definedness of shortest paths


If a graph G contains a negative-weight cycle, then some shortest paths may not exist.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.10

Well-definedness of shortest paths


If a graph G contains a negative-weight cycle, then some shortest paths may not exist.
Example:

<0

u u
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

v v
L17.11

Single-source shortest paths


Problem. From a given source vertex s V, find the shortest-path weights (s, v) for all v V. If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Greedy. 1. Maintain a set S of vertices whose shortestpath distances from s are known. 2. At each step add to S the vertex v V S whose distance estimate from s is minimal. 3. Update the distance estimates of vertices adjacent to v.
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.12

Dijkstras algorithm
d [ s] 0 for each v V {s} do d[v] S QV Q is a priority queue maintaining V S

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.13

Dijkstras algorithm
d [ s] 0 for each v V {s} do d[v] S QV Q is a priority queue maintaining V S while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v)

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.14

Dijkstras algorithm
d [ s] 0 for each v V {s} do d[v] S QV Q is a priority queue maintaining V S while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] relaxation do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) step

Implicit DECREASE-KEY
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.15

Example of Dijkstras algorithm


Graph with nonnegative edge weights:
10

B B
1 4

2 8

D D
7 9

A A
3

C C

E E

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.16

Example of Dijkstras algorithm


Initialize:
10

B B

2 8

D D
7 9

0 A A Q: A B C D E
0

1 4 3

C C

E E

S: {}
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.17

Example of Dijkstras algorithm


A EXTRACT-MIN(Q):
10

B B

2 8

D D
7 9

0 A A Q: A B C D E
0

1 4 3

C C

E E

S: { A }
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.18

Example of Dijkstras algorithm


Relax all edges leaving A:
10

10 B B

2 8

D D
7 9

0 A A Q: A B C D E
0 10 3

1 4 3

C C 3

E E

S: { A }
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.19

Example of Dijkstras algorithm


C EXTRACT-MIN(Q):
10

10 B B

2 8

D D
7 9

0 A A Q: A B C D E
0 10 3

1 4 3

C C 3

E E

S: { A, C }
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.20

Example of Dijkstras algorithm


Relax all edges leaving C:
10

7 B B

2 8

11 D D
7 9

0 A A Q: A B C D E
0 10 7 3 11 5

1 4 3

C C 3

E E 5

S: { A, C }
L17.21

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example of Dijkstras algorithm


E EXTRACT-MIN(Q):
10

7 B B

2 8

11 D D
7 9

0 A A Q: A B C D E
0 10 7 3 11 5

1 4 3

C C 3

E E 5

S: { A, C, E }
L17.22

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example of Dijkstras algorithm


Relax all edges leaving E:
10

7 B B

2 8

11 D D
7 9

0 A A Q: A B C D E
0 10 7 7 3 11 11 5

1 4 3

C C 3

E E 5

S: { A, C, E }
L17.23

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example of Dijkstras algorithm


B EXTRACT-MIN(Q):
10

7 B B

2 8

11 D D
7 9

0 A A Q: A B C D E
0 10 7 7 3 11 11 5

1 4 3

C C 3

E E 5

S: { A, C, E, B }
L17.24

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example of Dijkstras algorithm


Relax all edges leaving B:
10

7 B B

2 8

9 D D
7 9

0 A A Q: A B C D E
0 10 7 7 3 11 11 9 5

1 4 3

C C 3

E E 5

S: { A, C, E, B }
L17.25

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example of Dijkstras algorithm


D EXTRACT-MIN(Q):
10

7 B B

2 8

9 D D
7 9

0 A A Q: A B C D E
0 10 7 7 3 11 11 9 5

1 4 3

C C 3

E E 5

S: { A, C, E, B, D }
L17.26

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Correctness Part I
Lemma. Initializing d[s] 0 and d[v] for all v V {s} establishes d[v] (s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.27

Correctness Part I
Lemma. Initializing d[s] 0 and d[v] for all v V {s} establishes d[v] (s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps. Proof. Suppose not. Let v be the first vertex for which d[v] < (s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then, d[v] < (s, v) supposition (s, u) + (u, v) triangle inequality (s,u) + w(u, v) sh. path specific path d[u] + w(u, v) v is first violation Contradiction.
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.28

Correctness Part II
Lemma. Let u be vs predecessor on a shortest path from s to v. Then, if d[u] = (s, u) and edge (u, v) is relaxed, we have d[v] = (s, v) after the relaxation.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.29

Correctness Part II
Lemma. Let u be vs predecessor on a shortest path from s to v. Then, if d[u] = (s, u) and edge (u, v) is relaxed, we have d[v] = (s, v) after the relaxation. Proof. Observe that (s, v) = (s, u) + w(u, v). Suppose that d[v] > (s, v) before the relaxation. (Otherwise, were done.) Then, the test d[v] > d[u] + w(u, v) succeeds, because d[v] > (s, v) = (s, u) + w(u, v) = d[u] + w(u, v), and the algorithm sets d[v] = d[u] + w(u, v) = (s, v).
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.30

Correctness Part III


Theorem. Dijkstras algorithm terminates with d[v] = (s, v) for all v V.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.31

Correctness Part III


Theorem. Dijkstras algorithm terminates with d[v] = (s, v) for all v V.
Proof. It suffices to show that d[v] = (s, v) for every v V when v is added to S. Suppose u is the first vertex added to S for which d[u] > (s, u). Let y be the first vertex in V S along a shortest path from s to u, and let x be its predecessor:

u u S, just before adding u.


November 14, 2005

s s

x x

y y
L17.32

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Correctness Part III (continued)


S s s x x y y u u

Since u is the first vertex violating the claimed invariant, we have d[x] = (s, x). When x was added to S, the edge (x, y) was relaxed, which implies that d[y] = (s, y) (s, u) < d[u]. But, d[u] d[y] by our choice of u. Contradiction.
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.33

Analysis of Dijkstra
while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v)

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.34

Analysis of Dijkstra
|V | times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v)

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.35

Analysis of Dijkstra
|V | times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] degree(u) do if d[v] > d[u] + w(u, v) times then d[v] d[u] + w(u, v)

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.36

Analysis of Dijkstra
|V | times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] degree(u) do if d[v] > d[u] + w(u, v) times then d[v] d[u] + w(u, v)

Handshaking Lemma (E) implicit DECREASE-KEYs.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.37

Analysis of Dijkstra
|V | times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] degree(u) do if d[v] > d[u] + w(u, v) times then d[v] d[u] + w(u, v)

Handshaking Lemma (E) implicit DECREASE-KEYs.

Time = (VTEXTRACT-MIN + ETDECREASE-KEY)


Note: Same formula as in the analysis of Prims minimum spanning tree algorithm.
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.38

Analysis of Dijkstra (continued)


Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q TEXTRACT-MIN TDECREASE-KEY Total

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.39

Analysis of Dijkstra (continued)


Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q array TEXTRACT-MIN TDECREASE-KEY O(V) O(1) Total O(V2)

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.40

Analysis of Dijkstra (continued)


Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q array binary heap TEXTRACT-MIN TDECREASE-KEY O(V) O(lg V) O(1) O(lg V) Total O(V2) O(E lg V)

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.41

Analysis of Dijkstra (continued)


Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q array binary heap TEXTRACT-MIN TDECREASE-KEY O(V) O(lg V) O(1) O(lg V) Total O(V2) O(E lg V)

Fibonacci O(lg V) heap amortized


November 14, 2005

O(1) O(E + V lg V) amortized worst case


L17.42

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Unweighted graphs
Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstras algorithm be improved?

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.43

Unweighted graphs
Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstras algorithm be improved? Use a simple FIFO queue instead of a priority queue.

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.44

Unweighted graphs
Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstras algorithm be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first search
while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v)

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.45

Unweighted graphs
Suppose that w(u, v) = 1 for all (u, v) E. Can Dijkstras algorithm be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first search
while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v)

Analysis: Time = O(V + E).


November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.46

Example of breadth-first search


a a d d b b e e c c Q:
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.47

ff g g

h h

ii

Example of breadth-first search


0

a a d d b b e e c c Q: a
0

ff g g

h h

ii

November 14, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L17.48

Example of breadth-first search


0

a a b b

ff g g

h h

d d
1

e e c c
1 1

ii

Q: a b d
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.49

Example of breadth-first search


0

a a b b

ff g g

h h

d d
1

e e
2

ii

c c

2 1 2 2

Q: a b d c e
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.50

Example of breadth-first search


0

a a b b

ff g g

h h

d d
1

e e
2

ii
2 2

c c

Q: a b d c e
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.51

Example of breadth-first search


0

a a b b

ff g g

h h

d d
1

e e
2

ii
2

c c

Q: a b d c e
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.52

Example of breadth-first search


0

a a b b

ff
3

h h

d d
1

g g ii
3

e e
2

c c

2 3 3

Q: a b d c e g i
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.53

Example of breadth-first search


4 0

a a b b

ff
3

h h

d d
1

g g ii
3 3 4

e e
2

c c

Q: a b d c e g i f
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.54

Example of breadth-first search


4 0 4

a a b b

ff
3

h h

d d
1

g g ii
3 4 4

e e
2

c c

Q: a b d c e g i f h
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.55

Example of breadth-first search


4 0 4

a a b b

ff
3

h h

d d
1

g g ii
3 4

e e
2

c c

Q: a b d c e g i f h
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.56

Example of breadth-first search


4 0 4

a a b b

ff
3

h h

d d
1

g g ii
3

e e
2

c c

Q: a b d c e g i f h
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.57

Example of breadth-first search


4 0 4

a a b b

ff
3

h h

d d
1

g g ii
3

e e
2

c c

Q: a b d c e g i f h
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.58

Correctness of BFS
while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v)

Key idea: The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra. Invariant: v comes after u in Q implies that d[v] = d[u] or d[v] = d[u] + 1.
November 14, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L17.59

You might also like