Lecture Notes for Chapter 25: All-Pairs Shortest Paths

Similar documents
Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

Design and Analysis of Algorithms

Analysis of Algorithms I: All-Pairs Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

All-Pairs Shortest Paths

Introduction to Algorithms

Single Source Shortest Paths

Design and Analysis of Algorithms

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

Partha Sarathi Mandal

Data Structures and and Algorithm Xiaoqing Zheng

Single Source Shortest Paths

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Shortest Path Algorithms

Introduction to Algorithms

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Myriad of applications

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CS 241 Analysis of Algorithms

Introduction to Algorithms

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS173 Lecture B, November 3, 2015

Complexity Theory of Polynomial-Time Problems

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

The Simplest Construction of Expanders

CSE 431/531: Analysis of Algorithms. Dynamic Programming. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Chapter 8 Dynamic Programming

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

Chapter 8 Dynamic Programming

Inderjit Dhillon The University of Texas at Austin

Query Processing in Spatial Network Databases

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

BFS Dijkstra. Oct abhi shelat

Proof Techniques (Review of Math 271)

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

CS 6820 Fall 2014 Lectures, October 3-20, 2014

Tropical Arithmetic and Shortest Paths

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

iretilp : An efficient incremental algorithm for min-period retiming under general delay model

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

A faster algorithm for the single source shortest path problem with few distinct positive lengths

Applications of Binary Search

Lecture #21. c T x Ax b. maximize subject to

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Relations Graphical View

Shortest paths with negative lengths

Network Design and Game Theory Spring 2008 Lecture 6

Discrete Mathematics. Spring 2017

III. Linear Programming

On the Exponent of the All Pairs Shortest Path Problem

The Matching Polytope: General graphs

Admin NP-COMPLETE PROBLEMS. Run-time analysis. Tractable vs. intractable problems 5/2/13. What is a tractable problem?

Flows. Chapter Circulations

CH 59 SQUARE ROOTS. Every positive number has two square roots. Ch 59 Square Roots. Introduction

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Dynamic Programming. p. 1/43

x 1 + x 2 2 x 1 x 2 1 x 2 2 min 3x 1 + 2x 2

Lecture 6 January 21, 2013

Algorithm Theory - Exercise Class

MA015: Graph Algorithms

A Note on the Connection between the Primal-Dual and the A* Algorithm

PGSS Discrete Math Solutions to Problem Set #4. Note: signifies the end of a problem, and signifies the end of a proof.

Math Lecture 23 Notes

means is a subset of. So we say A B for sets A and B if x A we have x B holds. BY CONTRAST, a S means that a is a member of S.

MA 3280 Lecture 05 - Generalized Echelon Form and Free Variables. Friday, January 31, 2014.

Algorithm Design and Analysis

Shortest Paths in Directed Planar Graphs with Negative Lengths: a Linear-Space O(n log 2 n)-time Algorithm

Maximum Flow. Reading: CLRS Chapter 26. CSE 6331 Algorithms Steve Lai

P versus NP. Math 40210, Fall November 10, Math (Fall 2015) P versus NP November 10, / 9

CS Data Structures and Algorithm Analysis

Complex Networks CSYS/MATH 303, Spring, Prof. Peter Dodds

CSE 421 Introduction to Algorithms Final Exam Winter 2005

Design and Analysis of Algorithms April 16, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 2

from notes written mostly by Dr. Matt Stallmann: All Rights Reserved

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Scribes: Po-Hsuan Wei, William Kuzmaul Editor: Kevin Wu Date: October 18, 2016

CS261: A Second Course in Algorithms Lecture #8: Linear Programming Duality (Part 1)

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

Topics in Theoretical Computer Science April 08, Lecture 8

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Lecture 13: Spectral Graph Theory

2.3 Some Properties of Continuous Functions

Equivalence, Order, and Inductive Proof

6.889 Lecture 4: Single-Source Shortest Paths

NAME: Be clear and concise. You may use the number of points assigned toeach problem as a rough

CSE 326: Data Structures Network Flow. James Fogarty Autumn 2007

Lecture 4: Constructing the Integers, Rationals and Reals

Bob Brown, CCBC Essex Math 163 College Algebra, Chapter 1 Section 7 COMPLETED 1 Linear, Compound, and Absolute Value Inequalities

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Transcription:

