Introduction to Algorithms

Similar documents
Introduction to Algorithms

CS 331 DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING. Dr. Daisy Tang

U.C. Berkeley CS294: Beyond Worst-Case Analysis Luca Trevisan September 5, 2017

Analysis of Algorithms I: All-Pairs Shortest Paths

Dynamic Systems on Graphs

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Dynamic Programming. Preview. Dynamic Programming. Dynamic Programming. Dynamic Programming (Example: Fibonacci Sequence)

8.4 COMPLEX VECTOR SPACES AND INNER PRODUCTS

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

All-Pairs Shortest Paths

Design and Analysis of Algorithms

Problem Set 9 Solutions

Dynamic Programming. Lecture 13 (5/31/2017)

Single Source Shortest Paths

Lecture 5 Decoding Binary BCH Codes

Yong Joon Ryang. 1. Introduction Consider the multicommodity transportation problem with convex quadratic cost function. 1 2 (x x0 ) T Q(x x 0 )

Poisson brackets and canonical transformations

Random Walks on Digraphs

Some Comments on Accelerating Convergence of Iterative Sequences Using Direct Inversion of the Iterative Subspace (DIIS)

APPENDIX A Some Linear Algebra

Math 217 Fall 2013 Homework 2 Solutions

Outline and Reading. Dynamic Programming. Dynamic Programming revealed. Computing Fibonacci. The General Dynamic Programming Technique

Introduction to Algorithms

Singular Value Decomposition: Theory and Applications

princeton univ. F 17 cos 521: Advanced Algorithm Design Lecture 7: LP Duality Lecturer: Matt Weinberg

Advanced Topics in Optimization. Piecewise Linear Approximation of a Nonlinear Function

Lecture 3. Ax x i a i. i i

Lectures - Week 4 Matrix norms, Conditioning, Vector Spaces, Linear Independence, Spanning sets and Basis, Null space and Range of a Matrix

2.3 Nilpotent endomorphisms

Perron Vectors of an Irreducible Nonnegative Interval Matrix

Min Cut, Fast Cut, Polynomial Identities

MEM 255 Introduction to Control Systems Review: Basics of Linear Algebra

Errors for Linear Systems

Introduction to Algorithms

Grover s Algorithm + Quantum Zeno Effect + Vaidman

The Minimum Universal Cost Flow in an Infeasible Flow Network

Additional Codes using Finite Difference Method. 1 HJB Equation for Consumption-Saving Problem Without Uncertainty

Lecture 17: Lee-Sidford Barrier

Foundations of Arithmetic

U.C. Berkeley CS294: Beyond Worst-Case Analysis Handout 6 Luca Trevisan September 12, 2017

SL n (F ) Equals its Own Derived Group

Quantum Mechanics for Scientists and Engineers. David Miller

Continuous Time Markov Chain

Exercises. 18 Algorithms

LECTURE 9 CANONICAL CORRELATION ANALYSIS

find (x): given element x, return the canonical element of the set containing x;

EEE 241: Linear Systems

Combining Constraint Programming and Integer Programming

COS 511: Theoretical Machine Learning. Lecturer: Rob Schapire Lecture # 15 Scribe: Jieming Mao April 1, 2013

Randić Energy and Randić Estrada Index of a Graph

Lecture Notes Introduction to Cluster Algebra

Introduction to Algorithms

A 2D Bounded Linear Program (H,c) 2D Linear Programming

Lecture 20: Lift and Project, SDP Duality. Today we will study the Lift and Project method. Then we will prove the SDP duality theorem.

CS4495/6495 Introduction to Computer Vision. 3C-L3 Calibrating cameras

COS 511: Theoretical Machine Learning. Lecturer: Rob Schapire Lecture #16 Scribe: Yannan Wang April 3, 2014

MA/CSSE 473 Day 27. Dynamic programming

Matrix Approximation via Sampling, Subspace Embedding. 1 Solving Linear Systems Using SVD

Lecture 4: Universal Hash Functions/Streaming Cont d

Design and Analysis of Algorithms

Computers and Mathematics with Applications. Computing a fuzzy shortest path in a network with mixed fuzzy arc lengths using α-cuts

Dynamic Programming 4/5/12. Dynamic programming. Fibonacci numbers. Fibonacci: a first attempt. David Kauchak cs302 Spring 2012

Introduction to Algorithms

Finding Dense Subgraphs in G(n, 1/2)

Introduction to Algorithms

MTH 819 Algebra I S13. Homework 1/ Solutions. 1 if p n b and p n+1 b 0 otherwise ) = 0 if p q or n m. W i = rw i

College of Computer & Information Science Fall 2009 Northeastern University 20 October 2009

MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.265/15.070J Fall 2013 Lecture 12 10/21/2013. Martingale Concentration Inequalities and Applications

Simultaneous Optimization of Berth Allocation, Quay Crane Assignment and Quay Crane Scheduling Problems in Container Terminals

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

