Dynamic Programming. CSE 101: Design and Analysis of Algorithms Lecture 19

Similar documents
1 Non greedy algorithms (which we should have covered

Chapter 6. Dynamic Programming

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

Last week: Breadth-First Search

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Artificial Intelligence

Dynamic Programming II

CS 125 Section #10 Midterm 2 Review 11/5/14

Shortest Paths. Nishant Mehta Lectures 10 and 11

The Shortest Path Problem

Shortest Paths. Nishant Mehta Lectures 10 and 11

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

EDIT DISTANCE. Given two words (strings), how can we define a notion of closeness. For example: Is PELICAN closer to PENGUIN or POLITICIAN?

Shortest Path Problem

11. APPROXIMATION ALGORITHMS

Single Source Shortest Path (SSSP) Problem

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Homework 2 Solutions

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Homework 4 Solutions

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

CSE 417 Branch & Bound (pt 4) Branch & Bound

Chapter 9 Graph Algorithms

Shortest Paths. CSE 373 Data Structures Lecture 21

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Addis Ababa University, Amist Kilo July 20, 2011 Algorithms and Programming for High Schoolers. Lecture 12

Algorithms for Data Science

Chapter 9 Graph Algorithms

Unit-5 Dynamic Programming 2016

Weighted Graph Algorithms Presented by Jason Yuan

CSE 417 Network Flows (pt 4) Min Cost Flows

1 More on the Bellman-Ford Algorithm

1 Dynamic Programming

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CS 170 DISCUSSION 8 DYNAMIC PROGRAMMING. Raymond Chan raychan3.github.io/cs170/fa17.html UC Berkeley Fall 17

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Data Structure and Algorithm Homework #5 Due: 2:00pm, Thursday, May 31, 2012 TA === Homework submission instructions ===

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Graph Algorithms (part 3 of CSC 282),

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

LEC13: SHORTEST PATH. CSE 373 Analysis of Algorithms Fall 2016 Instructor: Prof. Sael Lee. Lecture slide courtesy of Prof.

CS483 Design and Analysis of Algorithms

Theorem 2.9: nearest addition algorithm

Announcements. CSEP 521 Applied Algorithms. Announcements. Polynomial time efficiency. Definitions of efficiency 1/14/2013

Analysis of Algorithms, I

CS599: Convex and Combinatorial Optimization Fall 2013 Lecture 14: Combinatorial Problems as Linear Programs I. Instructor: Shaddin Dughmi

Graph Algorithms (part 3 of CSC 282),

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Algorithms for Data Science

Longest Common Subsequence, Knapsack, Independent Set Scribe: Wilbur Yang (2016), Mary Wootters (2017) Date: November 6, 2017

A4B33ALG 2015/10 ALG 10. Dynamic programming. abbreviation: DP. Sources, overviews, examples see.

CS521 \ Notes for the Final Exam

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Satish Rao September 18, Midterm 1

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

Dynamic Programming Algorithms

Dynamic Programming Algorithms

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Lecture 3, Review of Algorithms. What is Algorithm?

Shortest path problems

Final Exam Solutions

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CMSC Introduction to Algorithms Spring 2012 Lecture 16

CS 561, Lecture 10. Jared Saia University of New Mexico

Total Points: 60. Duration: 1hr

L.J. Institute of Engineering & Technology Semester: VIII (2016)

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Bulldozers/Sites A B C D

11. APPROXIMATION ALGORITHMS

Lecture 18 Solving Shortest Path Problem: Dijkstra s Algorithm. October 23, 2009

END-TERM EXAMINATION

Design and Analysis of Algorithms

Recitation 12. Dynamic Programming Announcements. SegmentLab has been released and is due Apr 14.

FINAL EXAM SOLUTIONS

Solving problems on graph algorithms

EE/CSCI 451 Spring 2018 Homework 8 Total Points: [10 points] Explain the following terms: EREW PRAM CRCW PRAM. Brent s Theorem.

Design and Analysis of Algorithms


Design and Analysis of Algorithms

PATH FINDING AND GRAPH TRAVERSAL

Subset sum problem and dynamic programming

Approximation Algorithms

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Problem Score Maximum MC 34 (25/17) = 50 Total 100

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

11. APPROXIMATION ALGORITHMS

Algorithms Assignment 3 Solutions

Dynamic programming. Trivial problems are solved first More complex solutions are composed from the simpler solutions already computed

A BRIEF INTRODUCTION TO DYNAMIC PROGRAMMING (DP) by Amarnath Kasibhatla Nanocad Lab University of California, Los Angeles 04/21/2010

More Graph Algorithms: Topological Sort and Shortest Distance

Transcription:

Dynamic Programming CSE 101: Design and Analysis of Algorithms Lecture 19

CSE 101: Design and analysis of algorithms Dynamic programming Reading: Chapter 6 Homework 7 is due Dec 6, 11:59 PM This Friday s discussion section is the final exam review No instructor, TA, or tutor office hours during finals week. Ask questions on Piazza. Final exam is Friday, Dec 14, 7:00 PM-9:59 PM Practice problems released this Friday CSE 101, Fall 2018 2

Dynamic Programming Dynamic programming is an algorithmic paradigm in which a problem is solved by identifying a collection of subproblems and tackling them one by one, smallest first, using the answers to small problems to help figure out larger ones, until they are all solved Examples: Based on slides courtesy of Miles Jones CSE 101, Fall 2018 3

The knapsack problem Suppose you are a burglar who breaks into a store and you want to leave with the maximum value of items Example: your knapsack can only hold 13 lbs and the items in the store are Silver 4 lbs jewery Diamond jacket Golden suitcases Platinum Nintendo Solid ruby bowling ball Value 4 9 12 15 19 21 Weight 2 4 5 7 8 9 CSE 101, Fall 2018 4

The knapsack problem What is the maximum value you can have from a list of items a[1],,a[n] where each item has a value v[i] and a weight w[i] given that you cannot have more weight than W CSE 101, Fall 2018 5

The knapsack problem, algorithm Knapsack(w[1 n],v[1 n],w) KS(j,0):=0 for all j KS(0,w):=0 for all w for w from 1 to W for j from 1 to n Base cases Ordering KS(j,w):= max(ks(j,w-w(j)),ks(j-1,w)) Return KS(n,W) CSE 101, Fall 2018 6

The knapsack problem, runtime Knapsack(w[1 n],v[1 n],w) KS(j,0):=0 for all j KS(0,w):=0 for all w for w from 1 to W for j from 1 to n W times Total runtime O(nW) n times KS(j,w):= max(ks(j,w-w(j)),ks(j-1,w)) Return KS(n,W) CSE 101, Fall 2018 7

The knapsack problem, runtime We just showed that the dynamic programming runtime is O(nW) Reasonable when W is small, but is exponential in the input size For example, W = 75 = 1001011 2 2 7, so an input size of 7 must do 75 operations Brute force is 2 n (also exponential) It is unknown whether or not there is a polynomial algorithm to this problem Interesting variant: if integers are encoded in unary (e.g., 12 = 111111111111 1 ), then there is a polynomial algorithm CSE 101, Fall 2018 8

Dynamic programming SHORTEST PATHS CSE 101, Fall 2018 9

Shortest path We have a breadth-first search style algorithm for finding shortest paths in graphs with positive edge weights Dijkstra s algorithm But, what if there are negative edge weights? For example, what if edges represent bank transactions, where deposits are positive and withdrawals are negative (or gains and losses in altitude on a flight)? If there are negative cycles, then shortest path is not defined, because we can cycle around arbitrarily, getting more and more negative 4 For example -3 CSE 101, Fall 2018 10-2

What about graphs with no cycles? First, let s try to solve shortest paths for directed acyclic graphs with some negative edges We are still trying to minimize the sum of the edge weights along a path CSE 101, Fall 2018 11

Shortest path in directed acyclic graphs 1 2 A B C Example 2 3 2 5 4 D 1 E F CSE 101, Fall 2018 12

Shortest path in directed acyclic graphs 1 2 A B C Example 2 3 2 5 4 D 1 E F Topological sort 2 D 1 E 3 A 1 B 2 C 4 F 2 5 CSE 101, Fall 2018 13

Shortest path in directed acyclic graphs 2 D 1 E 3 A 1 B 2 C 4 F 2 5 Shortest distance from node D to another node x will be denoted dist(x) How do we determine the shortest distance from D to C? The shortest distance from D to C is dist(c) = min(dist(e) + 5, dist(b) + 2) This kind of relation can be written for every node Since it is a directed acyclic graph, the arrows only go to the right, so by the time we get to node x, we have all the information needed CSE 101, Fall 2018 14

Shortest path in directed acyclic graphs 2 D 1 E 3 A 1 B 2 C 4 F 2 5 Shortest distance from node D to another node x will be denoted dist(x) How do we determine the shortest distance from D to C? The shortest distance from D to C is dist(c) = min(dist(e) + 5, dist(b) + 2) This kind of relation can be written for every node Since it is a directed acyclic graph, the arrows only go to the right, so by the time we get to node x, we have all the information needed CSE 101, Fall 2018 15

Shortest path in directed acyclic graphs 2 D 1 E 3 A 1 B 2 C 4 F 2 5 Shortest distance from node D to another node x will be denoted dist(x) How do we determine the shortest distance from D to C? The shortest distance from D to C is dist(c) = min(dist(e) + 5, dist(b) + 2) This kind of relation can be written for every node Since it is a directed acyclic graph, the arrows only go to the right, so by the time we get to node x, we have all the information needed CSE 101, Fall 2018 16

Shortest path in directed acyclic graphs 2 D 1 E 3 A 1 B 2 C 4 F 2 5 Shortest distance from node D to another node x will be denoted dist(x) How do we determine the shortest distance from D to C? The shortest distance from D to C is dist(c) = min(dist(e) + 5, dist(b) + 2) This kind of relation can be written for every node Since it is a directed acyclic graph, the arrows only go to the right, so by the time we get to node x, we have all the information needed CSE 101, Fall 2018 17

Dynamic programming steps Step 1: Define subproblems and corresponding array Step 2: What are the base cases? Step 3: Give recursion for subproblems Step 4: Find bottom-up order Step 5: What is the final output? Step 6: Put it all together into an iterative algorithm that fills in the array step by step For analysis: Step 7: Correctness proof Step 8: Runtime analysis CSE 101, Fall 2018 19

Dynamic programming steps Step 0: Define the problem Step 1: Define subproblems and corresponding array Step 2: What are the base cases? Step 3: Give recursion for subproblems Step 4: Find bottom-up order Step 5: What is the final output? Step 6: Put it all together into an iterative algorithm that fills in the array step by step CSE 101, Fall 2018 20

Shortest path in directed acyclic graphs Step 0: Define the problem Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, where 1 s n, output the shortest distance from s to v 1,, v n Step 1: Define subproblems and corresponding array Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, output the shortest distance from s to v 1,, v i Let d i = dist(v i ) the shortest distance from s to v i CSE 101, Fall 2018 21

Shortest path in directed acyclic graphs Step 0: Define the problem Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, where 1 s n, output the shortest distance from s to v 1,, v n Step 1: Define subproblems and corresponding array Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, output the shortest distance from s to v 1,, v i Let d i = dist(v i ) the shortest distance from s to v i CSE 101, Fall 2018 22

Shortest path in directed acyclic graphs Step 0: Define the problem Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, where 1 s n, output the shortest distance from s to v 1,, v n Step 1: Define subproblems and corresponding array Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, output the shortest distance from s to v 1,, v i Let d i = dist(v i ) the shortest distance from s to v i CSE 101, Fall 2018 23

Shortest path in directed acyclic graphs Step 0: Define the problem Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, where 1 s n, output the shortest distance from s to v 1,, v n Step 1: Define subproblems and corresponding array Given a directed acyclic graph with vertices v 1,, v n in topological order and starting vertex s, output the shortest distance from s to v 1,, v i Let d i = dist(v i ) the shortest distance from s to v i CSE 101, Fall 2018 25

Shortest path in directed acyclic graphs Step 2: What are the base cases? dist s = 0, dist v = if v s Step 3: Give recursion for subproblems What is the second to last vertex in path to v i? Let s call it vertex u dist v i = min dist u + w(u, v i) (u,v i ) E CSE 101, Fall 2018 26

Shortest path in directed acyclic graphs Step 2: What are the base cases? dist s = 0, dist v = if v s Step 3: Give recursion for subproblems What is the second to last vertex in path to v i? Let s call it vertex u dist v i = min dist u + w(u, v i) (u,v i ) E CSE 101, Fall 2018 27

Shortest path in directed acyclic graphs Step 2: What are the base cases? dist s = 0, dist v = if v s Step 3: Give recursion for subproblems What is the second to last vertex in path to v i? Let s call it vertex u dist v i = min dist u + w(u, v i) (u,v i ) E CSE 101, Fall 2018 28

Shortest path in directed acyclic graphs Step 2: What are the base cases? dist s = 0, dist v = if v s Step 3: Give recursion for subproblems What is the second to last vertex in path to v i? Let s call it vertex u dist v i = min dist u + w(u, v i) (u,v i ) E CSE 101, Fall 2018 30

Shortest path in directed acyclic graphs Step 2: What are the base cases? dist s = 0, dist v = if v s Step 3: Give recursion for subproblems What is the second to last vertex in path to v i? Let s call it vertex u dist v i = min dist u + w(u, v i) (u,v i ) E CSE 101, Fall 2018 31

Shortest path in directed acyclic graphs Step 2: What are the base cases? dist s = 0, dist v = if v s Step 3: Give recursion for subproblems What is the second to last vertex in path to v i? Let s call it vertex u dist v i = min dist u + w(u, v i) (u,v i ) E CSE 101, Fall 2018 32

Shortest path in directed acyclic graphs Step 4: Find bottom-up order Topological order Step 5: What is the final output? [dist v 1,, dist v n ] CSE 101, Fall 2018 33

Shortest path in directed acyclic graphs Step 4: Find bottom-up order Topological order Step 5: What is the final output? [dist v 1,, dist v n ] CSE 101, Fall 2018 34

Shortest path in directed acyclic graphs Step 4: Find bottom-up order Topological order Step 5: What is the final output? [dist v 1,, dist v n ] CSE 101, Fall 2018 35

Shortest path in directed acyclic graphs Step 4: Find bottom-up order Topological order Step 5: What is the final output? [dist v 1,, dist v n ] CSE 101, Fall 2018 37

Shortest path in directed acyclic graphs Step 6: Put it all together into an iterative algorithm that fills in the array step by step initialize all dist(.) values to infinity dist(s):=0 for each v V\{s} in topological order dist(v) = min (u,v) E dist u + w u, v CSE 101, Fall 2018 38

Shortest path in directed acyclic graphs Step 6: Put it all together into an iterative algorithm that fills in the array step by step initialize all dist(.) values to infinity dist(s):=0 for each v V\{s} in topological order dist(v) = min (u,v) E dist u + w u, v CSE 101, Fall 2018 39

Example: Longest increasing subsequence Given a sequence of distinct positive integers a[1],,a[n], an increasing subsequence is a sequence a[i 1 ],,a[i k ] such that i 1 < < i k and a[i 1 ]< <a[i k ] Example: 15, 18, 8, 11, 5, 12, 16, 2, 20, 9, 10, 4 5, 16, 20 is an increasing subsequence How long is the longest increasing subsequence? 8, 11, 12, 16, 20 CSE 101, Fall 2018 40

Longest increasing subsequence Given a sequence of distinct positive integers a[1],,a[n], an increasing subsequence is a sequence a[i 1 ],,a[i k ] such that i 1 < < i k and a[i 1 ]< <a[i k ] Example: 15, 18, 8, 11, 5, 12, 16, 2, 20, 9, 10, 4 5, 16, 20 is an increasing subsequence How long is the longest increasing subsequence? 8, 11, 12, 16, 20 CSE 101, Fall 2018 41

Longest increasing subsequence Given a sequence of distinct positive integers a[1],,a[n], an increasing subsequence is a sequence a[i 1 ],,a[i k ] such that i 1 < < i k and a[i 1 ]< <a[i k ] Example: 15, 18, 8, 11, 5, 12, 16, 2, 20, 9, 10, 4 5, 16, 20 is an increasing subsequence How long is the longest increasing subsequence? 8, 11, 12, 16, 20 CSE 101, Fall 2018 42

Viewing longest increasing subsequence as shortest path in directed acyclic graph What could the vertices be? Positions in the array, and 0 When is there an edge? There is an edge from I to J if A[I] < A[J] or I=0 What are the weights of edges? -1 CSE 101, Fall 2018 43

Viewing longest increasing subsequence as shortest path in directed acyclic graph What could the vertices be? Positions in the array, and 0 When is there an edge? There is an edge from I to J if A[I] < A[J] or I=0 What are the weights of edges? -1 CSE 101, Fall 2018 44

Viewing longest increasing subsequence as shortest path in directed acyclic graph What could the vertices be? Positions in the array, and 0 When is there an edge? There is an edge from I to J if A[I] < A[J] or I=0 What are the weights of edges? -1 CSE 101, Fall 2018 45

Viewing longest increasing subsequence as shortest path in directed acyclic graph What could the vertices be? Positions in the array, and 0 When is there an edge? There is an edge from I to J if A[I] < A[J] or I=0 What are the weights of edges? -1 CSE 101, Fall 2018 47

Viewing longest increasing subsequence as shortest path in directed acyclic graph What could the vertices be? Positions in the array, and 0 When is there an edge? There is an edge from I to J if A[I] < A[J] or I=0 What are the weights of edges? -1 CSE 101, Fall 2018 48

Viewing longest increasing subsequence as shortest path in directed acyclic graph What could the vertices be? Positions in the array, and 0 When is there an edge? There is an edge from I to J if A[I] < A[J] or I=0 What are the weights of edges? -1 CSE 101, Fall 2018 50

Directed acyclic graphs are canonical for dynamic programming Consider a graph whose vertices are the distinct recursive calls an algorithm makes and where calls are edges from the subproblem to the main problem This graph had better be a directed acyclic graph or we are in deep trouble This graph should be small or dynamic programming will not help much Bottom-up order = topological sort CSE 101, Fall 2018 51

What about graphs with cycles? What if the graph has both negative weights and cycles? If the graph has negative cycles, some distances are negative infinity. Otherwise, dist C = min v v,c is an edge dist v + w v, C But, this does not give a consistent recursion, because the recursion graph is not a directed acyclic graph, so recursion loops around cycles How can we make recursion acyclic when the graph is cyclic? When would we even want to do this? CSE 101, Fall 2018 52

What about graphs with cycles? What if the graph has both negative weights and cycles? If the graph has negative cycles, some distances are negative infinity. Otherwise, dist C = min v v,c is an edge dist v + w v, C But, this does not give a consistent recursion, because the recursion graph is not a directed acyclic graph, so recursion loops around cycles How can we make recursion acyclic when the graph is cyclic? When would we even want to do this? CSE 101, Fall 2018 53

What about graphs with cycles? What if the graph has both negative weights and cycles? If the graph has negative cycles, some distances are negative infinity. Otherwise, dist C = min v v,c is an edge dist v + w v, C But, this does not give a consistent recursion, because the recursion graph is not a directed acyclic graph, so recursion loops around cycles How can we make recursion acyclic when the graph is cyclic? When would we even want to do this? CSE 101, Fall 2018 54

What about graphs with cycles? What if the graph has both negative weights and cycles? If the graph has negative cycles, some distances are negative infinity. Otherwise, dist C = min v v,c is an edge dist v + w v, C But, this does not give a consistent recursion, because the recursion graph is not a directed acyclic graph, so recursion loops around cycles How can we make recursion acyclic when the graph is cyclic? When would we even want to do this? CSE 101, Fall 2018 55

Example: exchange rates We usually deal with exchange rates as being determined so that you always lose a bit when exchanging one currency for another CSE 101, Fall 2018 57

Exchange rates But, there is actually no central authority that sets exchange rates. They are actually set by a market where individual buyers and sellers make offers to trade with different prices, sometimes varying from the current rate. This allows currencies to float, changing relative values as market conditions change This means the best way to change one currency for another might involve a sequence of trades, rather than just pick the one best rate CSE 101, Fall 2018 58

Model as a graph Exchange rates Vertices = currencies Edges = bids for trades Edge weight = exchange rate bid CSE 101, Fall 2018 59

Objective function How good is a sequence of trades? What function are we trying to maximize or minimize? What is the best thing that could happen for us? CSE 101, Fall 2018 60

Comparing objective functions We are trying to find the path that maximizes the product of the edge weights along a path What would be really good is if there were a cycle whose product was bigger than 1 This is called an arbitrage, and is a legal way to make money for free. But, they only last a short time. What we know how to do: Shortest path problem: Minimize the sum of the edge weights How can we map one to the other? What function would map a product to a sum? CSE 101, Fall 2018 61

Comparing objective functions We are trying to find the path that maximizes the product of the edge weights along a path What would be really good is if there were a cycle whose product was bigger than 1 This is called an arbitrage, and is a legal way to make money for free. But, they only last a short time. What we know how to do Shortest path problem: minimize the sum of the edge weights How can we map one to the other? What function would map a product to a sum? CSE 101, Fall 2018 62

Modifying the graph Define w e = log w e Then Σ e path w e = log Π e path w e Thus, the smallest sum of w (e) is the largest product of w(e) = best trades CSE 101, Fall 2018 63

The revised goal Revising the goal If there is any negative cycle in w, then find one such negative cycle (arbitrage!) If there is not a negative cycle, then find the best exchange rate from US dollars to every other currency = shortest path CSE 101, Fall 2018 64

Preventing infinite recursion Let s put a budget T on how deep we will recurse This corresponds to looking at paths of length at most T CSE 101, Fall 2018 65

Bound the number of edges Given a graph with vertices v 0, v 1, v 2,, v n, a starting vertex v 0, and weighted edges (possibly negative), give the shortest path from v 0 to each other vertex v 1,, v n or determine if there is a negative cycle Step 0: Define the problem Bound the number of edges Given a graph with vertices v 0, v 1, v 2,, v n, a starting vertex v 0, and weighted edges (possibly negative), give the shortest path from v 0 to each other vertex v 1,, v n that has T or fewer edges CSE 101, Fall 2018 66

Bound the number of edges Given a graph with vertices v 0, v 1, v 2,, v n, a starting vertex v 0, and weighted edges (possibly negative), give the shortest path from v 0 to each other vertex v 1,, v n or determine if there is a negative cycle Step 0: Define the problem Bound the number of edges Given a graph with vertices v 0, v 1, v 2,, v n, a starting vertex v 0, and weighted edges (possibly negative), give the shortest path from v 0 to each other vertex v 1,, v n that has T or fewer edges CSE 101, Fall 2018 67

Bound the number of edges Step 0: Define the problem Given a graph with vertices v 0, v 1, v 2,, v n, a starting vertex v 0, and weighted edges (possibly negative), give the shortest path from v 0 to each other vertex v 1,, v n that has T or fewer edges Step 1: Define subproblems and corresponding array Let BF[i,t] by the shortest path from v 0 to v i with t or fewer edges CSE 101, Fall 2018 68

Bound the number of edges Step 0: Define the problem Given a graph with vertices v 0, v 1, v 2,, v n, a starting vertex v 0, and weighted edges (possibly negative), give the shortest path from v 0 to each other vertex v 1,, v n that has T or fewer edges Step 1: Define subproblems and corresponding array Let B[i,t] by the shortest path from v 0 to v i with t or fewer edges CSE 101, Fall 2018 70

Bound the number of edges Step 2: What are the base cases? B[0,0] = 0 B[i,0] = for i 0 CSE 101, Fall 2018 71

Bound the number of edges Step 2: What are the base cases? B[0,0] = 0 B[i,0] = for i 0 CSE 101, Fall 2018 72

Bound the number of edges Step 3: Give recursion for subproblems What simple question can we ask to split up the possible paths from v 0 to v i? What is the second to last vertex in the shortest path from v 0 to v i (that uses at most t edges)? B i, t = min B 0, t 1 + w v 0, v i B n, t 1 + w v n, v i = min (v j,v i ) E B j, t 1 + w v j, v i CSE 101, Fall 2018 73

Bound the number of edges Step 3: Give recursion for subproblems What simple question can we ask to split up the possible paths from v 0 to v i? What is the second to last vertex in the shortest path from v 0 to v i (that uses at most t edges)? B i, t = min B 0, t 1 + w v 0, v i B n, t 1 + w v n, v i = min (v j,v i ) E B j, t 1 + w v j, v i CSE 101, Fall 2018 74

Bound the number of edges Step 3: Give recursion for subproblems What simple question can we ask to split up the possible paths from v 0 to v i? What is the second to last vertex in the shortest path from v 0 to v i (that uses at most t edges)? B i, t = min v 0 B 0, t 1 + w v 0, v i B n, t 1 + w v n, v i = min (v j,v i ) E B j, t 1 + w v j, v i v 1 v 4 v n v i CSE 101, Fall 2018 75

Bound the number of edges Step 3: Give recursion for subproblems What simple question can we ask to split up the possible paths from v 0 to v i? What is the second to last vertex in the shortest path from v 0 to v i (that uses at most t edges)? B i, t = min B 0, t 1 + w v 0, v i B n, t 1 + w v n, v i = min B j, t 1 + w v j, v i, B[i, t 1] (v j,v i ) E v 0 v j v i CSE 101, Fall 2018 76

Bound the number of edges Step 3: Give recursion for subproblems What simple question can we ask to split up the possible paths from v 0 to v i? What is the second to last vertex in the shortest path from v 0 to v i (that uses at most t edges)? B i, t = min B 0, t 1 + w v 0, v i B n, t 1 + w v n, v i = min B j, t 1 + w v j, v i, B[i, t 1] (v j,v i ) E v 0 v j v i The shortest path from v 0 to v j The shortest path from v 0 to v i CSE 101, Fall 2018 77

B i, t = Bound the number of edges min B j, t 1 + w v j, v i, B[i, t 1] (v j,v i ) E Step 4: Find bottom-up order You must do the t values in increasing order from t=0,1,2,,n The i values can be done in any order CSE 101, Fall 2018 78

B i, t = Bound the number of edges min B j, t 1 + w v j, v i, B[i, t 1] (v j,v i ) E Step 4: Find bottom-up order You must do the t values in increasing order from t=0,1,2,,n The i values can be done in any order CSE 101, Fall 2018 80

Bound the number of edges Step 5: What is the final output? Since we are doing the bounded problem, then we output the array [B 0, T, B 1, T, B 2, T, B 3, T,, B n, T ] Can we use this to detect negative cycles? CSE 101, Fall 2018 81

Bound the number of edges Step 5: What is the final output? Since we are doing the bounded problem, then we output the array [B 0, T, B 1, T, B 2, T, B 3, T,, B n, T ] Can we use this to detect negative cycles? CSE 101, Fall 2018 82

Bound the number of edges Step 5: What is the final output? Since we are doing the bounded problem, then we output the array [B 0, T, B 1, T, B 2, T, B 3, T,, B n, T ] Can we use this to detect negative cycles? CSE 101, Fall 2018 83

Bound the number of edges Preventing infinite recursion If there are no negative cycles: Then all shortest paths are simple (i.e., do not repeat vertices) So, the shortest path is of length at most V -1 = n-1 So, the array values will never improve after t gets bigger than n-1 (i.e., B(i,n-1)=B(i,n)=B(i,n+1)= ) CSE 101, Fall 2018 84

Bound the number of edges Preventing infinite recursion If there are no negative cycles: Then all shortest paths are simple (i.e., do not repeat vertices) So, the shortest path has at most V -1 = n-1 edges So, the array values will never improve after t gets bigger than n-1 (i.e., B(i,n-1)=B(i,n)=B(i,n+1)= ) CSE 101, Fall 2018 85

Bound the number of edges Preventing infinite recursion If there are no negative cycles: Then all shortest paths are simple (i.e., do not repeat vertices) So, the shortest path has at most V -1 = n-1 edges So, the array values will never improve after t gets bigger than n-1 (i.e., B(i,n-1)=B(i,n)=B(i,n+1)= ) CSE 101, Fall 2018 86

Bound the number of edges Preventing infinite recursion This means that if there exists an i such that B i, n 1 B i, n, then there is a negative cycle! So, to solve the main problem, compute all values of B i, n 1 and then, as you are computing B i, n, if anything changes, conclude that there is a negative cycle CSE 101, Fall 2018 87

Bellman-Ford algorithm BellmanFord(G=(V,E), V=v 0, v 1,, v n, starting vertex v 0, edgeweights w e ) If there is a negative cycle, then output negative cycle!!!! ; otherwise, output the shortest lengths of paths to each vertex from v 0 CSE 101, Fall 2018 88

Bellman-Ford algorithm BellmanFord(G=(V,E), V=v 0, v 1,, v n, starting vertex v 0, edgeweights w e ) Initialize BF(i,t)= for all 0 i, t n BF(0,0)=0 for t = 1,, n: for i = 0,, n: BF i, t = min v j,v i E BF j, t 1 + w v j, v i,[bf(i, t-1)] for i = 0,, n: if BF i, n 1 BF i, n : return negative cycle!!!! return [BF(0,n),BF(1,n),BF(2,n),,BF(n,n)] CSE 101, Fall 2018 89

Bellman-Ford algorithm, runtime BellmanFord(G=(V,E), V=v 0, v 1,, v n, starting vertex v 0, edgeweights w e ) Initialize BF(i,t)= for all 0 i, t n BF(0,0)=0 for t = 1,, n: n times for i = 0,, n: n + m times BF i, t = min BF j, t 1 + w v j, v i v j,v i E for i = 0,, n: if BF i, n 1 BF i, n : return negative cycle!!!! return [BF(0,n),BF(1,n),BF(2,n),,BF(n,n)],[BF(i, t-1)] Total runtime O(n(n + m)) (if connected graph, then O(nm)) CSE 101, Fall 2018 90

Floyd-Warshall algorithm Suppose you wanted the shortest path between all pairs of vertices You could run Bellman Ford n times for a total runtime of O n 2 m Floyd-Warshall accomplishes this in O n 3 time! CSE 101, Fall 2018 91

Floyd-Warshall algorithm Step 1: Define subproblems and corresponding array Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 2: What are the base cases? FW(i, j, 0) = w(i, j) CSE 101, Fall 2018 92

Floyd-Warshall algorithm Step 1: Define subproblems and corresponding array Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 2: What are the base cases? FW(i, j, 0) = w(i, j) CSE 101, Fall 2018 93

Floyd-Warshall algorithm Step 1: Define subproblems and corresponding array Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 2: What are the base cases? FW(i, j, 0) = w(i, j) CSE 101, Fall 2018 95

Floyd-Warshall algorithm Step 1: Define subproblems and corresponding array Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 2: What are the base cases? FW(i, j, 0) = w(i, j) CSE 101, Fall 2018 96

Floyd-Warshall algorithm Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 3: Give recursion for subproblems What can we ask about FW(i, j, t)? Does going through v t improve the length? FW i, j, t = min [FW i, j, t 1, FW i, t, t 1 + FW(t, j, t 1)] CSE 101, Fall 2018 97

Floyd-Warshall algorithm Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 3: Give recursion for subproblems What can we ask about FW(i, j, t)? Does going through v t improve the length? FW i, j, t = min [FW i, j, t 1, FW i, t, t 1 + FW(t, j, t 1)] CSE 101, Fall 2018 99

Floyd-Warshall algorithm Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 3: Give recursion for subproblems What can we ask about FW(i, j, t)? Does going through v t improve the length? FW i, j, t = min [FW i, j, t 1, FW i, t, t 1 + FW(t, j, t 1)] v t v i v j CSE 101, Fall 2018 100

Floyd-Warshall algorithm Let FW(i, j, t) be the shortest path from v i to v j using only the vertices {v 1,, v t } as intermediate vertices Step 3: Give recursion for subproblems What can we ask about FW(i, j, t)? Does going through v t improve the length? FW i, j, t = min [FW i, j, t 1, FW i, t, t 1 + FW(t, j, t 1)] v t See traveling salesperson problem in textbook for more details v i v j CSE 101, Fall 2018 101

Next lecture NP-complete problems Reading: Chapter 8 CSE 101, Fall 2018 102