You are on page 1of 38

Shortest Paths I: Properties,

Dijkstra's Algorithm, Breadthfirst Search

Lecture 14

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
k 1

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

Example:
v
4
1

v
2

v
3

v
4

v
5

w(p) = 2
L14.2

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
d(u, v) = min{w(p) : p is a path from u to v}.
Note: d(u, v) = if no path from u to v exists.

L14.3

Optimal substructure
Theorem. A subpath of a shortest path is a
shortest path.
Proof. Cut and paste:

L14.4

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

u
d(u, x)

v
d(x, v)

x
L14.5

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

<0
u

L14.6

Single-source shortest paths


Problem. From a given source vertex s V, find
the shortest-path weights d(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.
L14.7

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
L14.8

Example of Dijkstras
algorithm
Graph with
nonnegative
edge weights:

10

B
1 4

2
8

D
7 9

L14.9

Example of Dijkstras
algorithm

Initialize:
10

0 A
Q: A B C D E
0

1 4
3

2
8

D
7 9

S: {}
L14.10

Example of Dijkstras
algorithm
A EXTRACT-MIN(Q):
10

0 A
Q: A B C D E
0

1 4
3

2
8

D
7 9

S: { A }
L14.11

Example of Dijkstras
algorithm
Relax all edges leaving A:
10

0 A
Q: A B C D E
0

10

10
B
1 4

C
3

2
8

D
7 9

S: { A }
L14.12

Example of Dijkstras
algorithm
C EXTRACT-MIN(Q):
10

0 A
Q: A B C D E
0

10

10
B
1 4

C
3

2
8

D
7 9

S: { A, C }
L14.13

Example of Dijkstras
algorithm
Relax all edges leaving C:
10

0 A
Q: A B C D E
0

10
7

11

7
B
1 4

C
3

2
8

11
D
7 9

E
5

S: { A, C }
L14.14

Example of Dijkstras
algorithm
E EXTRACT-MIN(Q):
10

0 A
Q: A B C D E
0

10
7

11

7
B
1 4

2
8

11
D
7 9

E
5

S: { A, C, E }
L14.15

Example of Dijkstras
algorithm
Relax all edges leaving E:
10

0 A
Q: A B C D E
0

10
7
7

11
11

7
B
1 4

2
8

11
D
7 9

E
5

S: { A, C, E }
L14.16

Example of Dijkstras
algorithm
B EXTRACT-MIN(Q):
10

0 A
Q: A B C D E
0

10
7
7

11
11

7
B
1 4

2
8

11
D
7 9

E
5

S: { A, C, E, B }
L14.17

Example of Dijkstras
algorithm
Relax all edges leaving B:
10

0 A
Q: A B C D E
0

10
7
7

11
11
9

7
B
1 4

9
D

2
8

7 9

E
5

S: { A, C, E, B }
L14.18

Example of Dijkstras
algorithm
D EXTRACT-MIN(Q):
10

0 A
Q: A B C D E
0

10
7
7

11
11
9

7
B
1 4

C
3

2
8

9
D
7 9

E
5

S: { A, C, E, B, D }
L14.19

Correctness Part I
Lemma. Initializing d[s] 0 and d[v] for all
v V {s} establishes d[v] d(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] < d(s, v), and let u be the vertex that
caused d[v] to change: d[v] = d[u] + w(u, v). Then,
d[v] < d(s, v)
supposition
d(s, u) + d(u, v) triangle inequality
d(s,u) + w(u, v) sh. path specific path
d[u] + w(u, v)
v is first violation
Contradiction.
L14.20

Correctness Part II
Theorem. Dijkstras algorithm terminates with
d[v] = d(s, v) for all v V.
Proof. It suffices to show that d[v] = d(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] d(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
S, just before
adding u.

y
L14.21

Correctness Part II
(continued)
S
s

u
x

Since u is the first vertex violating the claimed invariant,


we have d[x] = d(s, x). Since subpaths of shortest paths
are shortest paths, it follows that d[y] was set to d(s, x) +
w(x, y) = d(s, y) when (x, y) was relaxed just after x was
added to S. Consequently, we have d[y] = d(s, y) d(s, u)
d[u]. But, d[u] d[y] by our choice of u, and hence d[y]
= d(s, y) d(s, u) d[u]. Contradiction.
L14.22

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 Q(E) implicit DECREASE-KEYs.

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


Note: Same formula as in the analysis of Prims
minimum spanning tree algorithm.
L14.23

Analysis of Dijkstra
(continued)
Time = Q(V)TEXTRACT-MIN + Q(E)TDECREASE-KEY
Q

TEXTRACT-MIN TDECREASE-KEY

Total

array

O(V)

O(1)

O(V2)

binary
heap

O(lg V)

O(lg V)

O(E lg V)

Fibonacci O(lg V)
heap
amortized

O(1)
O(E + V lg V)
amortized worst case
L14.24

Unweighted graphs
Suppose w(u, v) = 1 for all (u, v) E. Can the
code for Dijkstra 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).


L14.25

Example of breadth-first
search
a

d
b

g
e

c
Q:
L14.26

Example of breadth-first
search
0

d
b

g
e

c
0

Q: a
L14.27

Example of breadth-first
search
0

d
1

g
e

c
1 1

Q: a b d
L14.28

Example of breadth-first
search
0

d
1

g
e

2
1 2 2

Q: a b d c e
L14.29

Example of breadth-first
search
0

d
1

g
e

2
2 2

Q: a b d c e
L14.30

Example of breadth-first
search
0

d
1

g
e

2
2

Q: a b d c e
L14.31

Example of breadth-first
search
0

d
1

b
c

3
3 3

Q: a b d c e g i
L14.32

Example of breadth-first
search
4
0

d
1

b
c

3
3 4

Q: a b d c e g i f
L14.33

Example of breadth-first
search
0

d
1

b
c

3
4 4

Q: a b d c e g i f h
L14.34

Example of breadth-first
search
0

d
1

b
c

3
4

Q: a b d c e g i f h
L14.35

Example of breadth-first
search
0

d
1

b
c

Q: a b d c e g i f h
L14.36

Example of breadth-first
search
0

d
1

b
c

Q: a b d c e g i f h
L14.37

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.
L14.38

You might also like