Determinants Containing Powers of Generalized Fibonacci Numbers

Algebraic Solutions to Multidimensional Minimax Location Problems with Chebyshev Distance

Changing Topology and Communication Delays

The Exact Formulation of the Inverse of the Tridiagonal Matrix for Solving the 1D Poisson Equation with the Finite Difference Method

Lecture 12: Discrete Laplacian

U.C. Berkeley CS278: Computational Complexity Professor Luca Trevisan 2/21/2008. Notes for Lecture 8

Lecture 14: Bandits with Budget Constraints

Linear Algebra and its Applications

Lecture 21: Numerical methods for pricing American type derivatives

Stanford University CS359G: Graph Partitioning and Expanders Handout 4 Luca Trevisan January 13, 2011

EEL 6266 Power System Operation and Control. Chapter 3 Economic Dispatch Using Dynamic Programming

A property of the elementary symmetric functions

Lecture 4: November 17, Part 1 Single Buffer Management

LINEAR REGRESSION ANALYSIS. MODULE IX Lecture Multicollinearity

Chapter 7 Partially Balanced Incomplete Block Design (PBIBD)

First day August 1, Problems and Solutions

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

763622S ADVANCED QUANTUM MECHANICS Solution Set 1 Spring c n a n. c n 2 = 1.

Convexity preserving interpolation by splines of arbitrary degree

Greedy can also beat pure dynamic programming

Some Consequences. Example of Extended Euclidean Algorithm. The Fundamental Theorem of Arithmetic, II. Characterizing the GCD and LCM

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Non-negative Matrices and Distributed Control

Example: (13320, 22140) =? Solution #1: The divisors of are 1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 27, 30, 36, 41,

Partitioning Graphs of Supply and Demand

Optimal Dispatch in Electricity Markets

CHAPTER 17 Amortized Analysis

Lecture 2: Gram-Schmidt Vectors and the LLL Algorithm

Structure and Drive Paul A. Jensen Copyright July 20, 2003

Design and Analysis of Algorithms

Transcription:

Introducton to Algorthms 6.046J/8.40J LECTURE 6 Shortest Paths III All-pars shortest paths Matrx-multplcaton algorthm Floyd-Warshall algorthm Johnson s algorthm Prof. Charles E. Leserson

Shortest paths Sngle-source shortest paths Nonnegate edge weghts Djkstra s algorthm: O(E + V lg V) General Bellman-Ford algorthm: O(VE) DAG One pass of Bellman-Ford: O(V + E) Introducton to Algorthms Noember 8, 2004 L6.2

Shortest paths Sngle-source shortest paths Nonnegate edge weghts Djkstra s algorthm: O(E + V lg V) General Bellman-Ford: O(VE) DAG One pass of Bellman-Ford: O(V + E) All-pars shortest paths Nonnegate edge weghts Djkstra s algorthm V tmes: O(VE + V 2 lg V) General Three algorthms today. Introducton to Algorthms Noember 8, 2004 L6.3

All-pars shortest paths Input: Dgraph G = (V, E), where V = {, 2,, n}, wth edge-weght functon w : E R. Output: n n matrx of shortest-path lengths δ(, j) for all, j V. Introducton to Algorthms Noember 8, 2004 L6.4

All-pars shortest paths Input: Dgraph G = (V, E), where V = {, 2,, n}, wth edge-weght functon w : E R. Output: n n matrx of shortest-path lengths δ(, j) for all, j V. IDEA: Run Bellman-Ford once from each ertex. Tme = O(V 2 E). Dense graph (n 2 edges) Θ(n 4 ) tme n the worst case. Good frst try! Introducton to Algorthms Noember 8, 2004 L6.5

Dynamc programmng Consder the n n adjacency matrx A = (a j ) of the dgraph, and defne d (m) j = weght of a shortest path from to j that uses at most m edges. Clam: We hae d (0) 0 f = j, j = f j; and for m =, 2,, n, d (m) j = mn k {d (m ) k + a kj }. Introducton to Algorthms Noember 8, 2004 L6.6

Proof of clam d j (m) = mn k {d k (m ) + a kj } m edges m edges m edges k s M j m edges Introducton to Algorthms Noember 8, 2004 L6.7

Proof of clam d j (m) = mn k {d k (m ) + a kj } Relaxaton! for k to n do f d j > d k + a kj then d j d k + a kj m edges m edges m edges k s M m edges j Introducton to Algorthms Noember 8, 2004 L6.8

Proof of clam d j (m) = mn k {d k (m ) + a kj } m edges m edges m edges k s Relaxaton! for k to n do f d j > d k + a kj then d j d k + a kj m edges Note: No negate-weght cycles mples δ(, j) = d (n ) j = d (n) j = d (n+) j = L M j Introducton to Algorthms Noember 8, 2004 L6.9

Matrx multplcaton Compute C = A B, where C, A, and B are n n matrces: n c = a b. j k= Tme = Θ(n 3 ) usng the standard algorthm. k kj Introducton to Algorthms Noember 8, 2004 L6.0

