single source shortest paths · cmps 6610/4610 algorithms 7 single-source shortest paths problem....

82
CMPS 6610/4610 Algorithms CMPS 6610/4610 – Fall 2016 Single Source Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Upload: others

Post on 13-Oct-2020

38 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

CMPS 6610/4610 Algorithms

CMPS 6610/4610 – Fall 2016

Single Source Shortest Paths Carola Wenk

Slides courtesy of Charles Leiserson with changesand additions by Carola Wenk

Page 2: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

2CMPS 6610/4610 Algorithms

Paths in graphs

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

1

11),()(

k

iii vvwpw .

v1v2

v3v4

v54 –2 –5 1

Example:

w(p) = –2

Page 3: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

3CMPS 6610/4610 Algorithms

Shortest paths

A shortest path from u to v is a path of minimum weight from u to v.

The shortest-path 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.

Page 4: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

4CMPS 6610/4610 Algorithms

Optimal substructure

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

Proof. Cut and paste:

Page 5: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

5CMPS 6610/4610 Algorithms

Triangle inequality

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

u

Proof.

x

v(u, v)

(u, x) (x, v)

• (u,v) minimizes over all paths from uto v

• Concatenating two shortest paths from u to x and from x to v yields one specific path from u to v

Page 6: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

6CMPS 6610/4610 Algorithms

Well-definedness of shortest paths

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

Example:

u v

< 0

- 6 5

- 8 2

Page 7: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

7CMPS 6610/4610 Algorithms

Single-source shortest pathsProblem. From a given source vertex s V, find the shortest-path weights (s, v) for all v V.Assumption:All edge weights w(u, v) are non-negative.It follows that all shortest-path weights must exist.

IDEA: Greedy.1. Maintain a set S of vertices whose shortest-path weights from

s are known, i.e., d[v]=(s,v)2. At each step add to S the vertex u V – S whose distance

estimate d[u] from s is minimal.3. Update the distance estimates d[v] of vertices v adjacent to u.

Page 8: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

8CMPS 6610/4610 Algorithms

Dijkstra’s algorithmd[s] 0for each v V – {s}

do d[v] S Vertices for which d[v]=d(s,v)Q V Q is a priority queue maintaining V – S

sorted by d-values d[v]while Q do

u EXTRACT-MIN(Q)S S {u}for each v Adj[u] do

if d[v] > d[u] + w(u, v) thend[v] d[u] + w(u, v) relaxation step

implicit DECREASE-KEY in Q

Page 9: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

9CMPS 6610/4610 Algorithms

Dijkstra’s algorithmd[s] 0for each v V – {s}

do d[v] S Vertices for which d[v]=d(s,v)Q V Q is a priority queue maintaining V – S

sorted by d-values d[v]while Q do

u EXTRACT-MIN(Q)S S {u}for each v Adj[u] do

if d[v] > d[u] + w(u, v) thend[v] d[u] + w(u, v) relaxation step

implicit DECREASE-KEY in Q

Q V PRIM’s algorithmkey[v] for all v Vkey[s] 0 for some arbitrary s Vwhile Q

do u EXTRACT-MIN(Q)for each v Adj[u]

do if v Q and w(u, v) < key[v]then key[v] w(u, v)

[v] u

Difference to Prim’s:• It suffices to only check v Q, but it doesn’t hurt to check all v• Add d[u] to the weight

Page 10: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

10CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2

Graph with nonnegative edge weights:

while Q dou 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)

Page 11: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

11CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2

Initialize:

A B C D EQ:0

S: {} 0

while Q dou 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)

Page 12: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

12CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A } 0

“A” EXTRACT-MIN(Q):

while Q dou 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)

Page 13: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

13CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A } 0

10

3

10

Relax all edges leaving A:

while Q dou 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)

Page 14: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

14CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C } 0

10

3

10

“C” EXTRACT-MIN(Q):

while Q dou 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)

Page 15: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

15CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C } 0

7

3 5

11

10 7 11 5

Relax all edges leaving C:

while Q dou 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)

Page 16: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

16CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E } 0

7

3 5

11

10 7 11 5

“E” EXTRACT-MIN(Q):

while Q dou 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)

Page 17: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

17CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E } 0

7

3 5

11

10 7 11 57 11

Relax all edges leaving E:

while Q dou 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)

Page 18: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

18CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B } 0

7

3 5

11

10 7 11 57 11

“B” EXTRACT-MIN(Q):

while Q dou 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)

Page 19: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

19CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B } 0

7

3 5

9

10 7 11 57 11

