All-Pairs Shortest Paths

Similar documents
Analysis of Algorithms I: All-Pairs Shortest Paths

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

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

Single Source Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Chapter 8 Dynamic Programming

Chapter 8 Dynamic Programming

CS 241 Analysis of Algorithms

Design and Analysis of Algorithms

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

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

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Dynamic Programming: Shortest Paths and DFA to Reg Exps

IS 2610: Data Structures

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

Introduction to Algorithms

Partha Sarathi Mandal

Data Structures and and Algorithm Xiaoqing Zheng

CS173 Lecture B, November 3, 2015

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

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Dynamic Programming. p. 1/43

Introduction to Algorithms

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Introduction to Algorithms

Design and Analysis of Algorithms

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Shortest paths with negative lengths

2 hours THE UNIVERSITY OF MANCHESTER. 6 June :45 11:45

Mathematics for Decision Making: An Introduction. Lecture 8

Chapter 1 Divide and Conquer Algorithm Theory WS 2016/17 Fabian Kuhn

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Relations Graphical View

Single Source Shortest Paths

CS173 Strong Induction and Functions. Tandy Warnow

Myriad of applications

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Induced Cycles of Fixed Length

Ngày 20 tháng 7 năm Discrete Optimization Graphs

Introduction to Algorithms

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Notes. Relations. Introduction. Notes. Relations. Notes. Definition. Example. Slides by Christopher M. Bourke Instructor: Berthe Y.

Find: a multiset M { 1,..., n } so that. i M w i W and. i M v i is maximized. Find: a set S { 1,..., n } so that. i S w i W and. i S v i is maximized.

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

Lecture 2: Divide and conquer and Dynamic programming

Part V. Matchings. Matching. 19 Augmenting Paths for Matchings. 18 Bipartite Matching via Flows

1 Closest Pair of Points on the Plane

Introduction to Algorithms

Query Processing in Spatial Network Databases

Recurrence Relations

6. DYNAMIC PROGRAMMING II. sequence alignment Hirschberg's algorithm Bellman-Ford distance vector protocols negative cycles in a digraph

Discrete (and Continuous) Optimization WI4 131

Introduction to Algorithms

Design and Analysis of Algorithms

Solution of a conjecture of Volkmann on the number of vertices in longest paths and cycles of strong semicomplete multipartite digraphs

Today s Outline. CS 362, Lecture 13. Matrix Chain Multiplication. Paranthesizing Matrices. Matrix Multiplication. Jared Saia University of New Mexico

CSE 421 Introduction to Algorithms Final Exam Winter 2005

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

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

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

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

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Discrete Applied Mathematics

Omega notation. Transitivity etc.

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

Complexity Theory of Polynomial-Time Problems

6. DYNAMIC PROGRAMMING II

Dynamic Programming. Prof. S.J. Soni

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Dynamic Programming (CLRS )

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

MA015: Graph Algorithms

Tropical Arithmetic and Shortest Paths

Lecture 7: Dynamic Programming I: Optimal BSTs

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

