CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Shortest Paths Algorithm Design and Analysis Week 7 Bibliography: [CLRS] – chap 24 [CLRS] – chap 25.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
Chapter 25: All-Pairs Shortest-Paths
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Shortest Paths Definitions Single Source Algorithms
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Algorithms All pairs shortest path
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
All-Pairs Shortest Paths
Lecture 8 Shortest Path Shortest-path problems
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Shortest Paths1 C B A E D F
SINGLE-SOURCE SHORTEST PATHS
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
Lecture 16. Shortest Path Algorithms
Minimum weight spanning trees
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
All-Pairs Shortest Paths
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Algorithm Analysis Fall 2017 CS 4306/03
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
CS200: Algorithm Analysis
Analysis and design of algorithm
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Presentation transcript:

CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths

CS 473 All Pairs Shortest Paths2 All Pairs Shortest Paths (APSP) given : directed graph G = ( V, E ), weight function ω : E → R, |V| = n goal : create an n × n matrix D = ( d ij ) of shortest path distances i.e., d ij = δ ( v i, v j ) trivial solution : run a SSSP algorithm n times, one for each vertex as the source.

CS 473 All Pairs Shortest Paths3 All Pairs Shortest Paths (APSP) ► all edge weights are nonnegative : use Dijkstra’s algorithm –PQ = linear array : O ( V 3 + VE ) = O ( V 3 ) –PQ = binary heap : O ( V 2 lgV + EVlgV ) = O ( V 3 lgV ) for dense graphs better only for sparse graphs –PQ = fibonacci heap : O ( V 2 lgV + EV ) = O ( V 3 ) for dense graphs better only for sparse graphs ► negative edge weights : use Bellman-Ford algorithm –O ( V 2 E ) = O ( V 4 ) on dense graphs

CS 473 All Pairs Shortest Paths4 Adjacency Matrix Representation of Graphs ►n x n matrix W = (ω ij ) of edge weights : ω( v i, v j ) if ( v i, v j )  E ω ij = ∞ if ( v i, v j )  E ►assume ω ii = 0 for all v i  V, because –no neg-weight cycle  shortest path to itself has no edge, i.e., δ ( v i,v i ) = 0

CS 473 All Pairs Shortest Paths5 Dynamic Programming (1) Characterize the structure of an optimal solution. (2) Recursively define the value of an optimal solution. (3) Compute the value of an optimal solution in a bottom-up manner. (4) Construct an optimal solution from information constructed in (3).

CS 473 All Pairs Shortest Paths6 Shortest Paths and Matrix Multiplication Assumption : negative edge weights may be present, but no negative weight cycles. (1) Structure of a Shortest Path : Consider a shortest path p ij m from v i to v j such that |p ij m | ≤ m ► i.e., path p ij m has at most m edges. no negative-weight cycle  all shortest paths are simple  m is finite  m ≤ n – 1 i = j  |p ii | = 0 & ω( p ii ) = 0 i ≠ j  decompose path p ij m into p ik m-1 & v k → v j, where |p ik m-1 | ≤ m - 1 ► p ik m-1 should be a shortest path from v i to v k by optimal substructure property. ► Therefore, δ (v i,v j ) = δ (v i,v k ) + ω k j

CS 473 All Pairs Shortest Paths7 Shortest Paths and Matrix Multiplication (2) A Recursive Solution to All Pairs Shortest Paths Problem : d ij m = minimum weight of any path from v i to v j that contains at most “ m ” edges. m = 0 : There exist a shortest path from v i to v j with no edges ↔ i = j. 0 if i = j ► d ij 0 = ∞ if i ≠ j m ≥ 1 : d ij m = min { d ij m-1, min 1≤k≤n Λ k≠j { d ik m-1 + ω kj }} = min 1≤k≤n {d ik m-1 + ω kj } for all v k  V, since ω j j = 0 for all v j  V.

CS 473 All Pairs Shortest Paths8 Shortest Paths and Matrix Multiplication to consider all possible shortest paths with ≤ m edges from v i to v j ► consider shortest path with ≤ m -1 edges, from v i to v k, where v k  R v i and (v k,v j )  E note : δ (v i,v j ) = d ij n-1 = d ij n = d ij n+1, since m ≤ n -1 = | V | - 1 vivi vjvj v k ’s