Relax all edges leaving B:

9

while Q dou 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)

Page 20: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

20CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B, D}0

7

3 5

9

10 7 11 57 11

9

“D” EXTRACT-MIN(Q):

while Q dou 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)

Page 21: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

21CMPS 6610/4610 Algorithms

Analysis of Dijkstra

degree(u)times

|V |times

Handshaking Lemma (|E|) implicit DECREASE-KEY’s.Time = (|V|)·TEXTRACT-MIN + (|E|)·TDECREASE-KEY

while Q dou EXTRACT-MIN(Q)S S {u}for each v Adj[u] do

if d[v] > d[u] + w(u, v) thend[v] d[u] + w(u, v)

Page 22: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

22CMPS 6610/4610 Algorithms

Analysis of Dijkstra (continued)

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

Q TEXTRACT-MIN TDECREASE-KEY Total

array O(|V|) O(1) O(|V|2)binary heap O(log |V|) O(log |V|) O(|E| log |V|)

Fibonacci heap

O(log |V|)amortized

O(1)amortized

O(|E| + |V| log |V|)worst case

Page 23: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

23CMPS 6610/4610 Algorithms

CorrectnessTheorem. (i) For all v S: d[v] = (s, v)(ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S.

Corollary. Dijkstra’s algorithm terminates with d[v] = (s, v) for all v V.

Page 24: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

24CMPS 6610/4610 Algorithms

Correctness

Proof. By induction.• Base: Before the while loop, d[s]=0 and d[v]= for all

vs, so (i) and (ii) are true.• Step: Assume (i) and (ii) are true before an iteration; now we need to show they remain true after another iteration. Let u be the vertex added to S, so d[u] ≤ d[v] for all other v S.

Theorem. (i) For all v S: d[v] = (s, v)(ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S.

Page 25: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

25CMPS 6610/4610 Algorithms

Correctness

• (i) Need to show that d[u] = (s, u). Assume the contrary. There is a path p from s to u with w(p) < d[u]. Because of (ii) that path uses vertices S, in addition to u. Let y be first vertex on p such that y S.

Theorem. (i) For all v S: d[v] = (s, v)(ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S.

s x y

u

S, just before adding u.

Path p from s to u

Page 26: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

26CMPS 6610/4610 Algorithms

Correctness

d[y] ≤ w(p) < d[u]. Contradiction to the choice of u.

Theorem. (i) For all v S: d[v] = (s, v)(ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S.

s x y

u

S, just before adding u.

Path p from s to u

weights are nonnegative

assumption about path

Page 27: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

27CMPS 6610/4610 Algorithms

Correctness

• (ii) Let v S. Let p be a shortest path from s to v that uses only (besides v itself) vertices in S.

• p does not contain u: (ii) true by inductive hypothesis• p contains u: p consists of vertices in S\{u} and ends with an edge from u to v. w(p)=d[u]+w(u,v), which is the value of d[v] after adding u. So (ii) is true.

Theorem. (i) For all v S: d[v] = (s, v)(ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S.

Page 28: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

28CMPS 6610/4610 Algorithms

Unweighted graphsSuppose w(u, v) = 1 for all (u, v) E. Can the code for Dijkstra be improved?

while Q do u DEQUEUE(Q)

for each v Adj[u]do if d[v] =

then d[v] d[u] + 1ENQUEUE(Q, v)

• Use a simple FIFO queue instead of a priority queue.

• Breadth-first search

Analysis: Time = O(|V| + |E|).

while Q dou 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)

Page 29: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

29CMPS 6610/4610 Algorithms

Correctness of BFS

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.

while Q do u DEQUEUE(Q)

for each v Adj[u]do if d[v] =

then d[v] d[u] + 1ENQUEUE(Q, v)

Page 30: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

30CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q:d[v]

Page 31: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

31CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a

0

0

d[v] 0

Page 32: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

32CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d

0

1

2

1 2

d[v] 0 1 1

Page 33: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

33CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e

0

1

2

3 4

2 3 4

d[v] 0 1 1 2 2

Page 34: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

34CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e

0

1

2

3 4

3 4

d[v] 0 1 1 2 2

Page 35: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

35CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e

0

1

2

3 4

4

d[v] 0 1 1 2 2

Page 36: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

36CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e f g

0

1

2

3 4

5

6

5 6

d[v] 0 1 1 2 2 3 3

Page 37: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

37CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e f g i

0

1

2

3 4

5

6

7

6 7

d[v] 0 1 1 2 2 3 3 4

Page 38: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

38CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e f g i h

0

1

2

3 4

5

6

7 8

7 8

a

a

d[v] 0 1 1 2 2 3 3 4 4

Page 39: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

39CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e f g i h

0

1

2

3 4

5

6

7 8

8

a

a

d[v] 0 1 1 2 2 3 3 4 4

Page 40: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

40CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e f g i h

0

1

2

3 4

5

6

7 8

a

a

d[v] 0 1 1 2 2 3 3 4 4

Page 41: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

41CMPS 6610/4610 Algorithms

Example of breadth-first search

a

b

c

d

e

f

g

i h

Q: a b d c e f g i h

0

1

2

3 4

5

6

7 8

a

a

Distance to a:

0

1

234

d[v] 0 1 1 2 2 3 3 4 4

Page 42: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

42CMPS 6610/4610 Algorithms

How to find the actual shortest paths?

Store a predecessor tree:d[s] 0for each v V – {s}

do d[v] S Q V Q is a priority queue maintaining V – Swhile 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)

[v] u

Page 43: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

43CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2

Graph with nonnegative edge weights:

while Q dou 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)[v] u

Page 44: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

44CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2

Initialize:

A B C D EQ:0

S: {} 0

while Q dou 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)[v] u

Page 45: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

45CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A } 0

