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.

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.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
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.
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CS420 lecture twelve Shortest Paths wim bohm cs csu.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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
All-Pairs Shortest Paths
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
1 Dynamic programming algorithms for all-pairs shortest path and longest common subsequences We will study a new technique—dynamic programming algorithms.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem.
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.
Introduction to Algorithms Jiafen Liu Sept
Shortest Path Graph Theory Basics Anil Kishore.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
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
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.
IS 2610: Data Structures Graph April 12, Graph Weighted graph – call it networks Shortest path between nodes s and t in a network  Directed simple.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
CS 583 Analysis of Algorithms
Floyd-Warshall Algorithm
Advanced Algorithms Analysis and Design
All pairs shortest path problem
Algorithms (2IL15) – Lecture 7
Negative-Weight edges:
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

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 to vertex j with at most m edges. Decompose p as : i  k  j p’ has at most m-1 edges.  (i, j) =  (i, k) + w kj A recursive sol. to the all-pairs shortest-paths problem : minimum weight of any path from vertex i to vertex j that contains at most m edges. P’

p3. Computing the shortest-path weights bottom up: W=(w ij ) = w ij Let D (m) = ( ) Given D (m-1) and W, return D (m)

p4. All-pairs Shortest Paths: Input : G = (V,E), |V|= n W = (w ij ), Output : n  n matrix D = (d ij ) d i,j : the weight of a shortest path from vertex i to vertex j d i,j =  (i, j) at termination (Let  (i, j) denote the shortest-path weight from vertex i to vertex j )  = (  ij ) : predecessor matrix No negative-weight cycle Adjacency-matrix i j  ij

p5. Print-All-Pairs-Shortest-Path( , i, j) { if i=j then print I else if  ij = NIL then print “no path from” i “to” j “exists” else Print-All-Pairs-Shortest-Path( , i,  ij ) print j } Dynamic Programming: Characterize the structure of an optimal sol. Recursively define the value of an optimal sol. Compute the value of an optimal sol in a bottom-up fashion

p6. Extend-Shortest-Paths(D,W) { n  rows[D] Let D’ = (d ij ’) be an n  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 + w kj ) return D’ } Slow-All-Pairs-Shortest-Paths(W) { n = rows[W] D (1) = W for m=2 to n-1 do D (m) = Extend-Shortest-Paths(D (m-1), W) return D (n-1) } Very similar to matrix-multiplication  (n 4 )

p7. Faster-All-Pairs-Shortest-Paths(W) {n = rows[W] D (1) = W m = 1 While n-1>m do D (2m) = Extend-Shortest-Paths(D (m), D (m) ) m = 2m return D (m) }  (n 3 logn) (Repeated squaring)

p8. Floyd-Warshall algorithm Structure of a shortest path: Intermediate vertex of a simple path p= is any vertex of p other than v 1 or v, that is, any vertex in the set {v 2, …, v -1 } Let V={1, …, n} and a subset {1, 2, …, k} for some k For any pair of vertices i, j  V, consider all paths from i to j whose intermediate vertices are all drawn from {1, 2, …, k} and let p be a minimum-weight path from among them

p9.  If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set {1, 2, …, k-1}. Thus a shortest path from vertex i to vertex j with all intermediate vertices in the set {1, 2, …, k-1} is also a shortest path from i to j with all intermediate vertices in the set {1, 2, …, k}  all int. vertices in {1, 2, …, k-1} i k j p1p1 p2p2 p : all int. vertices in {1, 2, …, k} k : intermediate vertex of p

p10. A recursive solution to the all-pairs shortest-paths problem d ij (k) : the weight of a shortest path from i to j with all intermediate vertices in the set {1, 2, …, k} Computing the shortest-path weights bottom up Floyd-Warshall(W) {n = rows[W] D(0) = W for k=1 to n do for i=1 to n do for j=1 to n do return D (n) }  (n 3 )

p11. Constructing a shortest path:  ij (k) : the predecessor os vertex j on a shortest path from i with all intermediate vertices in the set {1, 2, …, k}

p12. Transitive closure of a directed graph G=(V,E): G*=(V, E*), E*={ (i, j): there is a path from i to j in G} For i, j, k = 1, 2, …, n, define t ij (k) =1, if  a path in G from i to j with all intermediate vertices in {1, 2, …, k}; otherwise t ij (k) =0

p13. TC(G) {n = | V[G] |; for i=1 to n do for j=1 to n do if i=j or (i, j)  E[G] then t ij (0) = 1 else t ij (0) = 0 for k=1 to n do for i=1 to n do for j=1 to n do return T (n) }

p14. Preserving shortest paths by reweighting:, which must satisfy 2 properties: For all pairs of vertices u, v  V, a shortest path from u to v using weight function w is also a shortest path from u to v using weight function For all edges (u, v), is non-negative

p15. Lemma: Given G=(V, E) weighted, directed graph with weight function w: E  R, let h: V  R be any function mapping vertices to real numbers. For each (u,v)  E, define Let p=  Then  Also G has a negative cycle with w iff G has a negative cycle with pf:

p16.  Suppose there is a shorter path p’ from v 0 to v k with Thus  Consider any cycle c=, where v 0 = v k Thus c has negative weight using w iff it has negative weight using Producing non-negative weights by reweighting: Given G=(V,E) with weight function w: E  R, make G’=(V’, E’), where V’=V  {s}, E’=E  {(s,v): v  V} Extend w s.t. w(s,v)=0 for all v  V Define h(v)=  (s,v) for all v  V’ For all edges (u,v)  E’, we have h(v)  h(u)+w(u,v) Thus

p17. Johnson’s algorithm for sparse graphs: Input : sparse graph in adj. list representation Output : either returns a matrix of shortest-path weights for all pairs or reports that the input graph contains a negative- weight cycle

p18. Johnson(G) {compute G’, where V[G’]=V[G]  {s} and E[G’]=E[G]  {(s,v):v  V[G]} if Bellman-Ford(G’, w, s)=False then print “  a negative-weight cycle” else for each vertex v  V[G’] do set h(v)=  (s,v) computed by the Bellman-Ford algo. for each edge (u,v)  E[G’] do for each vertex u  V[G] do run Dijkstra(G,, u) to compute for all v  V[G] for each v  V[G] do return D } O(V 2 logV+VE) Fibonacci heap O(VElogV) binary heap