CS 473 All Pairs Shortest Paths9 Shortest Paths and Matrix Multiplication (3) Computing the shortest-path weights bottom-up : given W = D 1, compute a series of matrices D 2, D 3,..., D n-1, where D m = ( d ij m ) for m = 1, 2,..., n-1 ► final matrix D n-1 contains actual shortest path weights, i.e., d ij n-1 = δ (v i,v j ) SLOW-APSP( W ) D 1 ← W for m ← 2 to n-1 do D m ← EXTEND( D m-1, W ) return D n-1

CS 473 All Pairs Shortest Paths10 Shortest Paths and Matrix Multiplication EXTEND ( D, W ) ► D = ( d ij ) is an n x n matrix for i ← 1 to n do for j ← 1 to n do d ij ← ∞ for k ← 1 to n do d ij ← min{d ij, d ik + ω k j } return D MATRIX-MULT ( A, B ) ► C = ( c ij ) is an n x n result matrix for i ←1 to n do for j ← 1 to n do c ij ← 0 for k ← 1 to n do c ij ← c ij + a ik x b k j return C

CS 473 All Pairs Shortest Paths11 Shortest Paths and Matrix Multiplication relation to matrix multiplication C = A B : c ij = ∑ 1≤k≤n a ik x b k j, ► D m-1 ↔ A & W ↔ B & D m ↔ C “min” ↔ “t” & “t” ↔ “x” & “∞” ↔ “0” Thus, we compute the sequence of matrix products D 1 = D 0 x W = W ; note D 0 = identity matrix, 0 if i = j D 2 = D 1 x W = W 2 i.e., d ij 0 = D 3 = D 2 x W = W 3 ∞ if i ≠ j D n-1 = D n-2 x W = W n-1 running time :  ( n 4 ) =  ( V 4 ) ► each matrix product :  ( n 3 ) ► number of matrix products : n-1

CS 473 All Pairs Shortest Paths12 Shortest Paths and Matrix Multiplication Example

CS 473 All Pairs Shortest Paths13 Shortest Paths and Matrix Multiplication ∞-4 2∞0∞17 3∞40∞∞ 42∞-50∞ 5∞∞∞60 D 1 = D 0 W

CS 473 All Pairs Shortest Paths14 Shortest Paths and Matrix Multiplication ∞ ∞160 D 2 = D 1 W

CS 473 All Pairs Shortest Paths15 Shortest Paths and Matrix Multiplication D 3 = D 2 W

CS 473 All Pairs Shortest Paths16 Shortest Paths and Matrix Multiplication D 4 = D 3 W

CS 473 All Pairs Shortest Paths17 SSSP and Matrix-Vector Multiplication relation of APSP to one step of matrix multiplication cjcj cjcj riri d ij m = riri k k D m ↔ C D m-1 ↔ A W ↔ B

CS 473 All Pairs Shortest Paths18 SSSP and Matrix-Vector Multiplication d ij n-1 at row r i and column c j of product matrix = δ ( v i =s, v j ) for j = 1, 2, 3,..., n row r i of the product matrix = solution to single-source shortest path problem for s = v i. ► r i of C = matrix B multiplied by r i of A  D i m = D i m-1 x W

CS 473 All Pairs Shortest Paths19 SSSP and Matrix-Vector Multiplication 0 if i = j let D i 0 = d 0, where d j 0 = ∞ otherwise we compute a sequence of n-1 “matrix-vector” products d i 1 = d i 0 x W d i 2 = d i 1 x W d i 3 = d i 2 x W : d i n-1 = d i n-2 x W

CS 473 All Pairs Shortest Paths20 SSSP and Matrix-Vector Multiplication this sequence of matrix-vector products ► same as Bellman-Ford algorithm. ► vector d i m  d values of Bellman-Ford algorithm after m-th relaxation pass. ► d i m ← d i m-1 x W  m-th relaxation pass over all edges.

