156
6.4: Single-Source Shortest Paths Frank Stajano Thomas Sauerwald Lent 2015 Priority Queue Q: ✟✟ H HH (t , 8), (x , 13) 0 s 0 s 5 y 13 x 7 z 8 t 1 2 3 9 2 6 4 7 10 5

6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

6.4: Single-Source Shortest PathsFrank Stajano Thomas Sauerwald

Lent 2015

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,1), (x ,1), (y ,1), (z,1)���HHH(s, 0), (t ,1), (x ,1), (y ,1), (z,1)���HHH(s, 0), (t , 10), (x ,1), (y ,1), (z,1)���HHH(s, 0), (t , 10), (x ,1), (y , 5), (z,1)���HHH(s, 0), (t , 10), (x ,1), (y , 5), (z,1)(t , 10), (x ,1),���HHH(y , 5), (z,1)(t , 8), (x ,1),���HHH(y , 5), (z,1)(t , 8), (x , 14),���HHH(y , 5), (z,1)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)

���HHH(t , 8), (x , 13)

���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

13

x

13

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 20

Page 2: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Outline

Bellman-Ford

Dijkstra’s Algorithm

All-Pairs Shortest Path

APSP via Matrix Multiplication

Johnson’s Algorithm

6.4: Single-Source Shortest Paths T.S. 2

Page 3: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

The Bellman-Ford Algorithm

BELLMAN-FORD(G,w,s)0: assert(s in G.vertices())1: for v in G.vertices()2: v.predecessor = None3: v.d = Infinity4: s.d = 05:6: repeat |V|-1 times7: for e in G.edges()8: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d9: if e.start.d + e.weight.d < e.end.d:10: e.end.d = e.start.d + e.weight11: e.end.predecessor = e.start12:13: for e in G.edges()14: if e.start.d + e.weight.d < e.end.d:15: return FALSE16: return TRUE

Can we terminate earlier if there is a pass that keeps all .d variables?

Yes, because if pass i keeps all .d variables, then so does pass i + 1.

6.4: Single-Source Shortest Paths T.S. 17

Page 4: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

The Bellman-Ford Algorithm

BELLMAN-FORD(G,w,s)0: assert(s in G.vertices())1: for v in G.vertices()2: v.predecessor = None3: v.d = Infinity4: s.d = 05:6: repeat |V|-1 times7: for e in G.edges()8: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d9: if e.start.d + e.weight.d < e.end.d:10: e.end.d = e.start.d + e.weight11: e.end.predecessor = e.start12:13: for e in G.edges()14: if e.start.d + e.weight.d < e.end.d:15: return FALSE16: return TRUE

Can we terminate earlier if there is a pass that keeps all .d variables?

Yes, because if pass i keeps all .d variables, then so does pass i + 1.

6.4: Single-Source Shortest Paths T.S. 17

Page 5: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

The Bellman-Ford Algorithm

BELLMAN-FORD(G,w,s)0: assert(s in G.vertices())1: for v in G.vertices()2: v.predecessor = None3: v.d = Infinity4: s.d = 05:6: repeat |V|-1 times7: for e in G.edges()8: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d9: if e.start.d + e.weight.d < e.end.d:10: e.end.d = e.start.d + e.weight11: e.end.predecessor = e.start12:13: for e in G.edges()14: if e.start.d + e.weight.d < e.end.d:15: return FALSE16: return TRUE

Can we terminate earlier if there is a pass that keeps all .d variables?

Yes, because if pass i keeps all .d variables, then so does pass i + 1.

6.4: Single-Source Shortest Paths T.S. 17

Page 6: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

The Bellman-Ford Algorithm (modified)

BELLMAN-FORD-NEW(G,w,s)0: assert(s in G.vertices())1: for v in G.vertices()2: v.predecessor = None3: v.d = Infinity4: s.d = 05:6: repeat |V| times7: flag = 08: for e in G.edges()9: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d10: if e.start.d + e.weight.d < e.end.d:11: e.end.d = e.start.d + e.weight12: e.end.predecessor = e.start13: flag = 114: if flag = 0 return TRUE15:16: return FALSE

Can we terminate earlier if there is a pass that keeps all .d variables?

Yes, because if pass i keeps all .d variables, then so does pass i + 1.

6.4: Single-Source Shortest Paths T.S. 17

Page 7: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

The Bellman-Ford Algorithm (modified)

