Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson - - PowerPoint PPT Presentation

shortest paths
SMART_READER_LITE
LIVE PREVIEW

Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson - - PowerPoint PPT Presentation

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 6610 Algorithms Paths in graphs Consider a digraph G = ( V , E ) with an edge-weight function w : E .


slide-1
SLIDE 1

CMPS 6610 Algorithms

CMPS 6610 – Fall 2018

Shortest Paths

Carola Wenk

Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

slide-2
SLIDE 2

2 CMPS 6610 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 1 1)

, ( ) (

k i i i v

v w p w .

v1 v2 v3 v4 v5

4 –2 –5 1

Example: w(p) = –2

slide-3
SLIDE 3

3 CMPS 6610 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.

slide-4
SLIDE 4

4 CMPS 6610 Algorithms

Optimal substructure

  • Theorem. A subpath of a shortest path is a

shortest path.

  • Proof. Cut and paste:
slide-5
SLIDE 5

5 CMPS 6610 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
  • ver all paths from u

to v

  • Concatenating two

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

slide-6
SLIDE 6

6 CMPS 6610 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

slide-7
SLIDE 7

7 CMPS 6610 Algorithms

Single-source shortest paths

  • Problem. 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.
slide-8
SLIDE 8

8 CMPS 6610 Algorithms

Dijkstra’s algorithm

d[s]  0 for 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) then d[v]  d[u] + w(u, v)

relaxation step implicit DECREASE-KEY in Q

slide-9
SLIDE 9

9 CMPS 6610 Algorithms

Dijkstra’s algorithm

d[s]  0 for 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) then d[v]  d[u] + w(u, v)

relaxation step implicit DECREASE-KEY in Q

Q  V PRIM’s algorithm key[v]   for all v  V key[s]  0 for some arbitrary s  V while 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
slide-10
SLIDE 10

10 CMPS 6610 Algorithms

How to find the actual shortest paths?

d[s]  0 for 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) then d[v]  d[u] + w(u, v) [v]  u

Store a predecessor tree:

slide-11
SLIDE 11

11 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

Graph with nonnegative edge weights:

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) [v]  u

slide-12
SLIDE 12

12 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

Initialize: A B C D E Q:

   

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) [v]  u

slide-13
SLIDE 13

13 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A }     “A”  EXTRACT-MIN(Q): A B C D E :

    

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) [v]  u

slide-14
SLIDE 14

14 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A } 10 3  

10   

Relax all edges leaving A: A B C D E :

 

  • 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) [v]  u

 

slide-15
SLIDE 15

15 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A } 10 3  

10   

Relax all edges leaving A: A B C D E :

   

  • 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) [v]  u

slide-16
SLIDE 16

16 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C } 10 3  

10   

“C”  EXTRACT-MIN(Q):

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) [v]  u

A B C D E :

   

slide-17
SLIDE 17

17 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C } 7 3 5 11

10    7 11 5

Relax all edges leaving C:

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) [v]  u

A B C D E :

   

slide-18
SLIDE 18

18 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C } 7 3 5 11

10    7 11 5

Relax all edges leaving C:

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) [v]  u

A B C D E :

 C  C C

slide-19
SLIDE 19

19 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C, E } 7 3 5 11

10    7 11 5

“E”  EXTRACT-MIN(Q): A B C D E :

 C  C C

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) [v]  u

slide-20
SLIDE 20

20 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C, E } 7 3 5 11

10    7 11 5 7 11

Relax all edges leaving E: A B C D E :

 C  C C

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) [v]  u

slide-21
SLIDE 21

21 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C, E, B } 7 3 5 11

10    7 11 5 7 11

“B”  EXTRACT-MIN(Q): A B C D E :

 C  C C

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) [v]  u

slide-22
SLIDE 22

22 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C, E, B } 7 3 5 9

10    7 11 5 7 11

Relax all edges leaving B:

9

A B C D E :

 C  B C

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) [v]  u

slide-23
SLIDE 23

23 CMPS 6610 Algorithms