CS 473 All Pairs Shortest Paths21 SSSP and Matrix-Vector Multiplication BELLMAN-FORD ( G, v i ) ► perform RELAX ( u, v ) for ► every edge ( u, v )  E for j ← 1 to n do for k ← 1 to n do RELAX ( v k, v j ) RELAX ( u, v ) d v = min { d v, d u + ω uv } EXTEND ( d i, W ) ► d i is an n-vector for j ← 1 to n do d j ← ∞ for k ← 1 to n do d j ← min { d j, d k + ω kj }

CS 473 All Pairs Shortest Paths22 idea : goal is not to compute all D m matrices ► we are interested only in matrix D n-1 recall : no negative-weight cycles  D m = D n-1 for all m ≥ n-1 we can compute D n-1 with only lg(n-1) matrix products as D 1 = W D 2 = W 2 = W x W D 4 = W 4 = W 2 x W 2 D 8 = W 8 = W 4 x W 4 = = This technique is called repeated squaring. Improving Running Time Through Repeated Squaring

CS 473 All Pairs Shortest Paths23 Improving Running Time Through Repeated Squaring FASTER-APSP ( W ) D 1 ← W m ← 1 while m < n-1 do D 2m ← EXTEND ( D m, D m ) m ← 2m return D m final iteration computes D 2m for some n-1 ≤ 2m ≤ 2n-2  D 2m = D n-1 running time :  ( n 3 lgn ) =  ( V 3 lgV ) ► each matrix product :  ( n 3 ) ► # of matrix products : lg( n-1 ) ► simple code, no complex data structures, small hidden constants in  -notation.

CS 473 All Pairs Shortest Paths24 Idea Behind Repeated Squaring decompose p ij 2m as p ik m & p kj m, where p ij 2m : v i v j p ik m : v i v k p kj m : v k v j vivi vjvj vk’svk’s

CS 473 All Pairs Shortest Paths25 Floyd-Warshall Algorithm assumption : negative-weight edges, but no negative-weight cycles (1) The Structure of a Shortest Path : Definition : intermediate vertex of a path p = ► any vertex of p other than v 1 or v k. p ij m : a shortest path from v i to v j with all intermediate vertices from V m = { v 1, v 2,..., v m } relationship between p ij m and p ij m-1 ► depends on whether v m is an intermediate vertex of p ij m - case 1: v m is not an intermediate vertex of p ij m  all intermediate vertices of p ij m are in V m -1  p ij m = p ij m-1

CS 473 All Pairs Shortest Paths26 Floyd-Warshall Algorithm - case 2 : v m is an intermediate vertex of p ij m - decompose path as v i v m v j  p 1 : v i v m & p 2 : v m v j - by opt. structure property both p 1 & p 2 are shortest paths. - v m is not an intermediate vertex of p 1 & p 2  p 1 = p im m-1 & p 2 = p mj m-1 vivi vjvj vmvm p1p1 p2p2 VmVm

CS 473 All Pairs Shortest Paths27 Floyd-Warshall Algorithm (2) A Recursive Solution to APSP Problem : d ij m = ω(p ij ) : weight of a shortest path from v i to v j with all intermediate vertices from V m = { v 1, v 2,..., v m }. note : d ij n = δ (v i,v j ) since V n = V ► i.e., all vertices are considered for being intermediate vertices of p ij n.

CS 473 All Pairs Shortest Paths28 Floyd-Warshall Algorithm compute d ij m in terms of d ij k with smaller k < m m = 0 : V 0 = empty set  path from v i to v j with no intermediate vertex. i.e., v i to v j paths with at most one edge  d ij 0 = ω i j m ≥ 1 : d ij m = min {d ij m-1, d im m-1 + d mj m-1 }

CS 473 All Pairs Shortest Paths29 Floyd-Warshall Algorithm (3) Computing Shortest Path Weights Bottom Up : FLOYD-WARSHALL( W ) ►D 0, D 1,..., D n are n x n matrices for m ← 1 to n do for i ← 1 to n do for j ← 1 to n do d ij m ← min {d ij m-1, d im m-1 + d mj m-1 } return D n

CS 473 All Pairs Shortest Paths30 Floyd-Warshall Algorithm FLOYD-WARSHALL ( W ) ► D is an n x n matrix D ← W for m ← 1 to n do for i ← 1 to n do for j ← 1 to n do if d ij > d im + d mj then d ij ← d im + d mj return D