Lecture Notes for Chapter 25: All-Pairs Shortest Paths Chapter 25 overview Given a directed graph G (V, E), weight function w : E R, V n. Goal: create an n n matrix of shortest-path distances δ(u,v). Could run BELLMAN-FORD once from each vertex: O(V 2 E) which is O(V 4 ) if the graph is dense (E (V 2 )). If no negative-weight edges, could run Dkstra s algorithm once from each vertex: O(VElg V ) with binary heap O(V 3 lg V ) if dense, O(V 2 lg V + VE) with Fibonacci heap O(V 3 ) if dense. We ll see how to do in O(V 3 ) in all cases, with no fancy data structure. Shortest paths and matrix multiplication Assume that G is given as adjacency matrix of weights: W (w ), with vertices numbered 1 to n. 0 if i j, w weight of (i, j) if i j, (i, j) E, if i j, (i, j) / E. Output is matrix D (d ), where d δ(i, j). Won t worry about predecessors see book. Will use dynamic programming at Þrst. Optimal substructure: Recall: subpaths of shortest paths are shortest paths. Recursive solution: Let l (m) weight of shortest path i j that contains m edges. m 0 there is a shortest path i j with m edges if and only if i j l (0) 0 ifi j, if i j.

25-2 Lecture Notes for Chapter 25: All-Pairs Shortest Paths m 1 ( l (m) min l (m 1), min l (m 1) } ) + w kj 1 k n (k is all possible predecessors of j) min 1 k n l (m 1) + w kj } since w jj 0 for all j. Observe that when m 1, must have l (1) w. Conceptually, when the path is restricted to at most 1 edge, the weight of the shortest path i j must be w. And the math works out, too: l (1) min l (0) } + w kj 1 k n l (0) ii + w (l (0) ii is the only non- among l (0) ) w. All simple shortest paths contain n 1 edges δ(i, j) l (n 1) l (n) l (n+1)... Compute a solution bottom-up: Compute L (1), L (2),...,L (n 1). Start with L (1) W, since l (1) w. Go from L (m 1) to L (m) : EXTEND(L, W, n) create L,ann n matrix for i 1 to n do l do l min(l, l + w kj ) return L Compute each L (m) : SLOW-APSP(W, n) L (1) W for m 2 to n 1 do L (m) EXTEND(L (m 1), W, n) return L (n 1) Time: EXTEND: (n 3 ). SLOW-APSP: (n 4 ).

Lecture Notes for Chapter 25: All-Pairs Shortest Paths 25-3 Observation: EXTEND is le matrix multiplication: L A W B L C min + + 0 create C, ann n matrix for i 1 to n do c 0 do c c + a b kj So, we can view EXTEND as just le matrix multiplication! Why do we care? Because our goal is to compute L (n 1) as fast as we can. Don t need to compute all the intermediate L (1), L (2), L (3),...,L (n 2). Suppose we had a matrix A and we wanted to compute A n 1 (le calling EXTEND n 1 times). Could compute A, A 2, A 4, A 8,... If we knew A m A n 1 for all m n 1, could just Þnish with A r, where r is the smallest power of 2 that s n 1. (r 2 lg(n 1) ) FASTER-APSP(W, n) L (1) W m 1 while m < n 1 do L (2m) EXTEND(L (m), L (m), n) m 2m return L (m) OK to overshoot, since products don t change after L (n 1). Time: (n 3 lg n). Floyd-Warshall algorithm A different dynamic-programming approach. For path p v 1,v 2,...,v l,anintermediate vertex is any vertex of p other than v 1 or v l. Let d (k) shortest-path weight of any path i j with all intermediate vertices in 1, 2,...,k}. p Consider a shortest path i j with all intermediate vertices in 1, 2,...,k}:

25-4 Lecture Notes for Chapter 25: All-Pairs Shortest Paths If k is not an intermediate vertex, then all intermediate vertices of p are in 1, 2,...,k 1}. If k is an intermediate vertex: p 1 p 2 i k j all intermediate vertices in 1, 2,..., k 1} Recursive formulation d (k) w if k 0, min ( d (k 1), d (k 1) + d (k 1) ) kj if k 1. (Have d (0) w because can t have intermediate vertices 1 edge.) Want D (n) ( d (n) ), since all vertices numbered n. Compute bottom-up Compute in increasing order of k: FLOYD-WARSHALL(W, n) D (0) W do for i 1 to n return D (n) do d (k) min ( d (k 1), d (k 1) Can drop superscripts. (See Exercise 25.2-4 in text.) Time: (n 3 ). + d (k 1) ) kj Transitive closure Given G (V, E), directed. Compute G (V, E ). E (i, j) : there is a path i j in G}. Could assign weight of 1 to each edge, then run FLOYD-WARSHALL. If d < n, then there is a path i j. Otherwise, d and there is no path.

