CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 22.

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

1 Chapter 6 Dynamic Programming Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Appendix B: Solving TSP by Dynamic Programming Course: Algorithm Design and Analysis.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
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.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 20.
Max Flow: Shortest Augmenting Path
CSCI 256 Data Structures and Algorithm Analysis Lecture 18 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
DYNAMIC PROGRAMMING. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer.
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.
Shortest Path With Negative Weights s 3 t
Lecture 39 CSE 331 Dec 6, On Friday, Dec 10 hours-a-thon Atri: 2:00-3:30 (Bell 123) Jeff: 4:00-5:00 (Bell 224) Alex: 5:00-6:30 (Bell 242)
Shortest Paths Definitions Single Source Algorithms
All-Pairs Shortest Paths
Lecture 39 CSE 331 Dec 9, Announcements Please fill in the online feedback form Sample final has been posted Graded HW 9 on Friday.
1 Dynamic Programming Jose Rolim University of Geneva.
CSE 421 Algorithms Richard Anderson Lecture 21 Shortest Paths.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Advanced Algorithms Piyush Kumar (Lecture 5: Weighted Matching) Welcome to COT5405 Based on Kevin Wayne’s slides.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
CSCI 256 Data Structures and Algorithm Analysis Lecture 4 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Advanced Algorithms Piyush Kumar (Lecture 5: Weighted Matching) Welcome to COT5405 Based on Kevin Wayne’s slides.
1 Algorithmic Paradigms Greed. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer. Break up a problem into.
CSCI 256 Data Structures and Algorithm Analysis Lecture 14 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
1 Chapter 10 Extending the Limits of Tractability Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 8.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 33.
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.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 17.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Introduction to Algorithms Jiafen Liu Sept
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Chapter 9 Finding the Optimum 9.1 Finding the Best Tree.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 12.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 25.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 6.
1 Chapter 5-2 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CS38 Introduction to Algorithms Lecture 10 May 1, 2014.
28.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 18.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 21.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 23.
CSCI 256 Data Structures and Algorithm Analysis Lecture 16 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
CSCI 256 Data Structures and Algorithm Analysis Lecture 10 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CSE 421 Algorithms Richard Anderson Lecture 21 Shortest Paths and Network Flow.
COMP108 Algorithmic Foundations Greedy methods
Chapter 6 Dynamic Programming
Richard Anderson Lecture 20 LCS / Shortest Paths
Chapter 6 Dynamic Programming
Chapter 6 Dynamic Programming
Dynamic Programming Longest Path in a DAG, Yin Tat Lee
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Evaluation of the Course (Modified)
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Richard Anderson Lecture 20 Space Efficient LCS
Memory Efficient Dynamic Programming / Shortest Paths
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 22

Shortest Paths Shortest path problem: Given a directed graph G = (V, E), with edge weights c vw, find shortest path from node s to node t –Ex: Nodes represent agents in a financial setting and c vw is cost of transaction in which we buy from agent v and sell immediately to w s 3 t

Shortest Paths: Failed Attempts Dijkstra: Can fail if negative edge costs Re-weighting: Adding a constant to every edge weight can fail u t sv st A path that starts on an expensive edge, but then compensates with subsequent edges of negative cost, can be cheaper than a path that starts on a cheap edge. Dijskstra-style greedy approach will not work! Unfortunately, changing the costs also changes the minimum-cost path!

Shortest Paths: Negative Cost Cycles Negative cost cycle Observation: If some path from s to t contains a negative cost cycle, there does not exist a shortest s-t path; otherwise, there exists one that is simple. Why? –Shortest paths have at most n-1 edges st W c(W) < 0

Shortest Paths: Dynamic Programming Let’s use DP to solve problem of finding shortest path from s to t when there are negative edge costs but no negative cycles We could try an idea that has worked for us so far: subproblem i could be to find a shortest path using only the first i nodes –This idea does not immediately work, but can be made to work with some effort –We will discuss a simpler and more efficient solution, the Bellman-Ford Algorithm. This algorithm is based on the crucial observation that if G has no negative cycles, then there is a shortest path from s to t that is simple, and hence has at most n- 1 edges

Shortest Paths: Dynamic Programming Def: OPT(i, v) = length of shortest v-t path P using at most i edges –Case 1: P uses at most i-1 edges OPT(i, v) = OPT(i-1, v) –Case 2: P uses exactly i edges if (v, w) is first edge, then OPT uses (v, w), and then selects best w-t path using at most i-1 edges Write final recurrence (DONE IN CLASS) Remark: By previous observation, if no negative cycles, then OPT(n-1, v) = length of shortest v-t path v wt

Shortest Paths: Implementation Shortest-Path (G, s, t) { foreach node v  V Opt[0, v]   Opt[0, t]  0 for i = 1 to n-1 foreach node v  V Compute Opt[i, v] using the recurrence return Opt[n-1, s] } Example (DONE IN CLASS) Analysis: O(n 3 ) time,  (n 2 ) space –Can be implemented in O(mn) time if G does not have too many edges (i.e., G is sparse). Memory requirements can also be reduced to O(n)

Detecting Negative Cycles Lemma: If OPT(n,v) = OPT(n-1,v) for all v, then no negative cycles –Pf: Bellman-Ford algorithm Lemma: If OPT(n,v) < OPT(n-1,v) for some node v, then (any) shortest path from v to t contains a cycle W. Moreover W has negative cost. Pf. (by contradiction) –Since OPT(n,v) < OPT(n-1,v), we know P has exactly n edges –By pigeonhole principle, P must contain a directed cycle W. –Deleting W yields a v-t path with < n edges  W has negative cost v t W c(W) < 0

Detecting Negative Cycles: Application Currency conversion: Given n currencies and exchange rates between pairs of currencies, is there an arbitrage opportunity? –Remark: Fastest algorithm very valuable! F$ £ ¥ DM 1/7 3/10 2/ /50 4/3 8 IBM 1/