CS 473 All Pairs Shortest Paths31 Floyd-Warshall Algorithm maintaining n D matrices can be avoided by dropping all superscripts. – m-th iteration of outermost for-loop begins with D = D m-1 ends with D = D m –computation of d ij m depends on d im m-1 and d mj m-1. no problem if d im & d mj are already updated to d im m & d mj m since d im m = d im m-1 & d mj m = d mj m-1. running time :  ( n 3 ) =  ( V 3 ) simple code, no complex data structures, small hidden constants

CS 473 All Pairs Shortest Paths32 Transitive Closure of a Directed Graph G ' = ( V, E ' ) : transitive closure of G = ( V, E ), where ► E ' = { (v i, v j ): there exists a path from v i to v j in G } trivial solution : assign W such that 1 if (v i, v j )  E ω ij = ∞ otherwise ► run Floyd-Warshall algorithm on W ► d ij n < n  there exists a path from v i to v j, i.e., (v i, v j )  E ' ► d ij n = ∞  no path from v i to v i, i.e., (v i, v j )  E ' ► running time :  ( n 3 ) =  ( V 3 )

CS 473 All Pairs Shortest Paths33 Transitive Closure of a Directed Graph Better  ( V 3 ) algorithm : saves time and space. 1 if i = j or (v i, v j )  E ► W = adjacency matrix : ω ij = 0 otherwise ► run Floyd-Warshall algorithm by replacing “min” → “ ” & “+” → “ ” 1 if a path from v i to v j with all intermediate vertices from V m define t ij m = 0 otherwise ► t ij n = 1  (v i, v j )  E ' & t ij n = 0  (v i, v j )  E ' recursive definition for t ij m = t ij m-1 (t im m-1 t mj m-1 ) with t ij 0 = ω ij

CS 473 All Pairs Shortest Paths34 Transitive Closure of a Directed Graph T-CLOSURE (G ) ► T = ( t ij ) is an n x n boolean matrix for i ← 1 to n do for j ← 1 to n do if i = j or ( v i, v j )  E then t ij ← 1 else t ij ← 0 for m ← 1 to n do for i ← 1 to n do for j ← 1 to n do t ij ← t ij ( t im t mj )

CS 473 All Pairs Shortest Paths35 Johnson’s Algorithm for Sparse Graphs (1) Preserving shortest paths by edge reweighting : L1 : given G = ( V, E ) with ω : E → R ► let h : V → R be any weighting function on the vertex set ► define ω( ω, h ) : E → R as ω( u, v ) = ω( u, v ) + h (u) – h (v) ► let p 0k = be a path from v 0 to v k (a) ω( p 0k ) = ω( p 0k ) + h (v 0 ) - h (v k ) (b) ω( p 0k ) = δ(v 0, v k ) in ( G, ω )  ω( p 0k ) = δ(v 0, v k ) in ( G, ω ) (c) ( G, ω ) has a neg-wgt cycle  ( G, ω ) has a neg-wgt cycle

CS 473 All Pairs Shortest Paths36 Johnson’s Algorithm for Sparse Graphs proof (a): ω( p 0k ) = ∑ 1 ≤i ≤k ω( v i-1,v i ) = ∑ 1 ≤i ≤k ( ω(v i-1,v i ) + h (v 0 ) - h (v k ) ) = ∑ 1 ≤i ≤k ω(v i-1,v i ) + ∑ 1 ≤i ≤k ( h (v 0 ) - h (v k ) ) = ω( p 0k ) + h (v 0 ) - h (v k ) proof (b): (  ) show ω( p 0k ) = δ ( v 0, v k )  ω( p 0k ) = δ ( v 0, v k ) by contradiction. ► Suppose that a shorter path p 0k ' from v 0 to v k in (G, ω ), then ω( p 0k ' ) < ω( p 0k ) due to (a) we have –ω( p 0k ' ) + h (v 0 ) - h (v k ) = ω( p 0k ' ) < ω( p 0k ) = ω( p 0k ) + h (v 0 ) - h (v k ) ω( p 0k ' ) + h (v 0 ) - h (v k ) < ω( p 0k ) + h (v 0 ) - h (v k ) ω( p 0k ' ) < ω( p 0k )  contradicts that p 0k is a shortest path in ( G, ω )

