Priority Queue Q: H (t,H 8), (x, 13) H t x 1 8 13 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 6.4: Single-Source Shortest Paths Frank Stajano Lent 2015 Thomas Sauerwald 20 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 The Bellman-Ford Algorithm BELLMAN-FORD(G,w,s) 0: assert(s in G.vertices()) 1: for v in G.vertices() 2: v.predecessor = None 3: v.d = Infinity 4: s.d = 0 5: 6: repeat |V|-1 times 7: for e in G.edges() 8: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d 9: if e.start.d + e.weight.d < e.end.d: 10: e.end.d = e.start.d + e.weight 11: e.end.predecessor = e.start 12: 13: for e in G.edges() 14: if e.start.d + e.weight.d < e.end.d: 15: return FALSE 16: return TRUE 6.4: Single-Source Shortest Paths T.S. 17 The Bellman-Ford Algorithm BELLMAN-FORD(G,w,s) 0: assert(s in G.vertices()) 1: for v in G.vertices() 2: v.predecessor = None 3: v.d = Infinity 4: s.d = 0 5: 6: repeat |V|-1 times 7: for e in G.edges() 8: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d 9: if e.start.d + e.weight.d < e.end.d: 10: e.end.d = e.start.d + e.weight 11: e.end.predecessor = e.start 12: 13: for e in G.edges() 14: if e.start.d + e.weight.d < e.end.d: 15: return FALSE 16: return TRUE Can we terminate earlier if there is a pass that keeps all .d variables? 6.4: Single-Source Shortest Paths T.S. 17 The Bellman-Ford Algorithm BELLMAN-FORD(G,w,s) 0: assert(s in G.vertices()) 1: for v in G.vertices() 2: v.predecessor = None 3: v.d = Infinity 4: s.d = 0 5: 6: repeat |V|-1 times 7: for e in G.edges() 8: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d 9: if e.start.d + e.weight.d < e.end.d: 10: e.end.d = e.start.d + e.weight 11: e.end.predecessor = e.start 12: 13: for e in G.edges() 14: if e.start.d + e.weight.d < e.end.d: 15: return FALSE 16: 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 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 = None 3: v.d = Infinity 4: s.d = 0 5: 6: repeat |V| times 7: flag = 0 8: for e in G.edges() 9: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d 10: if e.start.d + e.weight.d < e.end.d: 11: e.end.d = e.start.d + e.weight 12: e.end.predecessor = e.start 13: flag = 1 14: if flag = 0 return TRUE 15: 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 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 = None 3: v.d = Infinity 4: s.d = 0 5: 6: repeat |V| times 7: flag = 0 8: for e in G.edges() 9: Relax edge e=(u,v): Check if u.d + w(u,v) < v.d 10: if e.start.d + e.weight.d < e.end.d: 11: e.end.d = e.start.d + e.weight 12: e.end.predecessor = e.start 13: flag = 1 14: if flag = 0 return TRUE 15: 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 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 Dijkstra’s Algorithm Overview of Dijkstra Requires that all edges have non-negative weights Use a special order for relaxing edges 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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): 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 1 5 1 5 s 3 ∞ 0 3 0 2 6 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 1 5 1 5 s 3 ∞ 0 3 0 2 6 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 1 5 1 5 s 3 ∞ 0 3 0 2 6 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 1 5 1 5 s 3 ∞ 0 3 0 2 6 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 6 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 6 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 5 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 5 1 2 5 1 3 4 6.4: Single-Source Shortest Paths 3 ∞ 1 4 ∞ T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 5 1 2 5 1 3 3 ∞ 1 4 9 4 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 5 1 2 5 1 3 3 ∞ 1 4 9 4 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 5 1 2 5 1 3 3 ∞ 1 4 9 4 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 1 5 1 5 s 3 ∞ 0 3 0 2 5 1 2 5 1 3 3 ∞ 1 4 9 4 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v 6.4: Single-Source Shortest Paths T.S. 22 Dijkstra’s Algorithm Overview of Dijkstra 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.d 2. At each step, add a vertex v ∈ V \ S with minimal v .d (= v .δ) 3. Relax all edges leaving v DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) can actually skip edges which go to a vertex in S 1: S = ∅ 2: Q = V (cf. implementation of Dijksta in the handout) 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths T.S. 22 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: (s, 0), (t, ∞), (x, ∞), (y , ∞), (z, ∞) t ∞ 10 x 1 s ∞ 9 0 7 3 2 6 4 5 ∞ y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (s, 0), (t, ∞), (x, ∞), (y , ∞), (z, ∞) H H t ∞ 10 x 1 s ∞ 9 0 7 3 2 6 4 5 ∞ y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (s, 0), (t, ∞), (x, ∞), (y , ∞), (z, ∞) H H t ∞ 10 x 1 s ∞ 9 0 7 3 2 6 4 5 ∞ y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (s, 0), (t, 10), (x, ∞), (y , ∞), (z, ∞) H H t x 1 10 ∞ 10 s 9 0 7 3 2 6 4 5 ∞ y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (s, 0), (t, 10), (x, ∞), (y , ∞), (z, ∞) H H t x 1 10 ∞ 10 s 9 0 7 3 2 6 4 5 ∞ y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (s, 0), (t, 10), (x, ∞), (y , 5), (z, ∞) H H t x 1 10 ∞ 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (s, 0), (t, 10), (x, ∞), (y , 5), (z, ∞) H H t x 1 10 ∞ 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 10), (x, ∞), (y , 5), (z, ∞) H H t x 1 10 ∞ 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 10), (x, ∞), (y , 5), (z, ∞) H H t x 1 10 ∞ 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, ∞), (y , 5), (z, ∞) H H t x 1 8 ∞ 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, ∞), (y , 5), (z, ∞) H H t x 1 8 ∞ 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 14), (y , 5), (z, ∞) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 14), (y , 5), (z, ∞) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 2 ∞ z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 14), (y , 5), (z, 7) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 14), (y , 5), (z, 7) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: (t, 8), (x, 14), H (z, 7) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: (t, 8), (x, 14), H (z, 7) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: (t, 8), (x, 14), H (z, 7) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: (t, 8), (x, 14), H (z, 7) H H t x 1 8 14 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: (t, 8), (x, 13), H (z, 7) H H t x 1 8 13 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: (t, 8), (x, 13), H (z, 7) H H t x 1 8 13 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 13) H H t x 1 8 13 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 13) H H t x 1 8 13 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 13) H H t x 1 8 13 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 13) H H t x 1 8 13 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 9) H H t x 1 8 9 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (t, 8), (x, 9) H H t x 1 8 9 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (x, 9) H H t x 1 8 9 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (x, 9) H H t x 1 8 9 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (x, 9) H H t x 1 8 9 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Complete Run of Dijkstra (Figure 24.6) Priority Queue Q: H (x, 9) H H t x 1 8 9 10 s 9 0 7 3 2 6 4 5 5 y 6.4: Single-Source Shortest Paths 7 2 z T.S. 23 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) Initialization (l. 0-2): O(V ) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) Initialization (l. 0-2): O(V ) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) Initialization (l. 0-2): O(V ) ExtractMin (l. 4): O(V · log V ) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) Initialization (l. 0-2): O(V ) ExtractMin (l. 4): O(V · log V ) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) Initialization (l. 0-2): O(V ) ExtractMin (l. 4): O(V · log V ) DecreaseKey (l. 7): O(E · 1) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) Initialization (l. 0-2): O(V ) ExtractMin (l. 4): O(V · log V ) DecreaseKey (l. 7): O(E · 1) T.S. 24 Runtime of Dijkstra’s Algorithm DIJKSTRA(G,w,s) 0: INITIALIZE(G,s) 1: S = ∅ 2: Q = V 3: while Q 6= ∅ do 4: u = Extract-Min(Q) 5: S = S ∪ {u} 6: for each v ∈ G.Adj[u] do 7: RELAX(u, v , w) 8: end for 9: end while 6.4: Single-Source Shortest Paths Runtime (using Fibonacci Heaps) 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) T.S. 24 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s Let u be the first vertex with this property S 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 6.4: Single-Source Shortest Paths x y S T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.d 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.d ≤ y .d 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.d ≤ y .d u is extracted before y 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.d ≤ y .d = y .δ 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.d ≤ y .d = y .δ since x.d = x.δ when x is extracted, and then (x, y ) is relaxed ⇒ Convergence Property 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.δ < u.d ≤ y .d = y .δ 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.δ < u.d ≤ y .d = y .δ This contradicts that y is on a shortest path from s to u. 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.δ < u.d ≤ y .d = y .δ This contradicts that y is on a shortest path from s to u. This step requires non-negative weights! 6.4: Single-Source Shortest Paths T.S. 25 Correctness of Dijkstra’s Algorithm Theorem 24.6 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 . Proof: Invariant: If v is extracted, v .d = v .δ There are edge cases like s = x and/or y = u! Suppose there is u ∈ V , when extracted, u.d > u.δ u s 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 x y S u.δ < u.d ≤ y .d = y .δ This contradicts that y is on a shortest path from s to u. 6.4: Single-Source Shortest Paths T.S. 25 2) Remove vertex s and its incident edges e 3. For every vertex v 2 V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 1 3 2 4 7 4 0 1 6 6.5: All-Pairs Shortest Paths 0 4 13 5 2 5 0 0 0 10 2 T.S. 6.5: All-Pairs Shortest Paths Frank Stajano Lent 2015 0 Thomas Sauerwald 11 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 Formalizing the Problem All-Pairs Shortest Path Problem Given: directed graph G = (V , E), V = {1, 2, . . . , n}, with edge weights represented by a matrix W : weight of edge (i, j) for an edge (i, j) ∈ E, wi,j = ∞ if there is no edge from i to j, 0 if i = j. 6.5: All-Pairs Shortest Paths T.S. 3 Formalizing the Problem All-Pairs Shortest Path Problem Given: directed graph G = (V , E), V = {1, 2, . . . , n}, with edge weights represented by a matrix W : weight of edge (i, j) for an edge (i, j) ∈ E, wi,j = ∞ if there is no edge from i to j, 0 if i = j. Goal: Obtain a matrix of shortest path weights L, that is ( weight of a shortest path from i to j, if j is reachable from i `i,j = ∞ otherwise. 6.5: All-Pairs Shortest Paths T.S. 3 Formalizing the Problem All-Pairs Shortest Path Problem Given: directed graph G = (V , E), V = {1, 2, . . . , n}, with edge weights represented by a matrix W : weight of edge (i, j) for an edge (i, j) ∈ E, wi,j = ∞ if there is no edge from i to j, 0 if i = j. Goal: Obtain a matrix of shortest path weights L, that is ( weight of a shortest path from i to j, if j is reachable from i `i,j = ∞ otherwise. Here we will only compute the weight of the shortest path without keeping track of the edges of the path! 6.5: All-Pairs Shortest Paths T.S. 3 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 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges (1) Then `i,j = wi,j , so L(1) = W 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges (1) Then `i,j = wi,j , so L(1) = W How can we obtain L(2) from L(1) ? 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges (1) Then `i,j = wi,j , so L(1) = W How can we obtain L(2) from L(1) ? (2) (1) (1) `i,j = min `i,j , min `i,k + wk ,j 1≤k ≤n 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges (1) Then `i,j = wi,j , so L(1) = W How can we obtain L(2) from L(1) ? (2) (1) (1) `i,j = min `i,j , min `i,k + wk ,j 1≤k ≤n (m) `i,j = 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges (1) Then `i,j = wi,j , so L(1) = W How can we obtain L(2) from L(1) ? (2) (1) (1) `i,j = min `i,j , min `i,k + wk ,j 1≤k ≤n (m) (m−1) (m−1) `i,j = min `i,j , min `i,k + wk ,j 1≤k ≤n 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i j k Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges (1) Then `i,j = wi,j , so L(1) = W How can we obtain L(2) from L(1) ? (2) (1) (1) `i,j = min `i,j , min `i,k + wk ,j 1≤k ≤n Recall that wj,j = 0! (m) (m−1) (m−1) `i,j = min `i,j , min `i,k + wk ,j 1≤k ≤n 6.5: All-Pairs Shortest Paths T.S. 5 A Recursive Approach i k j Basic Idea Any shortest path from i to j of length k ≥ 2 is the concatenation of a shortest path of length k − 1 and an edge (m) Let `i,j be min. weight of any path from i to j with at most m edges (1) Then `i,j = wi,j , so L(1) = W How can we obtain L(2) from L(1) ? (2) (1) (1) `i,j = min `i,j , min `i,k + wk ,j 1≤k ≤n Recall that wj,j = 0! (m) (m−1) (m−1) (m−1) `i,j = min `i,j , min `i,k + wk ,j = min `i,k + wk ,j 1≤k ≤n 6.5: All-Pairs Shortest Paths 1≤k ≤n T.S. 5 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 2 6 4 8 3 1 4 −5 6.5: All-Pairs Shortest Paths T.S. 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 L(1) = W = 2 4 8 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 3 1 8 ∞ 0 −5 ∞ −5 ∞ 1 ∞ 0 6 6.5: All-Pairs Shortest Paths −4 7 ∞ ∞ 0 T.S. 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 L(1) = W = 2 4 8 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 3 1 8 ∞ 0 −5 ∞ −5 ∞ 1 ∞ 0 6 6.5: All-Pairs Shortest Paths −4 7 ∞ ∞ 0 L(2) = T.S. 0 3 ∞ 2 8 3 0 4 −1 ∞ 8 −4 0 −5 1 ? 1 5 0 6 −4 7 11 −2 0 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 L(1) = W = 2 4 8 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 3 1 8 ∞ 0 −5 ∞ −5 ∞ 1 ∞ 0 6 6.5: All-Pairs Shortest Paths −4 7 ∞ ∞ 0 (2) `1,4 = min{0 + ∞, 3 + 1, 8 + ∞, ∞ + 0, − 4 + 6} L(2) = T.S. 0 3 ∞ 2 8 3 0 4 −1 ∞ 8 −4 0 −5 1 ? 1 5 0 6 −4 7 11 −2 0 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 L(1) = W = 2 4 8 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 3 1 8 ∞ 0 −5 ∞ −5 ∞ 1 ∞ 0 6 6.5: All-Pairs Shortest Paths −4 7 ∞ ∞ 0 (2) `1,4 = min{0 + ∞, 3 + 1, 8 + ∞, ∞ + 0, − 4 + 6} L(2) = T.S. 0 3 ∞ 2 8 3 0 4 −1 ∞ 8 −4 0 −5 1 2 1 5 0 6 −4 7 11 −2 0 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 2 4 8 3 1 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 8 ∞ 0 −5 ∞ ∞ 1 ∞ 0 6 0 3 7 2 8 3 0 4 −1 5 −3 −4 0 −5 1 2 1 5 0 6 L(1) = W = L(3) = −5 −4 7 ∞ ∞ 0 −4 −1 11 −2 0 6.5: All-Pairs Shortest Paths (2) `1,4 = min{0 + ∞, 3 + 1, 8 + ∞, ∞ + 0, − 4 + 6} L(2) = 0 3 ∞ 2 8 3 0 4 −1 ∞ 8 −4 0 −5 1 2 1 5 0 6 −4 7 11 −2 0 T.S. 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 2 4 8 3 1 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 8 ∞ 0 −5 ∞ ∞ 1 ∞ 0 6 0 3 7 2 8 3 0 4 −1 5 −3 −4 0 −5 1 2 1 5 0 6 L(1) = W = L(3) = −5 −4 7 ∞ ∞ 0 −4 −1 11 −2 0 6.5: All-Pairs Shortest Paths (2) `1,4 = min{0 + ∞, 3 + 1, 8 + ∞, ∞ + 0, − 4 + 6} 0 3 ∞ 2 8 3 0 4 −1 ∞ 8 −4 0 −5 1 0 3 = 7 2 8 1 0 4 −1 5 −3 −4 0 −5 1 L(2) = L(4) T.S. 2 1 5 0 6 2 1 5 0 6 −4 7 11 −2 0 −4 −1 ? −2 0 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 2 4 8 3 1 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 8 ∞ 0 −5 ∞ ∞ 1 ∞ 0 6 0 3 7 2 8 3 0 4 −1 5 −3 −4 0 −5 1 2 1 5 0 6 L(1) = W = L(3) = −5 −4 7 ∞ ∞ 0 −4 −1 11 −2 0 (2) `1,4 = min{0 + ∞, 3 + 1, 8 + ∞, ∞ + 0, − 4 + 6} 0 3 ∞ 2 8 3 0 4 −1 ∞ 8 −4 0 −5 1 0 3 = 7 2 8 1 0 4 −1 5 −3 −4 0 −5 1 L(2) = L(4) 2 1 5 0 6 2 1 5 0 6 −4 7 11 −2 0 −4 −1 ? −2 0 (4) `3,5 = min{7 − 4, 4 + 7, 0 + ∞, 5 + ∞, 11 + 0} 6.5: All-Pairs Shortest Paths T.S. 6 Example of Shortest Path via Matrix Multiplication (Figure 25.1) 2 3 1 −4 7 5 2 4 8 3 1 4 6 0 ∞ ∞ 2 ∞ 3 0 4 ∞ ∞ 8 ∞ 0 −5 ∞ ∞ 1 ∞ 0 6 0 3 7 2 8 3 0 4 −1 5 −3 −4 0 −5 1 2 1 5 0 6 L(1) = W = L(3) = −5 −4 7 ∞ ∞ 0 −4 −1 11 −2 0 (2) `1,4 = min{0 + ∞, 3 + 1, 8 + ∞, ∞ + 0, − 4 + 6} 0 3 ∞ 2 8 3 0 4 −1 ∞ 8 −4 0 −5 1 0 3 = 7 2 8 1 0 4 −1 5 −3 −4 0 −5 1 L(2) = L(4) 2 1 5 0 6 2 1 5 0 6 −4 7 11 −2 0 −4 −1 3 −2 0 (4) `3,5 = min{7 − 4, 4 + 7, 0 + ∞, 5 + ∞, 11 + 0} 6.5: All-Pairs Shortest Paths T.S. 6 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : (m) (m−1) `i,j = min `i,k + wk ,j 1≤k ≤n 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : (m) (m−1) `i,j = min `i,k + wk ,j 1≤k ≤n X (m−1) (m−1) (L · W )i,j = `i,k × wk ,j 1≤k ≤n 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : (m) (m−1) `i,j = min `i,k + wk ,j 1≤k ≤n X (m−1) (m−1) (L · W )i,j = `i,k × wk ,j 1≤k ≤n The correspondence is as follows: min ⇔ X + ⇔ × 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : (m) (m−1) `i,j = min `i,k + wk ,j 1≤k ≤n X (m−1) (m−1) (L · W )i,j = `i,k × wk ,j 1≤k ≤n The correspondence is as follows: min ⇔ X + ⇔ × ∞ ⇔ ? 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : (m) (m−1) `i,j = min `i,k + wk ,j 1≤k ≤n X (m−1) (m−1) (L · W )i,j = `i,k × wk ,j 1≤k ≤n The correspondence is as follows: min ⇔ X + ⇔ × ∞ ⇔ 0 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : (m) (m−1) `i,j = min `i,k + wk ,j 1≤k ≤n X (m−1) (m−1) (L · W )i,j = `i,k × wk ,j 1≤k ≤n The correspondence is as follows: min ⇔ X + ⇔ × ∞ ⇔ 0 0 ⇔ ? 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : (m) (m−1) `i,j = min `i,k + wk ,j 1≤k ≤n X (m−1) (m−1) (L · W )i,j = `i,k × wk ,j 1≤k ≤n The correspondence is as follows: min ⇔ X + ⇔ × ∞ ⇔ 0 0 ⇔ 1 6.5: All-Pairs Shortest Paths T.S. 7 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 most n − 1 = |V | − 1 edges (assuming absence of negative-weight cycles) Computing L(m) : L(m) can be (m) (m−1) `i,j = min `i,k + wk ,j computed in O(n3 ) 1≤k ≤n X (m−1) (L(m−1) · W )i,j = `i,k × wk ,j 1≤k ≤n The correspondence is as follows: min ⇔ X + ⇔ × ∞ ⇔ 0 0 ⇔ 1 6.5: All-Pairs Shortest Paths T.S. 7 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 6.5: All-Pairs Shortest Paths T.S. 8 Computing L(n−1) efficiently (m) `i,j = min 1≤k ≤n (m−1) `i,k + wk ,j Takes O(n · n3 ) = O(n4 ) For, say, n = 738, we subsequently compute L(1) , L(2) , L(3) , L(4) , . . . , L(737) = L 6.5: All-Pairs Shortest Paths T.S. 8 Computing L(n−1) efficiently (m) `i,j = min 1≤k ≤n (m−1) `i,k + wk ,j Takes O(n · n3 ) = O(n4 ) 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 6.5: All-Pairs Shortest Paths T.S. 8 Computing L(n−1) efficiently (m) `i,j = min 1≤k ≤n (m−1) `i,k + wk ,j Takes O(n · n3 ) = O(n4 ) 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(log n · n3 ). 6.5: All-Pairs Shortest Paths T.S. 8 Computing L(n−1) efficiently (m) `i,j = min 1≤k ≤n (m−1) `i,k + wk ,j Takes O(n · n3 ) = O(n4 ) 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(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 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 Johnson’s Algorithm Overview 6.5: All-Pairs Shortest Paths T.S. 10 Johnson’s Algorithm Overview allow negative-weight edges and negative-weight cycles 6.5: All-Pairs Shortest Paths T.S. 10 Johnson’s Algorithm Overview allow negative-weight edges and negative-weight cycles one pass of Bellman-Ford and |V | passes of Dijkstra 6.5: All-Pairs Shortest Paths T.S. 10 Johnson’s Algorithm Overview 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. 6.5: All-Pairs Shortest Paths T.S. 10 Johnson’s Algorithm Overview 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-negative 6.5: All-Pairs Shortest Paths T.S. 10 Johnson’s Algorithm Overview 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-negative shortest paths are maintained 6.5: All-Pairs Shortest Paths T.S. 10 Johnson’s Algorithm Overview 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-negative shortest paths are maintained Adding a constant to every edge doesn’t work! 6.5: All-Pairs Shortest Paths T.S. 10 Johnson’s Algorithm Overview 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-negative shortest paths are maintained Adding a constant to every edge doesn’t work! 3 4 1004 1003 8 1008 2 −4 1002 1 7 −5 1001 995 996 1007 6 6.5: All-Pairs Shortest Paths 1006 T.S. 10 How Johnson’s Algorithm works Johnson’s Algorithm 1. Add a new vertex s and directed edges (s, v ), v ∈ V , with weight 0 3 4 8 2 −4 1 7 −5 6 6.5: All-Pairs Shortest Paths T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 1. Add a new vertex s and directed edges (s, v ), v ∈ V , with weight 0 0 0 s 3 4 8 0 0 2 −4 0 1 7 −5 6 6.5: All-Pairs Shortest Paths T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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 0 0 s 3 4 8 0 0 2 −4 0 1 7 −5 6 6.5: All-Pairs Shortest Paths T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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 0 0 s 0 4 8 0 0 −1 3 2 −4 0 1 7 −4 6 6.5: All-Pairs Shortest Paths −5 −5 0 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort 0 0 s 0 4 8 0 0 −1 3 2 −4 0 1 7 −4 6 6.5: All-Pairs Shortest Paths −5 −5 0 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges 0 0 s 0 4 8 0 0 −1 3 2 −4 0 1 7 −4 6 6.5: All-Pairs Shortest Paths −5 −5 0 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges 0 0 s 0 4 8 0 0 −1 3 2 −4 0 1 7 −4 6 6.5: All-Pairs Shortest Paths 0 4 13 −5 2 0 −5 0 0 10 0 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 −1 3 2 −4 0 1 7 −4 6 6.5: All-Pairs Shortest Paths 0 4 13 −5 2 0 −5 0 0 10 0 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 −1 3 2 −4 0 1 7 −4 6 6.5: All-Pairs Shortest Paths 0 4 13 −5 2 0 −5 0 0 10 0 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 −1 3 2 −4 0 1 7 −4 6 6.5: All-Pairs Shortest Paths 0 4 13 −5 2 0 −5 0 0 10 0 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 −1 3 2 −4 7 −4 6 0 Direct: 7, Detour: −1 6.5: All-Pairs Shortest Paths 1 0 4 13 −5 2 0 −5 0 0 10 0 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 −1 3 2 −4 7 −4 6 0 Direct: 7, Detour: −1 6.5: All-Pairs Shortest Paths 1 0 4 13 −5 2 0 −5 0 0 10 0 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 −1 3 2 −4 7 −4 6 0 Direct: 7, Detour: −1 6.5: All-Pairs Shortest Paths 1 0 4 13 −5 2 0 −5 0 0 10 0 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) 0 0 s 0 4 8 0 0 −1 3 2 −4 7 −4 6 0 Direct: 7, Detour: −1 6.5: All-Pairs Shortest Paths 1 0 4 13 −5 2 0 −5 0 0 10 0 2 Direct: 10, Detour: 2 T.S. 11 How Johnson’s Algorithm works Johnson’s Algorithm 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, abort Otherwise: e 1) Reweight every edge (u, v ) by w(u, v ) = w(u, v ) + u.δ − v .δ 2) Remove vertex s and its incident edges e 3. For every vertex v ∈ V , run Dijkstra on (G, E, w) Runtime: O(V ·E +V ·(V log V +E)) 0 0 s 0 4 8 0 0 −1 3 2 −4 7 −4 6 0 Direct: 7, Detour: −1 6.5: All-Pairs Shortest Paths 1 0 4 13 −5 2 0 −5 0 0 10 0 2 Direct: 10, Detour: 2 T.S. 11 Correctness of Johnson’s Algorithm Theorem For any graph G = (V , E, w) without negative-weight cycles: 1. After reweighting, all edges are non-negative 2. Shortest Paths are preserved 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem For any graph G = (V , E, w) without negative-weight cycles: 1. After reweighting, all edges are non-negative 2. Shortest Paths are preserved Proof of 1. 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 1. Let u.δ and v .δ be the distances from the fake source s 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 1. Let u.δ and v .δ be the distances from the fake source s u.δ + w(u, v ) ≥ v .δ 6.5: All-Pairs Shortest Paths (triangle inequality) T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 1. ⇒ Let u.δ and v .δ be the distances from the fake source s u.δ + w(u, v ) ≥ v .δ (triangle inequality) e w(u, v ) + u.δ + w(u, v ) ≥ w(u, v ) + u.δ − v .δ + v .δ 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 1. ⇒ Let u.δ and v .δ be the distances from the fake source s u.δ + w(u, v ) ≥ v .δ (triangle inequality) e w(u, v ) + u.δ + w(u, v ) ≥ w(u, v ) + u.δ − v .δ + v .δ e ⇒ w(u, v) ≥ 0 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 2. 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 2. Let p = (v0 , v1 , . . . , vk ) be any path 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 2. Let p = (v0 , v1 , . . . , vk ) be any path In the original graph, the weight is 6.5: All-Pairs Shortest Paths Pk i=1 w(vi−1 , vi ) = w(p). T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 2. Let p = (v0 , v1 , . . . , vk ) be any path In the original graph, the weight is Pk i=1 w(vi−1 , vi ) = w(p). In the reweighted graph, the weight is k X i=1 e i−1 , vi ) w(v 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 2. Let p = (v0 , v1 , . . . , vk ) be any path In the original graph, the weight is Pk i=1 w(vi−1 , vi ) = w(p). In the reweighted graph, the weight is k X i=1 e i−1 , vi ) = w(v k X i=1 (w(vi−1 , vi ) + vi−1 .δ − vi .δ) 6.5: All-Pairs Shortest Paths T.S. 12 Correctness of Johnson’s Algorithm Theorem e 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 Proof of 2. Let p = (v0 , v1 , . . . , vk ) be any path In the original graph, the weight is Pk i=1 w(vi−1 , vi ) = w(p). In the reweighted graph, the weight is k X i=1 e i−1 , vi ) = w(v k X i=1 (w(vi−1 , vi ) + vi−1 .δ − vi .δ) = w(p) + v0 .δ − vk .δ 6.5: All-Pairs Shortest Paths T.S. 12 Comparison of all Shortest-Path Algorithms SSSP Algorithm sparse 2 APSP dense dense X Dijkstra V log V V2 V 2 log V V3 X Matrix Mult. – – V 3 log V V 3 log V (X) 6.5: All-Pairs Shortest Paths – V weights 4 V – V 3 Bellman-Ford Johnson V sparse 3 negative 2 V log V T.S. V 3 X 13 Comparison of all Shortest-Path Algorithms SSSP Algorithm sparse 2 APSP dense dense X Dijkstra V log V V2 V 2 log V V3 X Matrix Mult. – – V 3 log V V 3 log V (X) – V weights 4 V – V 3 Bellman-Ford Johnson V sparse 3 negative 2 V log V V 3 X can handle negative weight edges, but not negative-weight cycles 6.5: All-Pairs Shortest Paths T.S. 13
© Copyright 2025 Paperzz