Practice Final Solutions. 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) { 2 j = 0 3 if (n = 0) { 4 return j

Dynamic Programming. Reading: CLRS Chapter 15 & Section CSE 2331 Algorithms Steve Lai

Legendre s Equation. PHYS Southern Illinois University. October 18, 2016

Dynamic Programming. Cormen et. al. IV 15

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Contents. Counting Methods and Induction

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

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

Disjoint paths in unions of tournaments

Maximum Flow. Jie Wang. University of Massachusetts Lowell Department of Computer Science. J. Wang (UMass Lowell) Maximum Flow 1 / 27

U.C. Berkeley CS294: Beyond Worst-Case Analysis Handout 3 Luca Trevisan August 31, 2017

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

4.4 Recurrences and Big-O

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

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

Data Structures and Algorithms Chapter 3

Big-oh stuff. You should know this definition by heart and be able to give it,

Introduction to Divide and Conquer

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Transcription:

All-Pairs Shortest Paths Version of October 28, 2016 Version of October 28, 2016 All-Pairs Shortest Paths 1 / 26

Outline Another example of dynamic programming Will see two different dynamic programming formulations for same problem. Outline The all-pairs shortest path problem. A first dynamic programming solution. The Floyd-Warshall algorithm Extracting shortest paths. Version of October 28, 2016 All-Pairs Shortest Paths 2 / 26

The All-Pairs Shortest Paths Problem Input: weighted digraph G = (V, E) with weight function w : E R Find: lengths of the shortest paths (i.e., distance) between all pairs of vertices in G. we assume that there are no cycles with zero or negative cost. a 12 20 6 e 5 4 3 3 b 8 4 a 20 e 4 4 b 5 d 17 c d 10 c without negative cost cycle with negative cost cycle Version of October 28, 2016 All-Pairs Shortest Paths 3 / 26

Solution 1: Using Dkstra s Algorithm Where there are no negative cost edges. Apply Dkstra s algorithm n times, once with each vertex (as the source) of the shortest path tree. Recall that Dkstra algorithm runs in Θ(e log n) n = V and e = E. This gives a Θ(ne log n) time algorithm If the digraph is dense, this is a Θ(n 3 log n) algorithm. When negative-weight edges are present: Run the Bellman-Ford algorithm from each vertex. O(n 2 e), which is O(n 4 ) for dense graphs. We don t learn Bellman-Ford in this class. Version of October 28, 2016 All-Pairs Shortest Paths 4 / 26

Outline The all-pairs shortest path problem. A first dynamic programming solution. The Floyd-Warshall algorithm Extracting shortest paths. Version of October 28, 2016 All-Pairs Shortest Paths 5 / 26

Input and Output Formats Input Format: To simplify the notation, we assume that V = {1, 2,..., n}. Adjacency matrix: graph is represented by an n n matrix containing edge weights 0 if i = j, w = w(i, j) if i j and (i, j) E, if i j and (i, j) / E. Output Format: an n n matrix D = [d ] in which d is the length of the shortest path from vertex i to j. Version of October 28, 2016 All-Pairs Shortest Paths 6 / 26

Step 1: Space of Subproblems For m = 1, 2, 3..., Define d (m) to be the length of the shortest path from i to j that contains at most m edges. Let D (m) be the n n matrix [d (m) ]. We will see (next page) that solution D satisfies D = D n 1. Subproblems: (Iteratively) compute D (m) for m = 1,..., n 1. Version of October 28, 2016 All-Pairs Shortest Paths 7 / 26

Step 1: Space of Subproblems Lemma D (n 1) = D, i.e. = true distance from i to j d (n 1) Proof. We prove that any shortest path P from i to j contains at most n 1 edges. First note that since all cycles have positive weight, a shortest path can have no cycles (if there were a cycle, we could remove it and lower the length of the path). A path without cycles can have length at most n 1 (since a longer path must contain some vertex twice, that is, contain a cycle). Version of October 28, 2016 All-Pairs Shortest Paths 8 / 26

Step 2: Building D (m) from D (m 1). Consider a shortest path from i to j that contains at most m edges. Let k be the vertex immediately before j on the shortest path (k could be i). The sub-path from i to k must be the shortest1-k path with at most m 1 edges. Then d (m) = d (m 1) + w kj. Since we don t know k, we try all possible choices: 1 k n. d (m) { } = min d (m 1) 1 k n + w kj Version of October 28, 2016 All-Pairs Shortest Paths 9 / 26

Step 3: Bottom-up Computation of D (n 1) Initialization: D (1) = [w ], the weight matrix. Iteration step: Compute D (m) from D (m 1), for m = 2,..., n 1, using { } d (m) = min 1 k n d (m 1) + w kj Version of October 28, 2016 All-Pairs Shortest Paths 10 / 26

Example: Bottom-up Computation of D (n 1) D (1) = [w ] is just the weight matrix: D (1) = 0 3 8 0 4 11 0 7 4 0 1 3 2 8 11 4 4 4 7 3 d (2) D (2) = { } = min d (1) 1 k 4 + w kj 0 3 7 14 15 0 4 11 11 0 7 4 7 12 0 d (3) D (3) = { } = min d (2) 1 k 4 + w kj 0 3 7 14 15 0 4 11 11 14 0 7 4 7 11 0 D (3) gives the distances between any pair of vertices. Version of October 28, 2016 All-Pairs Shortest Paths 11 / 26

{ } d (m) = min d (m 1) 1 k n + w kj for m = 1 to n 1 do for i = 1 to n do for j = 1 to n do min = ; for k = 1 to n do new = d (m 1) + w kj ; if new < min then min = new d (m) = min; Version of October 28, 2016 All-Pairs Shortest Paths 12 / 26

Notes Running time O(n 4 ), much worse than the solution using Dkstra s algorithm. Question Can we improve this? Version of October 28, 2016 All-Pairs Shortest Paths 12 / 26

Repeated Squaring We use the recurrence relation: Initialization: D (1) = [w ], the weight matrix. For s 1 compute D (2s) using d (2s) = min 1 k n { d (s) } + d (s) kj From equation, can calculate D (2i ) from D (2 i 1 ) in O(n 3 ) time. have shown earlier that the shortest path between any two vertices contains no more than n 1 edges. So D i = D (n 1) for all i n. We can therefore calculate all of D (2), D (4), D (8),..., D (2 log 2 n ) = D (n 1) in O(n 3 log n) time, improving our running time. Version of October 28, 2016 All-Pairs Shortest Paths 13 / 26

Outline The all-pairs shortest path problem. A first dynamic programming solution. The Floyd-Warshall algorithm Extracting shortest paths. Version of October 28, 2016 All-Pairs Shortest Paths 14 / 26

Step 1: Space of subproblems The vertices v 2, v 3,..., v l 1 are called the intermediate vertices of the path p = v 1, v 2,..., v l 1, v l. For any k=0, 1,..., n, let d (k) be the length of the shortest path from i to j such that all intermediate vertices on the path (if any) are in the set {1, 2,..., k}. Version of October 28, 2016 All-Pairs Shortest Paths 15 / 26

Step 1: Space of subproblems is the length of the shortest path from i to j such that all intermediate vertices on the path (if any) are in the set {1, 2,..., k}. d (k) Let D (k) be the n n matrix [d (k) ]. Subproblems: compute D (k) for k = 0, 1,, n. Original Problem: D = D (n), i.e. d (n) from i to j is the shortest distance Version of October 28, 2016 All-Pairs Shortest Paths 16 / 26

Step 2: Relating Subproblems Observation : For a shortest path from i to j with intermediate vertices from the set {1, 2,..., k}, there are two possibilities: 1 k is not a vertex on the path: d (k) 2 k is a vertex on the path.: d (k) = d (k 1) = d (k 1) + d (k 1) kj (Impossible for k to appear in path twice. Why?) So: { } d (k) = min d (k 1), d (k 1) + d (k 1) kj Version of October 28, 2016 All-Pairs Shortest Paths 17 / 26

Step 2: Relating Subproblems Proof. Consider a shortest path from i to j with intermediate vertices from the set {1, 2,..., k}. Either it contains vertex k or it does not. If it does not contain vertex k, then its length must be d (k 1). Otherwise, it contains vertex k, and we can decompose it into a subpath from i to k and a subpath from k to j. Each subpath can only contain intermediate vertices in {1,..., k 1}, and must be as short as possible. Hence they have lengths d (k 1) and d (k 1) kj. Hence the shortest path from i to j has length { min d (k 1), d (k 1) + d (k 1) kj }. Version of October 28, 2016 All-Pairs Shortest Paths 18 / 26

Step 3: Bottom-up Computation Initialization: D (0) = [w ], the weight matrix. Compute D (k) from D (k 1) using ( = min d (k) for k = 1,..., n. d (k 1), d (k 1) ) + d (k 1) kj Version of October 28, 2016 All-Pairs Shortest Paths 19 / 26

The Floyd-Warshall Algorithm: Version 1 Floyd-Warshall(w, n): d (k) ( = min d (k 1), d (k 1) + d (k 1) kj for i = 1 to n do for j = 1 to n do d (0) [i, j] = w[i, j]; pred[i, j] = nil; // initialize // dynamic programming for k = 1 to n do for i = 1 to n do for j = ( 1 to n do ) if d (k 1) [i, k] + d (k 1) [k, j] < d (k 1) [i, j] then d (k) [i, j] = d (k 1) [i, k] + d (k 1) [k, j]; pred[i, j] = k; else d (k) [i, j] = d (k 1) [i, j]; return d (n) [1..n, 1..n] Version of October 28, 2016 All-Pairs Shortest Paths 20 / 26 )

Comments on the Floyd-Warshall Algorithm The algorithm s running time is clearly Θ(n 3 ). The predecessor pointer pred[i, j] can be used to extract the shortest paths (see later). Problem: the algorithm uses Θ(n 3 ) space. It is possible to reduce this to Θ(n 2 ) space by keeping only one matrix instead of n. Algorithm is on next page. Convince yourself that it works. Version of October 28, 2016 All-Pairs Shortest Paths 21 / 26

The Floyd-Warshall Algorithm: Version 2 Floyd-Warshall(w, n) d (k) ( = min d (k 1), d (k 1) ) + d (k 1) kj for i = 1 to n do for j = 1 to n do d[i, j] = w[i, j]; pred[i, j] = nil; // initialize // dynamic programming for k = 1 to n do for i = 1 to n do for j = 1 to n do if (d[i, k] + d[k, j] < d[i, j]) then d[i, j] = d[i, k] + d[k, j]; pred[i, j] = k; return d[1..n, 1..n]; Version of October 28, 2016 All-Pairs Shortest Paths 22 / 26

Outline The all-pairs shortest path problem. A first dynamic programming solution. The Floyd-Warshall algorithm Extracting shortest paths. Version of October 28, 2016 All-Pairs Shortest Paths 23 / 26

Extracting the Shortest Paths predecessor pointers pred[i, j] can be used to extract the shortest paths. Idea: Whenever we discover that the shortest path from i to j passes through an intermediate vertex k, we set pred[i, j] = k. If the shortest path does not pass through any intermediate vertex, then pred[i, j] = nil. To find the shortest path from i to j, we consult pred[i, j]. 1 If it is nil, then the shortest path is just the edge (i, j). 2 Otherwise, we recursively construct the shortest path from i to pred[i, j] and the shortest path from pred[i, j] to j. Version of October 28, 2016 All-Pairs Shortest Paths 24 / 26

The Algorithm for Extracting the Shortest Paths Path(i, j) if pred[i, j] = nil then // single edge output (i, j); else // compute the two parts of the path Path(i, pred[i, j]); Path( pred[i, j], j); Version of October 28, 2016 All-Pairs Shortest Paths 25 / 26

Example of Extracting the Shortest Paths Find the shortest path from vertex 2 to vertex 3. 2..3 Path(2, 3) pred[2, 3] = 6 2..6..3 Path(2, 6) pred[2, 6] = 5 2..5..6..3 Path(2, 5) pred[2, 5] = nil Output(2,5) 25..6..3 Path(5, 6) pred[5, 6] = nil Output(5,6) 256..3 Path(6, 3) pred[6, 3] = 4 256..4..3 Path(6, 4) pred[6, 4] = nil Output(6,4) 2564..3 Path(4, 3) pred[4, 3] = nil Output(4,3) 25643 Version of October 28, 2016 All-Pairs Shortest Paths 26 / 26