Shortest Paths with Dynamic Programming

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
1 Chapter 6 Dynamic Programming Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Single Source Shortest Paths
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
CSEP 521 Applied Algorithms Richard Anderson Lecture 7 Dynamic Programming.
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.
Shortest Paths Definitions Single Source Algorithms
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
CSE 421 Algorithms Richard Anderson Lecture 21 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.
10/4/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 17 Dynamic.
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.
CS38 Introduction to Algorithms Lecture 10 May 1, 2014.
CSEP 521 Applied Algorithms Richard Anderson Lecture 8 Network Flow.
CSE 421 Algorithms Richard Anderson Lecture 21 Shortest Paths and Network Flow.
Graphs – Part III CS 367 – Introduction to Data Structures.
Shortest Paths C B A E D F Shortest Paths
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Lecture 13 Shortest Path.
CSC317 Shortest path algorithms
Dijkstra’s shortest path Algorithm
Shortest Paths C B A E D F Shortest Paths 1
Shortest Paths C B A E D F Shortest Paths
Algorithms and Data Structures Lecture XIII
Shortest Paths C B A E D F Shortest Paths
Discussion section #2 HW1 questions?
Discussion section #2 HW1 questions?
Chapter 6 Dynamic Programming
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
CSCE 411 Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Richard Anderson Lecture 20 LCS / Shortest Paths
Chapter 6 Dynamic Programming
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Autumn 2015 Lecture 10 Minimum Spanning Trees
Richard Anderson Lecture 21 Shortest Path Network Flow Introduction
Chapter 6 Dynamic Programming
Richard Anderson Lecture 21 Shortest Paths
Lecture 13 Algorithm Analysis
Richard Anderson Autumn 2016 Lecture 5
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming Longest Path in a DAG, Yin Tat Lee
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
Shortest Path Problems
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
The Bellman-Ford Shortest Path Algorithm Neil Tang 03/27/2008
Evaluation of the Course (Modified)
Single-Source All-Destinations Shortest Paths With Negative Costs
Chapter 24: Single-Source Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
Lecture 12 Shortest Path.
Richard Anderson Lecture 20 Shortest Paths and Network Flow
Winter 2019 Lecture 10 Minimum Spanning Trees
Chapter 24: Single-Source Shortest-Path
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Richard Anderson Lecture 20 Space Efficient LCS
Memory Efficient Dynamic Programming / Shortest Paths
Single-Source All-Destinations Shortest Paths With Negative Costs
Directed Graphs (Part II)
Advanced Algorithms Analysis and Design
Presentation transcript:

Shortest Paths with Dynamic Programming Bellman-Ford Algorithm Paul Beame

Shortest Path Problem Dijkstra’s Single Source Shortest Paths Algorithm O(mlog n) time, positive cost edges Bellman-Ford Algorithm O(mn) time for graphs with negative cost edges

Shortest paths with negative cost edges Dijsktra’s algorithm failed with negative-cost edges What can we do in this case? Negative-cost cycles could result in shortest paths with length - but these would be infinitely long... What if we just wanted shortest paths of exactly i edges?

Shortest paths with negative cost edges (Bellman-Ford) We want to grow paths from s to t based on the # of edges in the path Let Cost(s,w,i)=cost of minimum-length path from s to w using exactly i edges. Cost(s,w,0) = 0 if w=s  otherwise Cost(s,w,i) = min(v,w)E(Cost(s,v,i-1)+cvw)

Bellman-Ford Observe that the recursion for Cost(s,w,i) doesn’t change s Only store an entry for each w and i OPTi(w) OPT0(w)= 0 if w=s  otherwise OPTi(w) = min(v,w)E(OPTi-1(v)+cvw)

Shortest paths with negative cost edges (Bellman-Ford) Suppose no negative-cost cycles in G Shortest path from s to t has at most n-1 edges If not, there would be a repeated vertex which would create a cycle that could be removed since cycle can’t have –ve cost

Algorithm, Version 1 foreach w M[0, w] = infinity; M[0, s] = 0; for i = 1 to n-1 M[i, w] = minv(M[i-1,v] + cost[v,w]); What if we want to allow up to i edges rather than require exactly i edges?

Algorithm, Version 2 foreach w M[0, w] = infinity; M[0, s] = 0; for i = 1 to n-1 M[i, w] = min(M[i-1, w], minv(M[i-1,v] + cost[v,w])) Now M[i,w] ≤ M[i-1,w] ≤ ... ≤ M[0,w] . If all we only care about is finding short paths we can use the shortest length we have found and forget # of hops

Algorithm, Version 3 foreach w M[w] = infinity; M[s] = 0; for i = 1 to n-1 M[w] = min(M[w], minv(M[v] + cost[v,w]))

Correctness Proof for Algorithm 3 Key lemma – at the end of iteration i, for all w, M[w] ≤ M[i, w]; Reconstructing the path: Set P[w] = v, whenever M[w] is updated from vertex v

Bellman-Ford -2   5 6 - 4 -3 s 8 7  2 7   9

Bellman-Ford -2   5 6 - 4 -3 s 8 7 2 7   9

Bellman-Ford -2  6 5 6 - 4 -3 s 8 7 2 7  7 9

Bellman-Ford -2 4 6 5 6 - 4 -3 s 8 7 2 7 2 7 9

Bellman-Ford -2 4 2 5 6 - 4 -3 s 8 7 2 7 2 7 9

Bellman-Ford -2 4 2 5 6 - 4 -3 s 8 7 2 7 -2 7 9

Bellman-Ford -2 4 2 5 6 - 4 -3 s 8 7 2 7 -2 7 9

Other details Can run algorithm and stop early if M doesn’t change in an iteration Even better, one can update only neighbors x of vertices w whose M value changed in an iteration

If P[w] = v then M[w] ≥ M[v] + cost(v,w) If the pointer graph has a cycle, then the graph has a negative cost cycle If P[w] = v then M[w] ≥ M[v] + cost(v,w) Equal when w is updated M[v] could later be reduced after update Let v1, v2,…vk be a cycle in the pointer graph with (vk,v1) the last edge added Just before the update M[vj] ≥ M[vj+1] + cost(vj+1, vj) for j < k M[vk] > M[v1] + cost(v1, vk) Adding everything up 0 > cost(v1,v2) + cost(v2,v3) + … + cost(vk, v1) v1 v4 v2 v3

Finding negative cost cycles What if you want to find negative cost cycles? 2 -2 3 2 -3 5 6 2 1 2 2 -5 2 4

Foreign Exchange Arbitrage USD 1.2 1.2 USD EUR CAD ------ 0.8 1.2 1.6 0.6 ----- CAD EUR 0.6 USD 0.8 0.8 CAD EUR 1.6

Bellman-Ford with a DAG Edges only go from lower to higher-numbered vertices Update distances in order of topological sort Only one pass through vertices required O(n+m) time 1 4 3 12 10 8 9 11 13 14 5 6 7 2