Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Dynamic Programming (1). 2 Dynamic Programming The dynamic programming archetype is used to solve problems that can be defined by recurrences with overlapping.

Similar presentations


Presentation on theme: "1 Dynamic Programming (1). 2 Dynamic Programming The dynamic programming archetype is used to solve problems that can be defined by recurrences with overlapping."— Presentation transcript:

1 1 Dynamic Programming (1)

2 2 Dynamic Programming The dynamic programming archetype is used to solve problems that can be defined by recurrences with overlapping subproblems. Invented by mathematician Richard Bellman in the 1950s to solve optimization problems; later co-opted by computer scientists. The word “programming” in the name is not computer programming as we know it - it means “planning”.

3 3 Dynamic Programming The basic idea: –Set up a recurrence of some sort, relating a solution to a large problem instance to one or more solutions of smaller problem instances. –Solve each of the smaller problem instances once. –Record these solutions in a lookup table. –Build the solution to the large instance by looking up the solutions to the smaller instances in the table. Problems that can be solved by dynamic programming are said to exhibit optimal substructure - you can build an optimal solution using optimal solutions to smaller problem instances.

4 4 Optimal Substructure The problem of finding the shortest route to drive between two cities exhibits optimal substructure: –Assume the shortest route from Seattle to Los Angeles passes through Portland and Sacramento. –That means the shortest route from Seattle to Sacramento must also pass through Portland. Solving the smaller problem is part of solving the bigger one. On the other hand, finding the cheapest airfare between two cities does not typically exhibit optimal substructure. –If the cheapest ticket from Seattle to Prague stops in New York and Frankfurt, we usually can’t assume that the cheapest ticket from Seattle to Frankfurt stops in New York.

5 5 Dynamic Programming Example: Fibonacci Numbers

6 6 Xxxx This algorithm is rather inefficient - it’s exponential in n (section 2.5 of the book contains the full analysis). It’s duplicating a lot of work, since Fibonacci(n – 3) is computed twice (once for the n – 1 call and once for the n – 2 call), and results further back are duplicated even more.

7 7 Dynamic Programming Example: Fibonacci Numbers This algorithm performs a linear, not exponential, number of additions. Much nicer. We’re storing only 2 prior results, not all of them - but we did need to calculate all of them.

8 8 Dynamic Programming The reason that Fibonacci numbers are a trivial example is that the optimal substructure is inherent in the definition of the problem. The difficult part of devising most dynamic programming algorithms is finding the optimal substructure in the first place - most problems don’t have obvious, well-structured recurrence relations.

9 9 The Coin-Row Problem There is a row of n coins whose values are positive integers c 1, c 2,..., c n - not necessarily distinct or in any particular order. The goal is to pick up the maximum possible amount of money without picking up any two adjacent coins. Example: What is the best solution for the list of coins {10, 3, 5, 9, 1, 1, 6}? After looking at it for a while, we can see that the best we can do is 25 - taking the 10, the 9, and the 6 and skipping the 3, the 5, and the 1, 1. But what’s an algorithm for this?

10 10 The Coin-Row Problem

11 11 Making Change

12 12 Making Change Complexity: Θ(n) space, Θ(mn) time

13 13 Multi-Dimension So far, the problems we’ve looked at have had onedimensional structure - to find the solution for n we look back to the solutions for previous values of n. We can also solve problems with 2 (or 3, or k) dimensions.

14 14 Robot Coin Collection Consider an n * m grid, where some cells have coins in them and others do not. A robot starts at the upper-left corner, can move only one cell right or one cell down at every step, and picks up every coin it encounters. The goal: collect the most coins possible by the time the robot gets to the lower-right corner of the board.

15 15 Robot Coin Collection Here’s an example board: what’s the best path through it? The best path gets 5 coins; there are choices for where to turn, but on this board we must take those 5 coins

16 16 Robot Coin Collection

17 17 Robot Coin Collection

18 18 Robot Coin Collection Complexity? Θ(nm) time, Θ(nm) space

19 19 Robot Coin Collection How can we calculate the maximum number of coins? We can proceed in 2 dimensions... Each cell represent the maximum # of coins the robot can collect after it arrives in the particular cell.

20 20 Robot Coin Collection How can we calculate the maximum number of coins? We could also do this one row or column at a time, rather than simultaneously in two dimensions...

21 21 Robot Coin Collection How can we calculate the maximum number of coins? We could also do this one row or column at a time, rather than simultaneously in two dimensions...

22 22 Robot Coin Collection How can we calculate the maximum number of coins? We could also do this one row or column at a time, rather than simultaneously in two dimensions...

23 23 Robot Coin Collection How can we calculate the maximum number of coins? We could also do this one row or column at a time, rather than simultaneously in two dimensions...

24 24 Robot Coin Collection How can we calculate the maximum number of coins? We could also do this one row or column at a time, rather than simultaneously in two dimensions...

25 25 Robot Coin Collection How can we calculate the maximum number of coins? We could also do this one row or column at a time, rather than simultaneously in two dimensions...

26 26 Robot Coin Collection How can we calculate the maximum number of coins? We can proceed in 2 dimensions...

27 27 Robot Coin Collection How can we calculate the maximum number of coins? We can proceed in 2 dimensions...

28 28 Robot Coin Collection How can we calculate the maximum number of coins? We can proceed in 2 dimensions...

29 29 Robot Coin Collection How can we calculate the maximum number of coins? We can proceed in 2 dimensions...

30 30 Robot Coin Collection How can we calculate the maximum number of coins? We can proceed in 2 dimensions...

31 31 Robot Coin Collection How can we calculate the maximum number of coins? We can proceed in 2 dimensions...

32 32 Path Counting Consider the problem of counting the number of shortest paths from point A to point B in a city with perfectly horizontal streets and vertical avenues (0,0) (m,n)(m-1,n) (m,n-1) (0,n) (m,0) F(m,n) = F(m-1,n)+ F(m,n-1) for n > 1, F(0,0) = 0, F(0,i)= F(j,0)=1 0<j<=m, 0<i<=n


Download ppt "1 Dynamic Programming (1). 2 Dynamic Programming The dynamic programming archetype is used to solve problems that can be defined by recurrences with overlapping."

Similar presentations


Ads by Google