“A” EXTRACT-MIN(Q):

A B C D E:

while Q dou 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)[v] u

Page 46: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

46CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A } 0

10

3

10

Relax all edges leaving A:

A B C D E: -

while Q dou 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)[v] u

Page 47: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

47CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A } 0

10

3

10

Relax all edges leaving A:

A B C D E: -

while Q dou 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)[v] u

Page 48: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

48CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C } 0

10

3

10

“C” EXTRACT-MIN(Q):

while Q dou 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)[v] u

A B C D E: -

Page 49: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

49CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C } 0

7

3 5

11

10 7 11 5

Relax all edges leaving C:

while Q dou 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)[v] u

A B C D E: -

Page 50: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

50CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C } 0

7

3 5

11

10 7 11 5

Relax all edges leaving C:

while Q dou 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)[v] u

A B C D E: -

Page 51: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

51CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E } 0

7

3 5

11

10 7 11 5

“E” EXTRACT-MIN(Q):

A B C D E: C C C

while Q dou 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)[v] u

Page 52: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

52CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E } 0

7

3 5

11

10 7 11 57 11

Relax all edges leaving E:

A B C D E: C C C

while Q dou 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)[v] u

Page 53: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

53CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B } 0

7

3 5

11

10 7 11 57 11

“B” EXTRACT-MIN(Q):

A B C D E: C C C

while Q dou 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)[v] u

Page 54: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

54CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B } 0

7

3 5

9

10 7 11 57 11

Relax all edges leaving B:

9

A B C D E: C B C

while Q dou 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)[v] u

Page 55: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

55CMPS 6610/4610 Algorithms

Example of Dijkstra’s algorithm

A

B D

C E

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B, D}0

7

3 5

9

10 7 11 57 11

9

“D” EXTRACT-MIN(Q):

A B C D E: C B C

while Q dou 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)[v] u

Page 56: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

56CMPS 6610/4610 Algorithms

Negative-weight cyclesRecall: If a graph G = (V, E) contains a negative-weight cycle, then some shortest paths may not exist.Example:

u v

< 0

Bellman-Ford algorithm: Finds all shortest-path weights from a source s V to all v V or determines that a negative-weight cycle exists.

Page 57: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

57CMPS 6610/4610 Algorithms

Bellman-Ford algorithmd[s] 0for each v V – {s}

do d[v]

for i 1 to |V| – 1 dofor each edge (u, v) E do

if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v)[v] u

for each edge (u, v) Edo if d[v] > d[u] + w(u, v)

then report that a negative-weight cycle exists

initialization

At the end, d[v] = (s, v). Time = O(|V||E|).

relaxation step

Page 58: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

58CMPS 6610/4610 Algorithms

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

3

A B C D E0

0

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 59: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

59CMPS 6610/4610 Algorithms

–1

0 –1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

3

A B C D E0

0

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 60: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

60CMPS 6610/4610 Algorithms

–1

0 –1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

3

A B C D E0

0

0 –1

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 61: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

61CMPS 6610/4610 Algorithms

0 –1 2

–1

0 –1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

3

A B C D E0

0

0 –1

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 62: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

62CMPS 6610/4610 Algorithms

–1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

30

0 –1 2

0 –1

A B C D E0

0 –1

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 63: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

63CMPS 6610/4610 Algorithms