BELLMAN-FORD-NEW(G,w,s)0: assert(s in G.vertices())1: for v in G.vertices()2: v.predecessor = None3: v.d = Infinity4: s.d = 05:6: repeat |V| times7: flag = 08: for e in G.edges()9: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d10: if e.start.d + e.weight.d < e.end.d:11: e.end.d = e.start.d + e.weight12: e.end.predecessor = e.start13: flag = 114: if flag = 0 return TRUE15:16: return FALSE

Can we terminate earlier if there is a pass that keeps all .d variables?

Yes, because if pass i keeps all .d variables, then so does pass i + 1.

6.4: Single-Source Shortest Paths T.S. 17

Page 8: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Outline

Bellman-Ford

Dijkstra’s Algorithm

All-Pairs Shortest Path

APSP via Matrix Multiplication

Johnson’s Algorithm

6.4: Single-Source Shortest Paths T.S. 21

Page 9: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edges

The order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 10: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 11: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d

2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 12: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d

2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

6

55

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 13: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)

3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

6

55

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 14: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)

3. Relax all edges leaving v

Overview of Dijkstra

5

5

5

0

s

6

55

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 15: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)

3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

6

55

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 16: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

6

55

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 17: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

6

55

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 18: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

6

5

5

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 19: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

65

5

99

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 20: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

65

5

9

9

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 21: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

65

5

∞9

9

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 22: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

65

5

∞9

9

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 23: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

5

5

0

s

65

5

∞9

9

4

4

∞∞

3

1

51

3 1

2

3

0

41

2

1

3

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 24: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 25: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Dijkstra’s Algorithm

Requires that all edges have non-negative weights

Use a special order for relaxing edgesThe order follows a greedy-strategy (similar to Prim’s algorithm):

1. Maintain set S of vertices u with u.δ = u.d2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ)3. Relax all edges leaving v

Overview of Dijkstra

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

can actually skip edges which go to a vertex in S(cf. implementation of Dijksta in the handout)

6.4: Single-Source Shortest Paths T.S. 22

Page 26: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

∞y

∞y

∞t

∞t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 27: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

∞y

∞y

∞t

∞t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 28: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

∞y

∞y

∞t

∞t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 29: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

∞y

∞y

10

t

10

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 30: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

∞y

∞y

10

t

10

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 31: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

10

t

10

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 32: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)

���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)

(t , 10), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

10

t

10

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 33: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)

(t , 10), (x ,∞),���HHH(y , 5), (z,∞)

(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

10

t

10

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 34: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)

(t , 10), (x ,∞),���HHH(y , 5), (z,∞)

(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

10

t

10

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 35: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)

(t , 8), (x ,∞),���HHH(y , 5), (z,∞)

(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 36: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)

(t , 8), (x ,∞),���HHH(y , 5), (z,∞)

(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

∞x

∞x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 37: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)

(t , 8), (x , 14),���HHH(y , 5), (z,∞)

(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 38: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)

(t , 8), (x , 14),���HHH(y , 5), (z,∞)

(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

∞z

∞z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 39: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)

(t , 8), (x , 14),���HHH(y , 5), (z, 7)

(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 40: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)

(t , 8), (x , 14),���HHH(y , 5), (z, 7)

(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 41: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)

(t , 8), (x , 14),���HHH(z, 7)

(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 42: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)

(t , 8), (x , 14),���HHH(z, 7)

(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 43: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)

(t , 8), (x , 14),���HHH(z, 7)

(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 44: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)

(t , 8), (x , 14),���HHH(z, 7)

(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

14

x

14

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 45: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)

(t , 8), (x , 13),���HHH(z, 7)

(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

13

x

13

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 46: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)

(t , 8), (x , 13),���HHH(z, 7)

���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

13

x

13

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 47: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)

���HHH(t , 8), (x , 13)

���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

13

x

13

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 48: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)

���HHH(t , 8), (x , 13)

���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

13

x

13

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 49: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)

���HHH(t , 8), (x , 13)

���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

13

x

13

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 50: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)

���HHH(t , 8), (x , 13)

���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

13

x

13

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 51: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)

���HHH(t , 8), (x , 9)

���HHH(t , 8), (x , 9)���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

9

x

9

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 52: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)

���HHH(t , 8), (x , 9)

���HHH(x , 9)���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

9

x

9

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 53: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)

���HHH(x , 9)

���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

9

x

9

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 54: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)

���HHH(x , 9)

���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

9

x

9

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 55: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)