Example of Dijkstra’s algorithm

A B D C E

10 3 1 4 7 9 8 2 2

A B C D E Q:

   

S: { A, C, E, B, D}0 7 3 5 9

10    7 11 5 7 11 9

“D”  EXTRACT-MIN(Q): A B C D E :

 C  B C

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) [v]  u

slide-24
SLIDE 24

24 CMPS 6610 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   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)

slide-25
SLIDE 25

25 CMPS 6610 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

slide-26
SLIDE 26

26 CMPS 6610 Algorithms

Correctness

  • 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.

  • Corollary. Dijkstra’s algorithm terminates with d[v]

= (s, v) for all v  V.

slide-27
SLIDE 27

27 CMPS 6610 Algorithms

Correctness

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

vs, 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.

slide-28
SLIDE 28

28 CMPS 6610 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

  • f (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

slide-29
SLIDE 29

29 CMPS 6610 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

slide-30
SLIDE 30

30 CMPS 6610 Algorithms

Correctness

  • (ii) Let v  S. Let p be a shortest path from s to v that uses
  • nly (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.

slide-31
SLIDE 31

31 CMPS 6610 Algorithms

Unweighted graphs

Suppose 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] + 1 ENQUEUE(Q, v)

  • Use a simple FIFO queue instead of a priority

queue.

  • Breadth-first search

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

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)

slide-32
SLIDE 32

32 CMPS 6610 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] + 1 ENQUEUE(Q, v)

slide-33
SLIDE 33

33 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: d[v]

slide-34
SLIDE 34

34 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: a d[v] 0

slide-35
SLIDE 35

35 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: a b d

1 2 1 2

d[v] 0

1 1

slide-36
SLIDE 36

36 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: a b d c e

1 2 3 4 2 3 4

d[v] 0

1 1 2 2

slide-37
SLIDE 37

37 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: a b d c e

1 2 3 4 3 4

d[v] 0

1 1 2 2

slide-38
SLIDE 38

38 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: a b d c e

1 2 3 4 4

d[v] 0

1 1 2 2

slide-39
SLIDE 39

39 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: a b d c e f g

1 2 3 4 5 6 5 6

d[v] 0

1 1 2 2 3 3

slide-40
SLIDE 40

40 CMPS 6610 Algorithms

Example of breadth-first search

a b c d e f g i h Q: a b d c e f g i

1 2 3 4 5 6 7 6 7

d[v] 0

1 1 2 2 3 3 4

slide-41
SLIDE 41

41 CMPS 6610 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

1 2 3 4 5 6 7 8 7 8

a a

d[v] 0

1 1 2 2 3 3 4 4

slide-42
SLIDE 42

42 CMPS 6610 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

1 2 3 4 5 6 7 8 8

a a

d[v] 0

1 1 2 2 3 3 4 4

slide-43
SLIDE 43

43 CMPS 6610 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

1 2 3 4 5 6 7 8

a a

d[v] 0

1 1 2 2 3 3 4 4

slide-44
SLIDE 44

44 CMPS 6610 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

1 2 3 4 5 6 7 8

a a

Distance to a: 1 2 3 4

d[v] 0

1 1 2 2 3 3 4 4

slide-45
SLIDE 45

45 CMPS 6610 Algorithms

Negative-weight cycles

Recall: 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.

slide-46
SLIDE 46

46 CMPS 6610 Algorithms

Bellman-Ford algorithm

d[s]  0 for each v  V – {s} do d[v]   for i  1 to |V| – 1 do for 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)  E do 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

slide-47
SLIDE 47

47 CMPS 6610 Algorithms

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

A B C D E        

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

slide-48
SLIDE 48

48 CMPS 6610 Algorithms

 –1 –1   

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

A B C D E       

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

slide-49
SLIDE 49

49 CMPS 6610 Algorithms

 –1 –1   

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

A B C D E        –1   

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

slide-50
SLIDE 50

50 CMPS 6610 Algorithms

 –1 2     –1 –1   

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

A B C D E       –1   

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