–1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

30

0 –1 2

0 –1

A B C D E0

0 –1

0 –1 2 1

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 64: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

64CMPS 6610/4610 Algorithms

0 –1 2 1 11

–1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

30

0 –1 2

0 –1

A B C D E0

0 –1

0 –1 2 1

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 65: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

65CMPS 6610/4610 Algorithms

10 –1 2 –2 1

–2 0 –1 2 1 1

–1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

30

0 –1 2

0 –1

A B C D E0

0 –1

0 –1 2 1

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

Page 66: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

66CMPS 6610/4610 Algorithms

10 –1 2 –2 1

–2 0 –1 2 1 1

–1

Example of Bellman-Ford

A

B

E

C D

–1

4

12

–3

2

5

30

0 –1 2

0 –1

A B C D E0

0 –1

0 –1 2 1

Note: d-values decrease monotonically.

Order of edges: (B,E), (D,B), (B,D), (A,B), (A,C), (D,C), (B,C), (E,D)

… and 2 more iterations

Page 67: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

67CMPS 6610/4610 Algorithms

CorrectnessTheorem. If G = (V, E) contains no negative-weight cycles, then after the Bellman-Ford algorithm executes, d[v] = (s, v) for all v V. Proof. Let v V be any vertex, and consider a shortest path p from s to v with the minimum number of edges.

v1v2

v3 vkv0

…s

v

p:

Since p is a shortest path, we have(s, vi) = (s, vi–1) + w(vi–1, vi) .

Page 68: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

68CMPS 6610/4610 Algorithms

Correctness (continued)

v1v2

v3 vkv0

…s

v

p:

Initially, d[v0] = 0 = (s, v0), and d[s] is unchanged by subsequent relaxations.

• After 1 pass through E, we have d[v1] = (s, v1).• After 2 passes through E, we have d[v2] = (s, v2).

...• After k passes through E, we have d[vk] = (s, vk).Since G contains no negative-weight cycles, p is simple. Longest simple path has |V| – 1 edges.

Page 69: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

69CMPS 6610/4610 Algorithms

Detection of negative-weight cycles

Corollary. If a value d[v] fails to converge after |V| – 1 passes, there exists a negative-weight cycle in G reachable from s.

Page 70: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

70CMPS 6610/4610 Algorithms

DAG shortest pathsIf the graph is a directed acyclic graph (DAG), we first topologically sort the vertices.• Determine f : V {1, 2, …, |V|} such that (u, v) E f (u) < f (v).

3 5 6

4

2

7

9

81

3 5 642 7 981

Page 71: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

71CMPS 6610/4610 Algorithms

DAG shortest pathsIf the graph is a directed acyclic graph (DAG), we first topologically sort the vertices.

• Walk through the vertices u V in this order, relaxing the edges in Adj[u], thereby obtaining the shortest paths from s in a total of O(|V| + |E|) time.

• Determine f : V {1, 2, …, |V|} such that (u, v) E f (u) < f (v).

• O(|V| + |E|) time

3 5 6

4

2s

7

9

81

Page 72: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

72CMPS 6610/4610 Algorithms

Shortest pathsSingle-source shortest paths• Nonnegative edge weights

• Dijkstra’s algorithm: O(|E| + |V| log |V|)• General: Bellman-Ford: O(|V||E|)• DAG: One pass of Bellman-Ford: O(|V| + |E|)All-pairs shortest paths

Page 73: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

73CMPS 6610/4610 Algorithms

All-pairs shortest paths

Input: Digraph G = (V, E), where |V | = n, with edge-weight function w : E R.Output: n n matrix of shortest-path lengths (i, j) for all i, j V.Algorithm #1:• Run Bellman-Ford once from each vertex.• Time = O(|V|2 |E|).• But: Dense graph O(|V|4) time.

Page 74: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

74CMPS 6610/4610 Algorithms

Shortest pathsSingle-source shortest paths• Nonnegative edge weights

• Dijkstra’s algorithm: O(|E| + |V| log |V|)• General: Bellman-Ford: O(|V||E|)• DAG: One pass of Bellman-Ford: O(|V| + |E|)All-pairs shortest paths• Nonnegative edge weights

• Dijkstra’s algorithm |V| times: O(|V||E|+|V|2 log |V|)• General

• Bellman-Ford |V| times: O(|V|2 |E|)• Floyd-Warshall: O(|V|3)

Page 75: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

75CMPS 6610/4610 Algorithms

Floyd-Warshall algorithm• Dynamic programming algorithm.

Define cij(k) = weight of a shortest path from i