���HHH(x , 9)

���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

9

x

9

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 56: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Complete Run of Dijkstra (Figure 24.6)

Priority Queue Q:

(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t ,∞), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y ,∞), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)���HHH(s, 0), (t , 10), (x ,∞), (y , 5), (z,∞)(t , 10), (x ,∞),��

�HHH(y , 5), (z,∞)(t , 8), (x ,∞),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z,∞)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(y , 5), (z, 7)(t , 8), (x , 14),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)(t , 8), (x , 13),���HHH(z, 7)���HHH(t , 8), (x , 13)���HHH(t , 8), (x , 9)���HHH(t , 8), (x , 9)��

�HHH(x , 9)

���HHH(x , 9)

0

s

0

s

5

y

5

y

8

t

8

t

9

x

9

x

7

z

7

z

0

s

5

y

7

z

8

t

9

x

1

2 3

9

2

647

10

5

6.4: Single-Source Shortest Paths T.S. 23

Page 57: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 58: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 59: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 60: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 61: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 62: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 63: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 64: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)

⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 65: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)

⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 66: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Runtime of Dijkstra’s Algorithm

DIJKSTRA(G,w,s)0: INITIALIZE(G,s)1: S = ∅2: Q = V3: while Q 6= ∅ do4: u = Extract-Min(Q)5: S = S ∪ {u}6: for each v ∈ G.Adj[u] do7: RELAX(u, v ,w)8: end for9: end while

Initialization (l. 0-2): O(V )

ExtractMin (l. 4): O(V · log V )

DecreaseKey (l. 7): O(E · 1)⇒ Overall: O(V log V + E)

Runtime (using Fibonacci Heaps)

6.4: Single-Source Shortest Paths T.S. 24

Page 67: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d

≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 68: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d

≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 69: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d

≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 70: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d

≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 71: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d

≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 72: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d ≤ y .d

= y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 73: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d ≤ y .d

= y .δ

u is extracted before y

since x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 74: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d ≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 75: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

u.δ <

u.d ≤ y .d = y .δ

u is extracted before y

since x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 76: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

⇒u.δ < u.d ≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 77: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

⇒u.δ < u.d ≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 78: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

⇒u.δ < u.d ≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 79: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Dijkstra’s Algorithm

For any directed graph G = (V ,E) with non-negative edge weights w :E → R+ and source s, Dijkstra terminates with u.d = u.δ for all u ∈ V .

Theorem 24.6

Proof: Invariant: If v is extracted, v .d = v .δ

Suppose there is u ∈ V , when extracted,

u.d > u.δ

Let u be the first vertex with this property

Take a shortest path from s to u and let(x , y) be the first edge from S to V \ S

⇒u.δ < u.d ≤ y .d = y .δ

u is extracted before ysince x .d = x .δ when x is extracted, and then(x , y) is relaxed ⇒ Convergence Property

There are edge cases likes = x and/or y = u!

This contradicts that y is on a shortest pathfrom s to u.

This step requires non-negative weights!

S

s

u

x y

6.4: Single-Source Shortest Paths T.S. 25

Page 80: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

6.5: All-Pairs Shortest PathsFrank Stajano Thomas Sauerwald

Lent 2015

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v 2 V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by ew(u, v) = w(u, v) + u.� � v .�2) Remove vertex s and its incident edges

3. For every vertex v 2 V , run Dijkstra on (G, E , ew)

Johnson’s Algorithm

3

8

�4 71

4

�5

2

6

s0

0

0

0

0

0

�1

�5

�4 0

4

13

0 100

0

0

2

2

�1

�4

Direct: 7, Detour: �1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V +E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 81: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Outline

Bellman-Ford

Dijkstra’s Algorithm

All-Pairs Shortest Path

APSP via Matrix Multiplication

Johnson’s Algorithm

6.5: All-Pairs Shortest Paths T.S. 2

Page 82: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Formalizing the Problem

Given: directed graph G = (V ,E), V = {1, 2, . . . , n}, with edgeweights represented by a matrix W :

wi,j =

weight of edge (i, j) for an edge (i, j) ∈ E ,∞ if there is no edge from i to j,0 if i = j.

Goal: Obtain a matrix of shortest path weights L, that is