slide-51
SLIDE 51

51 CMPS 6610 Algorithms

 –1

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

   –1 2   –1    A B C D E     –1   

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

slide-52
SLIDE 52

52 CMPS 6610 Algorithms

 –1

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

  –1 2   –1    A B C D E     –1     –1 2  1

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

slide-53
SLIDE 53

53 CMPS 6610 Algorithms

 –1 2 1 1 1  –1

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

 –1 2   –1    A B C D E     –1     –1 2  1

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

slide-54
SLIDE 54

54 CMPS 6610 Algorithms

1 –1 2 –2 1 –2 –1 2 1 1  –1

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

 –1 2   –1    A B C D E     –1     –1 2  1

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

slide-55
SLIDE 55

55 CMPS 6610 Algorithms

1 –1 2 –2 1 –2 –1 2 1 1  –1

Example of Bellman-Ford

A B E C D

–1 4 1 2 –3 2 5 3

 –1 2   –1    A B C D E     –1     –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

slide-56
SLIDE 56

56 CMPS 6610 Algorithms

Correctness

  • Theorem. 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. v1 v2 v3 vk v0

… s v p:

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

slide-57
SLIDE 57

57 CMPS 6610 Algorithms

Correctness (continued)

v1 v2 v3 vk v0

… 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.

slide-58
SLIDE 58

58 CMPS 6610 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.

slide-59
SLIDE 59

59 CMPS 6610 Algorithms

DAG shortest paths

If 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 8 1 3 5 6 4 2 7 9 8 1

slide-60
SLIDE 60

60 CMPS 6610 Algorithms

DAG shortest paths

If 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 2 s 7 9 8 1

slide-61
SLIDE 61

61 CMPS 6610 Algorithms

Shortest paths

Single-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

slide-62
SLIDE 62

62 CMPS 6610 Algorithms

All-pairs shortest paths

Input: Digraph G = (V, E), where |V| = n, with edge-weight function w : E  . 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.
slide-63
SLIDE 63

63 CMPS 6610 Algorithms

Shortest paths

Single-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)
slide-64
SLIDE 64

64 CMPS 6610 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 given

in an adjacency matrix A=(aij)1i,jn where aij is the weight of the edge from i to j.

slide-65
SLIDE 65

65 CMPS 6610 Algorithms

Floyd-Warshall recurrence

cij

(k) = min {cij (k–1), cik (k–1) + ckj (k–1)}

i j k i cij

(k–1)

cik

(k–1)

ckj

(k–1)

intermediate vertices in {1, 2, …, k-1} Use vertex k Do not use vertex k

slide-66
SLIDE 66

66 CMPS 6610 Algorithms

Pseudocode for Floyd- Warshall

for k  1 to n do for i  1 to n do for j  1 to n do if cij

(k-1) > cik

(k-1) + ckj (k-1) then

cij

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

else

cij

(k)  cij (k-1)

relaxation

  • Runs in (n3) time and space
  • Simple to code.
  • Efficient in practice.
slide-67
SLIDE 67

67 CMPS 6610 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

  • f (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)}

slide-68
SLIDE 68

68 CMPS 6610 Algorithms

Shortest paths

Single-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
slide-69
SLIDE 69

69 CMPS 6610 Algorithms

Graph reweighting

  • Theorem. 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  L  vk be a path in the graph.

 

) ( ) ( ) ( ) ( ) ( ) , ( ) ( ) ( ) , ( ) , ( ˆ ) ( ˆ

1 1 1 1 1 1 1 1 1 1 1 1 k k k i i i k i i i i i k i i i

v h v h p w v h v h v v w v h v h v v w v v w p w          

  

         

. Then, we have

slide-70
SLIDE 70

70 CMPS 6610 Algorithms

Johnson’s algorithm

  • 1. 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)

  • f the original graph G.
  • Time = O(|V| 2)

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

^ ^

slide-71
SLIDE 71

71 CMPS 6610 Algorithms

Shortest paths

Single-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. matrix
  • adj. list