Lecture Notes for Chapter 25: All-Pairs Shortest Paths 25-5 Simpler way: Substitute other values and operators in FLOYD-WARSHALL. Use unweighted adjacency matrix min (OR) + (AND) t (k) 1 if there is path i j with all intermediate vertices in1, 2,...,k}, 0 otherwise. t (0) 0ifi j and (i, j) / E, 1ifi j or (i, j) E. t (k) t (k 1) ( t (k 1) t (k 1) ) kj. TRANSITIVE-CLOSURE(E, n) for i 1 to n do if i j or (i, j) E[G] then t (0) 1 else t (0) 0 do for i 1 to n return T (n) do t (k) t (k 1) ( t (k 1) t (k 1) ) kj Time: (n 3 ), but simpler operations than FLOYD-WARSHALL. Johnson s algorithm Idea: If the graph is sparse, it pays to run Dkstra s algorithm once from each vertex. If we use a Fibonacci heap for the priority queue, the running time is down to O(V 2 lg V + VE), which is better than FLOYD-WARSHALL s (V 3 ) time if E o(v 2 ). But Dkstra s algorithm requires that all edge weights be nonnegative. Donald Johnson Þgured out how to make an equivalent graph that does have all edge weights 0. Reweighting Compute a new weight function ŵ such that 1. For all u,v V, p is a shortest path u v using w if and only if p is a shortest path u v using ŵ. 2. For all (u,v) E, ŵ(u,v) 0.

25-6 Lecture Notes for Chapter 25: All-Pairs Shortest Paths Property (1) says that it sufþces to Þnd shortest paths with ŵ. Property (2) says we can do so by running Dkstra s algorithm from each vertex. How to come up with ŵ? Lemma shows it s easy to get property (1): Lemma (Reweighting doesn t change shortest paths) Given a directed, weighted graph G (V, E), w : E R. Let h be any function such that h : V R. For all (u,v) E, deþne ŵ(u,v) w(u,v)+ h(u) h(v). Let p v 0,v 1,...,v k be any path v 0 v k. Then, p is a shortest path v 0 v k with w if and only if p is a shortest path v 0 v k with ŵ. (Formally, w(p) δ(v 0,v k ) if and only if ŵ δ(v 0,v k ), where δ is the shortest-path weight with ŵ.) Also, G has a negative-weight cycle with w if and only if G has a negative-weight cycle with ŵ. Proof First, we ll show that ŵ(p) w(p) + h(v 0 ) h(v k ): k ŵ(p) ŵ(v i 1,v i ) i1 k (w(v i 1,v i ) + h(v i 1 ) h(v i )) i1 k w(v i 1,v i ) + h(v 0 ) h(v k ) i1 w(p) + h(v 0 ) h(v k ). (sum telescopes) p Therefore, any path v 0 v k has ŵ(p) w(p) + h(v 0 ) h(v k ). Since h(v 0 ) and h(v k ) don t depend on the path from v 0 to v k, if one path v 0 v k is shorter than another with w, it s also shorter with ŵ. Now show there exists a negative-weight cycle with w if and only if there exists a negative-weight cycle with ŵ: Let cycle c v 0,v 1,...,v k, where v 0 v k. Then ŵ(c) w(c) + h(v 0 ) h(v k ) w(c) (since v 0 v k ). Therefore, c has a negative-weight cycle with w if and only if it has a negativeweight cycle with ŵ. (lemma) So, now to get property (2), we just need to come up with a function h : V R such that when we compute ŵ(u,v) w(u,v)+ h(u) h(v), it s 0. Do what we did for difference constraints: G (V, E )

Lecture Notes for Chapter 25: All-Pairs Shortest Paths 25-7 V V s}, where s is a new vertex. E E (s,v): v V }. w(s,v) 0 for all v V. Since no edges enter s, G has the same set of cycles as G. In particular, G has a negative-weight cycle if and only if G does. DeÞne h(v) δ(s,v)for all v V. Claim ŵ(u,v) w(u,v)+ h(u) h(v) 0. Proof By the triangle inequality, δ(s,v) δ(s, u) + w(u,v) h(v) h(u) + w(u,v). Therefore, w(u,v) + h(u) h(v) 0. (claim) Johnson s algorithm form G run BELLMAN-FORD on G to compute δ(s,v)for all v V if BELLMAN-FORD returns FALSE then G has a negative-weight cycle else compute ŵ(u,v) w(u,v)+ δ(s, u) δ(s,v)for all (u,v) E for each vertex u V do run Dkstra s algorithm from u using weight function ŵ to compute δ(u,v)for all v V for each vertex v V do Compute entry d uv in matrix D d uv δ(u,v)+ δ(s,v) δ(s, u) }} because if p is a path u v, then ŵ(p) w(p) + h(u) h(v) Time: (V + E) to compute G. O(VE) to run BELLMAN-FORD. (E) to compute ŵ. O(V 2 lg V + VE) to run Dkstra s algorithm V times (using Fibonacci heap). (V 2 ) to compute D matrix. Total: O(V 2 lg V + VE).