`i,j =

{weight of a shortest path from i to j, if j is reachable from i∞ otherwise.

All-Pairs Shortest Path Problem

Here we will only compute the weight of the shortestpath without keeping track of the edges of the path!

6.5: All-Pairs Shortest Paths T.S. 3

Page 83: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Formalizing the Problem

Given: directed graph G = (V ,E), V = {1, 2, . . . , n}, with edgeweights represented by a matrix W :

wi,j =

weight of edge (i, j) for an edge (i, j) ∈ E ,∞ if there is no edge from i to j,0 if i = j.

Goal: Obtain a matrix of shortest path weights L, that is

`i,j =

{weight of a shortest path from i to j, if j is reachable from i∞ otherwise.

All-Pairs Shortest Path Problem

Here we will only compute the weight of the shortestpath without keeping track of the edges of the path!

6.5: All-Pairs Shortest Paths T.S. 3

Page 84: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Formalizing the Problem

Given: directed graph G = (V ,E), V = {1, 2, . . . , n}, with edgeweights represented by a matrix W :

wi,j =

weight of edge (i, j) for an edge (i, j) ∈ E ,∞ if there is no edge from i to j,0 if i = j.

Goal: Obtain a matrix of shortest path weights L, that is

`i,j =

{weight of a shortest path from i to j, if j is reachable from i∞ otherwise.

All-Pairs Shortest Path Problem

Here we will only compute the weight of the shortestpath without keeping track of the edges of the path!

6.5: All-Pairs Shortest Paths T.S. 3

Page 85: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Outline

Bellman-Ford

Dijkstra’s Algorithm

All-Pairs Shortest Path

APSP via Matrix Multiplication

Johnson’s Algorithm

6.5: All-Pairs Shortest Paths T.S. 4

Page 86: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j =

min(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)= min

1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 87: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j =

min(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)= min

1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 88: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j =

min(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)= min

1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 89: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j =

min(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)= min

1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 90: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j =

min(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)= min

1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 91: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j =

min(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)= min

1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 92: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j = min

(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)

= min1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 93: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j = min

(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)

= min1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 94: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

A Recursive Approach

i k j

Any shortest path from i to j of length k ≥ 2 is the concatenation ofa shortest path of length k − 1 and an edge

Basic Idea

Let `(m)i,j be min. weight of any path from i to j with at most m edges

Then `(1)i,j = wi,j , so L(1) = W

How can we obtain L(2) from L(1)?

`(2)i,j = min

(`(1)i,j , min

1≤k≤n`(1)i,k + wk,j

)

`(m)i,j = min

(`(m−1)i,j , min

1≤k≤n`(m−1)i,k + wk,j

)= min

1≤k≤n

(`(m−1)i,k + wk,j

)

Recall that wj,j = 0!

6.5: All-Pairs Shortest Paths T.S. 5

Page 95: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(2) =

0 3 8 −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 52 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 96: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(2) =

0 3 8 −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 52 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 97: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(2) =

0 3 8 ? −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 52 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 98: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(2) =

0 3 8 ? −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 52 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 99: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(2) =

0 3 8 2 −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 52 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 100: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(3) =

0 3 −3 2 −43 0 −4 1 −17 4 0 5 112 −1 −5 0 −28 5 1 6 0

L(2) =

0 3 8 2 −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 52 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 101: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(3) =

0 3 −3 2 −43 0 −4 1 −17 4 0 5 112 −1 −5 0 −28 5 1 6 0

L(2) =

0 3 8 2 −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 5 ?2 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 102: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(3) =

0 3 −3 2 −43 0 −4 1 −17 4 0 5 112 −1 −5 0 −28 5 1 6 0

L(2) =

0 3 8 2 −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 5 ?2 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 103: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Example of Shortest Path via Matrix Multiplication (Figure 25.1)

1

2

3

5 4

38

−4 71

4

−52

6

1

4

3

5

L(1) = W =

0 3 8 ∞ −4∞ 0 ∞ 1 7∞ 4 0 ∞ ∞2 ∞ −5 0 ∞∞ ∞ ∞ 6 0

L(3) =

0 3 −3 2 −43 0 −4 1 −17 4 0 5 112 −1 −5 0 −28 5 1 6 0

L(2) =

0 3 8 2 −43 0 −4 1 7∞ 4 0 5 112 −1 −5 0 −28 ∞ 1 6 0

L(4) =

0 1 −3 2 −43 0 −4 1 −17 4 0 5 32 −1 −5 0 −28 5 1 6 0

`(2)1,4 = min{0 +∞, 3 + 1, 8 +∞,∞+ 0, − 4 + 6}

`(4)3,5 = min{7 − 4, 4 + 7, 0 +∞, 5 +∞, 11 + 0}