to j with intermediate vertices belonging to the set {1, 2, …, k}.

i k k k k k k k k j

Thus, (i, j) = cij(n). Also, cij

(0) = aij .

• Assume V={1, 2, …, n}, and assume G is givenin an adjacency matrix A=(aij)1i,jn where aij is the weight of the edge from i to j.

Page 76: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

76CMPS 6610/4610 Algorithms

Floyd-Warshall recurrencecij

(k) = min {cij(k–1), cik

(k–1) + ckj(k–1)}

i j

k

icij

(k–1)

cik(k–1) ckj

(k–1)

intermediate vertices in {1, 2, …, k-1}

Use vertex kDo not use vertex k

Page 77: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

77CMPS 6610/4610 Algorithms

Pseudocode for Floyd-Warshall

for k 1 to n do for i 1 to n do

for j 1 to n doif cij

(k-1) > cik(k-1) + ckj

(k-1) thencij

(k) cik(k-1) + ckj

(k-1)

elsecij

(k) cij(k-1)

relaxation

• Runs in (n3) time and space• Simple to code.• Efficient in practice.

Page 78: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

78CMPS 6610/4610 Algorithms

Transitive Closure of a Directed Graph

Compute tij = 1 if there exists a path from i to j,0 otherwise.

IDEA: Use Floyd-Warshall, but with (, ) instead of (min, +):

tij(k) = tij(k–1) (tik(k–1) tkj(k–1)).

Time = (n3).Floyd-Warshall recurrence

cij(k) = min {cij

(k–1), cik(k–1) + ckj

(k–1)}

Page 79: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

79CMPS 6610/4610 Algorithms

Shortest pathsSingle-source shortest paths• Nonnegative edge weights

• Dijkstra’s algorithm: O(|E| + |V| log |V|)• General: Bellman-Ford: O(|V||E|)• DAG: One pass of Bellman-Ford: O(|V| + |E|)All-pairs shortest paths• Nonnegative edge weights

• Dijkstra’s algorithm |V| times: O(|V||E|+|V|2 log |V|)• General

• Bellman-Ford |V| times: O(|V|2 |E|)• Floyd-Warshall: O(|V|3)

adj. list

adj. list

adj. list

adj. matrix

Page 80: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

80CMPS 6610/4610 Algorithms

Graph reweightingTheorem. Given a label h(v) for each v V, reweight each edge (u, v) E by

ŵ(u, v) = w(u, v) + h(u) – h(v).Then, all paths between the same two vertices are reweighted by the same amount.Proof. Let p = v1 v2 vk be a path in the graph.

)()()(

)()(),(

)()(),(

),(ˆ)(ˆ

1

1

1

11

1

111

1

11

k

k

k

iii

k

iiiii

k

iii

vhvhpw

vhvhvvw

vhvhvvw

vvwpw

.

Then, we have

Page 81: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

81CMPS 6610/4610 Algorithms

Johnson’s algorithm1. Find a vertex labeling h, by running Bellman-Ford on

G + super-source s. Set h(v)=(s,v) or determine that a negative-weight cycle exists.By triangle inequality h(v) h(u) + w(u, v), and henceŵ(u, v) = w(u, v) + h(u) – h(v) 0.• Time = O(|V||E|)

2. Run Dijkstra’s algorithm from each vertex using ŵ.• Time = O(|V||E|+|V|2 log |V|).

3. Reweight each shortest-path weight (u,v) to compute the shortest-path weight (u,v) = (u,v) – h(u) + h(v)of the original graph G.• Time = O(|V| 2)

Total time = O(|V||E|+|V|2 log |V|).

^^

Page 82: Single Source Shortest Paths · CMPS 6610/4610 Algorithms 7 Single-source shortest paths Problem. From a given source vertex sV, find the shortest-path weights (s, v) for all vV

82CMPS 6610/4610 Algorithms

Shortest pathsSingle-source shortest paths• Nonnegative edge weights

• Dijkstra’s algorithm: O(|E| + |V| log |V|)• General: Bellman-Ford: O(|V||E|)• DAG: One pass of Bellman-Ford: O(|V| + |E|)All-pairs shortest paths• Nonnegative edge weights

• Dijkstra’s algorithm |V| times: O(|V||E|+|V|2 log |V|)• General

• Bellman-Ford |V| times: O(|V|2 |E|)• Floyd-Warshall: O(|V|3)• Johnson’s algorithm: O(|V||E|+|V|2 log |V|)

adj. list

adj. list

adj. list

adj. matrixadj. list