CS 473 All Pairs Shortest Paths37 Johnson’s Algorithm for Sparse Graphs proof (b): (<=) similar proof (c): (  ) consider a cycle c =. Due to (a) ► ω(c ) = ∑ 1 ≤i ≤k ω(v i-1,v i ) = ω(c ) + h (v 0 ) - h (v k ) = ω(c ) + h (v 0 ) - h (v 0 ) = ω(c ) since v k = v 0 ► ω(c ) = ω(c ). QED

CS 473 All Pairs Shortest Paths38 Johnson’s Algorithm for Sparse Graphs (2) Producing nonnegative edge weights by reweighting : given (G, ω) with G = ( V, E ) and ω : E → R construct a new graph ( G ', ω ' ) with G ' = ( V ', E ' ) and ω ' = E ' → R ► V ' = V U { s } for some new vertex s  V ► E ' = E U { ( s,v ) : v  V } ► ω ' (u,v) = ω(u,v) (u,v)  E and ω ' (s,v) = 0, v  V vertex s has no incoming edges  s  R v for any v in V ► no shortest paths from u ≠ s to v in G ' contains vertex s ► ( G ', ω ' ) has no neg-wgt cycle  (G, ω) has no neg-wgt cycle

CS 473 All Pairs Shortest Paths39 Johnson’s Algorithm for Sparse Graphs suppose that G and G ' have no neg-wgt cycle L2 : if we define h (v) = δ (s,v ) v  V in G ' and ω according to L1. ► we will have ω(u,v) = ω(u,v) + h(u) – h(v) ≥ 0 v  V proof : for every edge (u, v)  E δ (s,v ) ≤ δ (s, u) + ω(u, v) in G ' due to triangle inequality h (v) ≤ h (u) + ω(u, v)  0 ≤ ω(u, v) + h(u) – h(v) = ω(u, v)

CS 473 All Pairs Shortest Paths40 Johnson’s Algorithm for Sparse Graphs example : v1v1 v2v2 v3v3 v4v4 v5v5 v0 = sv0 = s  ( G ', ω ' )

CS 473 All Pairs Shortest Paths41 Johnson’s Algorithm for Sparse Graphs Edge Reweighting v1v1 v2v2 v3v3 v4v4 v5v5  (G ', ω ' ) with h(v)

CS 473 All Pairs Shortest Paths42 Johnson’s Algorithm for Sparse Graphs Edge Reweighting  (G, ω ) v1v1 v2v2 v3v3 v4v4 v5v5

CS 473 All Pairs Shortest Paths43 Johnson’s Algorithm for Sparse Graphs Computing All-Pairs Shortest Paths adjacency list representation of G. returns n x n matrix D = ( d ij ) where d ij = δ ij, or reports the existence of a neg-wgt cycle.

CS 473 All Pairs Shortest Paths44 Johnson’s Algorithm for Sparse Graphs JOHNSON(G,ω) ► D=(d ij ) is an nxn matrix ► construct ( G ' = (V ', E ' ), ω ' ) s.t. V ' = V U {s}; E ' = E U { (s,v) : v  V } ► ω ' (u,v) = ω(u,v), (u,v)  E & ω ' (s,v) = 0 v  V if BELLMAN-FORD(G ', ω ', s) = FALSE then return “negative-weight cycle” else for each vertex v  V ' - {s} = V do h[v] ← d ' [v] ► d ' [v] = δ ' (s,v) computed by BELLMAN-FORD(G ', ω ', s) for each edge (u,v)  E do ω(u,v) ← ω(u,v) + h[u] – h[v] ► edge reweighting for each vertex u  V do run DIJKSTRA(G, ω, u) to compute d[v] = δ (u,v) for all v in V  (G,ω) for each vertex v  V do d uv = d[v] – ( h[u] – h[v] ) return D

CS 473 All Pairs Shortest Paths45 Johnson’s Algorithm for Sparse Graphs running time : O ( V 2 lgV + EV ) ► edge reweighting BELLMAN-FORD(G ', ω ', s) : O ( EV ) computing ω values : O (E ) ► |V| runs of DIJKSTRA : | V | x O ( VlgV + EV ) = O ( V 2 lgV + EV ); PQ = fibonacci heap