6.5: All-Pairs Shortest Paths T.S. 6

Page 104: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)

Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 105: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 106: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 107: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 108: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 109: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×∞ ⇔ ?

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 110: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×∞ ⇔ 0

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 111: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×∞ ⇔ 0

0 ⇔ ?

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 112: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×∞ ⇔ 0

0 ⇔ 1

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 113: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(m)

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

L(n−1) = L(n) = L(n+1) = . . . = L, since every shortest path uses at mostn − 1 = |V | − 1 edges (assuming absence of negative-weight cycles)Computing L(m):

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

(L(m−1) ·W )i,j =∑

1≤k≤n

(`(m−1)i,k × wk,j

)

The correspondence is as follows:

min ⇔∑

+ ⇔ ×∞ ⇔ 0

0 ⇔ 1

L(m) can becomputed in O(n3)

6.5: All-Pairs Shortest Paths T.S. 7

Page 114: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(n−1) efficiently

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

For, say, n = 738, we subsequently compute

L(1), L(2), L(3), L(4), . . . , L(737) = L

Since we don’t need the intermediate matrices, a more efficient way is

L(1), L(2), L(4), . . . , L(512), L(1024) = L

Takes O(n · n3) = O(n4)

Takes O(log n · n3).We need L(4) = L(2) ·L(2) = L(3) ·L(1)! (see Ex. 25.1-4)

6.5: All-Pairs Shortest Paths T.S. 8

Page 115: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(n−1) efficiently

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

For, say, n = 738, we subsequently compute

L(1), L(2), L(3), L(4), . . . , L(737) = L

Since we don’t need the intermediate matrices, a more efficient way is

L(1), L(2), L(4), . . . , L(512), L(1024) = L

Takes O(n · n3) = O(n4)

Takes O(log n · n3).We need L(4) = L(2) ·L(2) = L(3) ·L(1)! (see Ex. 25.1-4)

6.5: All-Pairs Shortest Paths T.S. 8

Page 116: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(n−1) efficiently

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

For, say, n = 738, we subsequently compute

L(1), L(2), L(3), L(4), . . . , L(737) = L

Since we don’t need the intermediate matrices, a more efficient way is

L(1), L(2), L(4), . . . , L(512), L(1024) = L

Takes O(n · n3) = O(n4)

Takes O(log n · n3).We need L(4) = L(2) ·L(2) = L(3) ·L(1)! (see Ex. 25.1-4)

6.5: All-Pairs Shortest Paths T.S. 8

Page 117: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(n−1) efficiently

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

For, say, n = 738, we subsequently compute

L(1), L(2), L(3), L(4), . . . , L(737) = L

Since we don’t need the intermediate matrices, a more efficient way is

L(1), L(2), L(4), . . . , L(512), L(1024) = L

Takes O(n · n3) = O(n4)

Takes O(log n · n3).

We need L(4) = L(2) ·L(2) = L(3) ·L(1)! (see Ex. 25.1-4)

6.5: All-Pairs Shortest Paths T.S. 8

Page 118: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Computing L(n−1) efficiently

`(m)i,j = min

1≤k≤n

(`(m−1)i,k + wk,j

)

For, say, n = 738, we subsequently compute

L(1), L(2), L(3), L(4), . . . , L(737) = L

Since we don’t need the intermediate matrices, a more efficient way is

L(1), L(2), L(4), . . . , L(512), L(1024) = L

Takes O(n · n3) = O(n4)

Takes O(log n · n3).We need L(4) = L(2) ·L(2) = L(3) ·L(1)! (see Ex. 25.1-4)

6.5: All-Pairs Shortest Paths T.S. 8

Page 119: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Outline

Bellman-Ford

Dijkstra’s Algorithm

All-Pairs Shortest Path

APSP via Matrix Multiplication

Johnson’s Algorithm

6.5: All-Pairs Shortest Paths T.S. 9

Page 120: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstraafter Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negativeshortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 121: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstraafter Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negativeshortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 122: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstra

after Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negativeshortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 123: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstraafter Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negativeshortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 124: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstraafter Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negative

shortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 125: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstraafter Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negativeshortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 126: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstraafter Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negativeshortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 127: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Johnson’s Algorithm

allow negative-weight edges and negative-weight cycles

one pass of Bellman-Ford and |V | passes of Dijkstraafter Bellman-Ford, edges are reweighted s.t.

all edge weights are non-negativeshortest paths are maintained

Overview

Adding a constant to every edge doesn’t work!

3

8

−47

1

4

−5

2

6

1003

1008

9961007

1001

1004

995

1002

1006

6.5: All-Pairs Shortest Paths T.S. 10

Page 128: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 0

2. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 129: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 0

2. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 130: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 131: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 132: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abort

Otherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 133: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 134: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 135: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 136: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 137: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 138: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1

Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 139: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1

Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 140: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1

Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 141: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 142: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

How Johnson’s Algorithm works

1. Add a new vertex s and directed edges (s, v), v ∈ V , with weight 02. Run Bellman-Ford on this augmented graph with source s

If there are negative weight cycles, abortOtherwise:1) Reweight every edge (u, v) by w̃(u, v) = w(u, v) + u.δ − v .δ2) Remove vertex s and its incident edges

3. For every vertex v ∈ V , run Dijkstra on (G,E , w̃)

Johnson’s Algorithm

3

8

−4 71

4

−5

2

6

s0

0

0

0

0

0

−1

−5

−4 0

4

13

0 100

0

0

2

2

−1

−4

Direct: 7, Detour: −1 Direct: 10, Detour: 2

Runtime: O(V ·E+V ·(V log V+E))

6.5: All-Pairs Shortest Paths T.S. 11

Page 143: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 144: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 1.

Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 145: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 1. Let u.δ and v .δ be the distances from the fake source s

Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 146: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 1. Let u.δ and v .δ be the distances from the fake source s

u.δ + w(u, v) ≥ v .δ (triangle inequality)

⇒ w̃(u, v) + u.δ + w(u, v) ≥ w(u, v) + u.δ − v .δ + v .δ

⇒ w̃(u, v) ≥ 0

Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 147: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 1. Let u.δ and v .δ be the distances from the fake source s

u.δ + w(u, v) ≥ v .δ (triangle inequality)

⇒ w̃(u, v) + u.δ + w(u, v) ≥ w(u, v) + u.δ − v .δ + v .δ

⇒ w̃(u, v) ≥ 0

Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 148: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 1. Let u.δ and v .δ be the distances from the fake source s

u.δ + w(u, v) ≥ v .δ (triangle inequality)

⇒ w̃(u, v) + u.δ + w(u, v) ≥ w(u, v) + u.δ − v .δ + v .δ

⇒ w̃(u, v) ≥ 0

Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 149: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 2.

Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 150: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 2.Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 151: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 2.Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 152: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 2.Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi)

=k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 153: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 2.Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi) =k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ)

= w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 154: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Correctness of Johnson’s Algorithm

w̃(u, v) = w(u, v) + u.δ − v .δ

For any graph G = (V ,E ,w) without negative-weight cycles:

1. After reweighting, all edges are non-negative

2. Shortest Paths are preserved

Theorem

Proof of 2.Let p = (v0, v1, . . . , vk ) be any path

In the original graph, the weight is∑k

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

In the reweighted graph, the weight is

k∑

i=1

w̃(vi−1, vi) =k∑

i=1

(w(vi−1, vi) + vi−1.δ − vi .δ) = w(p) + v0.δ − vk .δ

6.5: All-Pairs Shortest Paths T.S. 12

Page 155: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Comparison of all Shortest-Path Algorithms

AlgorithmSSSP APSP negative

sparse dense sparse dense weights

Bellman-Ford V 2 V 3 V 3 V 4 X

Dijkstra V log V V 2 V 2 log V V 3 X

Matrix Mult. – – V 3 log V V 3 log V (X)

Johnson – – V 2 log V V 3 X

can handle negative weight edges,but not negative-weight cycles

6.5: All-Pairs Shortest Paths T.S. 13

Page 156: 6.4: Single-Source Shortest Paths · 6.4: Single-Source Shortest Paths T.S. 20. Outline Bellman-Ford Dijkstra’s Algorithm All-Pairs Shortest Path APSP via Matrix Multiplication

Comparison of all Shortest-Path Algorithms

AlgorithmSSSP APSP negative

sparse dense sparse dense weights

Bellman-Ford V 2 V 3 V 3 V 4 X

Dijkstra V log V V 2 V 2 log V V 3 X

Matrix Mult. – – V 3 log V V 3 log V (X)

Johnson – – V 2 log V V 3 X

can handle negative weight edges,but not negative-weight cycles

6.5: All-Pairs Shortest Paths T.S. 13