Matrx multplcaton Compute C = A B, where C, A, and B are n n matrces: n c = a b. j k= Tme = Θ(n 3 ) usng the standard algorthm. What f we map + mn and +? k kj Introducton to Algorthms Noember 8, 2004 L6.

Matrx multplcaton Compute C = A B, where C, A, and B are n n matrces: n c = a b. j k= Tme = Θ(n 3 ) usng the standard algorthm. What f we map + mn and +? c j = mn k {a k + b kj }. Thus, D (m) = D (m ) A. Identty matrx = I = k kj 0 0 0 0 = D 0 = (d j (0) ). Introducton to Algorthms Noember 8, 2004 L6.2

Matrx multplcaton (contnued) The (mn, +) multplcaton s assocate, and wth the real numbers, t forms an algebrac structure called a closed semrng. Consequently, we can compute D () = D (0) A = A D (2) = D () A = A 2 M M D (n ) = D (n 2) A = A n, yeldng D (n ) = (δ(, j)). Tme = Θ(n n 3 ) = Θ(n 4 ). No better than n B-F. Introducton to Algorthms Noember 8, 2004 L6.3

Improed matrx multplcaton algorthm Repeated squarng: A 2k = A k A k. Compute A 2, A 4,, A 2 lg(n ). O(lg n) squarngs Note: A n = A n = A n+ = L. Tme = Θ(n 3 lg n). To detect negate-weght cycles, check the dagonal for negate alues n O(n) addtonal tme. Introducton to Algorthms Noember 8, 2004 L6.4

Floyd-Warshall algorthm Also dynamc programmng, but faster! Defne c j (k) = weght of a shortest path from to j wth ntermedate ertces belongng to the set {, 2,, k}. k k k k j Thus, δ(, j) = c j (n). Also, c j (0) = a j. Introducton to Algorthms Noember 8, 2004 L6.5

Floyd-Warshall recurrence c j (k) = mn {c j (k ), c k (k ) + c kj (k ) } c k (k ) k c kj (k ) c j (k ) ntermedate ertces n {, 2,, k} j Introducton to Algorthms Noember 8, 2004 L6.6

Pseudocode for Floyd- Warshall for k to n do for to n do for j to n do f c j > c k + c kj then c j c k + c kj relaxaton Notes: Okay to omt superscrpts, snce extra relaxatons can t hurt. Runs n Θ(n 3 ) tme. Smple to code. Effcent n practce. Introducton to Algorthms Noember 8, 2004 L6.7

Transte closure of a drected graph Compute t j = f there exsts a path from to j, 0 otherwse. IDEA: Use Floyd-Warshall, but wth (, ) nstead of (mn, +): Tme = Θ(n 3 ). t j (k) = t j (k ) (t k (k ) t kj (k ) ). Introducton to Algorthms Noember 8, 2004 L6.8

Graph reweghtng Theorem. Gen a functon h : V R, reweght each edge (u, ) E by w h (u, ) = w(u, ) + h(u) h(). Then, for any two ertces, all paths between them are reweghted by the same amount. Introducton to Algorthms Noember 8, 2004 L6.9

Introducton to Algorthms Noember 8, 2004 L6.20 Graph reweghtng Proof. Let p = 2 L k be a path n G. We hae ( ) ) ( ) ( ) ( ) ( ) ( ), ( ) ( ) ( ), ( ), ( ) ( k k k k k h h h h p w h h w h h w w p w + = + = + = = = + = + + = +. Theorem. Gen a functon h : V R, reweght each edge (u, ) E by w h (u, ) = w(u, ) + h(u) h(). Then, for any two ertces, all paths between them are reweghted by the same amount. Same amount!

Shortest paths n reweghted graphs Corollary. δ h (u, ) = δ(u, ) + h(u) h(). Introducton to Algorthms Noember 8, 2004 L6.2

Shortest paths n reweghted graphs Corollary. δ h (u, ) = δ(u, ) + h(u) h(). IDEA: Fnd a functon h : V R such that w h (u, ) 0 for all (u, ) E. Then, run Djkstra s algorthm from each ertex on the reweghted graph. NOTE: w h (u, ) 0 ff h() h(u) w(u, ). Introducton to Algorthms Noember 8, 2004 L6.22

Johnson s algorthm. Fnd a functon h : V R such that w h (u, ) 0 for all (u, ) E by usng Bellman-Ford to sole the dfference constrants h() h(u) w(u, ), or determne that a negate-weght cycle exsts. Tme = O(VE). 2. Run Djkstra s algorthm usng w h from each ertex u V to compute δ h (u, ) for all V. Tme = O(VE + V 2 lg V). 3. For each (u, ) V V, compute δ(u, ) = δ h (u, ) h(u) + h(). Tme = O(V 2 ). Total tme = O(VE+ V 2 lg V). Introducton to Algorthms Noember 8, 2004 L6.23