comp 3403 algorithm analysis part 4 chapter 8
play

COMP 3403 Algorithm Analysis Part 4 Chapter 8 Jim Diamond CAR - PowerPoint PPT Presentation

COMP 3403 Algorithm Analysis Part 4 Chapter 8 Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University Chapter 8 Dynamic Programming Jim Diamond, Jodrey School of Computer Science, Acadia University Chapter 8 128


  1. COMP 3403 — Algorithm Analysis Part 4 — Chapter 8 Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

  2. Chapter 8 Dynamic Programming Jim Diamond, Jodrey School of Computer Science, Acadia University

  3. Chapter 8 128 Dynamic Programming: Introduction • The word “programming” here refers to the concept of “planning”, rather than the concept of “coding in a computer language” • Idea: we have seen that it is a common idea to break down a larger problem into sub-problems – • Example: consider F ( n ) = F ( n − 1) + F ( n − 2) the two sub-problems overlap, since to calculate F ( n − 1) we will – need to calculate F ( n − 2) (which in this particular case is the entirety of the second sub-problem) if we choose the “obvious” recursive implementation of F ( n ) , the – number of sub-problems solved is exponential in n (!) – – the obvious recursive algorithm is very, very inefficient Jim Diamond, Jodrey School of Computer Science, Acadia University

  4. Chapter 8 129 Dynamic Programming for Mr. Fibonacci • As mentioned, the “obvious” recursive algorithm to compute Fibonacci numbers is horribly inefficient • A dynamic programming approach would arrange the calculations so that no sub-problem is solved more than once • Two approaches: – bottom-up: compute in the following order: F (0) = 0 , F (1) = 1 F (2) = 1 + 0 = 1 F (3) = 1 + 1 = 2 The iterative approach using an array · · · F ( n ) = F ( n − 1) + F ( n − 2) – top-down: record the solution to each sub-problem when calculated; when the solution to a sub-problem is desired, see if the particular sub-problem has already been solved so called “memory functions” • Top-down may be more efficient for some problems, since the solution to some “smaller” sub-problems may not be required Jim Diamond, Jodrey School of Computer Science, Acadia University

  5. Chapter 8 130 Dynamic Programming: General Concept • Recall: many problem solving techniques involve – – solving each of the sub-problems, and – assembling the solutions to the sub-problems into a solution of the big problem • In some problems (such as calculating F ( n ) ) the sub-problems may “overlap” and/or themselves have sub-problems in common – • Idea: store the solution to a given sub-problem in a table the first time it is computed – • The “trick” to using dynamic programming is to figure out – what the overlapping/repeated calculations are, and – how to arrange the calculations so as to avoid repeatedly solving the same sub-problem(s) Jim Diamond, Jodrey School of Computer Science, Acadia University

  6. Chapter 8 131 Example: Binomial Coefficients • The binomial coefficients are the coefficients of the binomial formula: � � � � � � n n n ( a + b ) n = a n b 0 + · · · + a n − k b k + · · · + a 0 b n 0 k n Recurrence: � = � = 1 � n � n for n ≥ 0 0 n � = � + � n − 1 � n − 1 � n � for n > k > 0 Why? k − 1 k k � n � • The value of can be computed by filling a table as follows: k · · k − 1 0 1 2 k 0 1 1 1 1 · 1 � � � � n − 1 n − 1 n − 1 1 k − 1 k � � n n 1 k • Q: Do we need all of this table filled in? Jim Diamond, Jodrey School of Computer Science, Acadia University

  7. Chapter 8 132 Example: The “Coin-Row” Problem: 1 • Given: there are n coins in a row, of values c 1 , c 2 , . . . , c n • Goal: pick up the maximum amount of money, without taking two adjacent coins • Observation 1: starting with the largest coin won’t work – • Q: how to proceed? • Observation 2: either the optimum solution uses the first coin or it doesn’t Observation 2 ′ : either the optimum solution uses the last coin or it • doesn’t – Jim Diamond, Jodrey School of Computer Science, Acadia University

  8. Chapter 8 133 Example: The “Coin-Row” Problem: 2 • Define F ( k ) to be the best solution using only the first k coins Apply Observation 2 ′ : • � c n + F ( n − 2) , F ( n − 1) � F ( n ) = max (*) • Write down the base cases (“initial conditions”): F (0) = 0 , F (1) = c 1 • Now fill in a one-row table of F ( i ) values from left to right using formula (*) – • Problem for the diligent student: F ( n ) just gives us the optimal amount; how do we know which specific coins to take? GEQ! • Note that for this problem , to calculate only F ( n ) , we don’t really need the whole array of size n : we could get away with just three memory locations Jim Diamond, Jodrey School of Computer Science, Acadia University

  9. Chapter 8 134 Collecting Objects from an n × m Board: 1 • Given: there is an n × m board with objects at some of the board positions; for example * * * * * * * * * * * • Rules: you start in the upper left corner, at at each turn you can move right or down (but not off the board); you collect the object from any location you move onto • Goal: collect as many objects as possible • Observation 1: a wrong choice at the beginning or near the end can produce a sub-optimal solution (as well as in the middle) Jim Diamond, Jodrey School of Computer Science, Acadia University

  10. Chapter 8 135 Collecting Objects from an n × m Board: 2 • As with many (most?) dynamic programming problems, the first trick is to figure out what function you are optimizing • The second trick is to figure out how you can relate the optimal solution of smaller problems to the optimal solution of larger problems • Once you know these two things, the rest is often “easy” • Idea 1: maximize F ( k ) , the number of objects collected after k steps • Problem: relating F ( k ) to F ( k − 1) is difficult because there are (in general) many places you can be after k steps, and for each of those there are (for this problem) usually two places you might have come from (to the left or above) • So that is not a good choice of function to optimize Jim Diamond, Jodrey School of Computer Science, Acadia University

  11. Chapter 8 136 Collecting Objects from an n × m Board: 3 • Idea 2: maximize F ( i, j ) , the number of objects which can be collected when you get to board position ( i, j ) • This definition of F () can be “easily” related to “smaller” problems – (and for those, only if i and/or j is larger than 0) – • Base case: F (0 , 0) is the number of objects at (0 , 0) * * * * 0 1 2 2 3 4 * 1 1 2 2 3 4 gives F () = * * 1 1 3 4 4 4 * * * * 2 3 3 5 6 6 • Keep track of how we did it with another matrix (of size O ( n · m ) ): ← ← ← ← ← - ↑ ← ↑ ← ↑ ↑ ↑ ← ↑ ← ← ← ↑ ← ← ↑ ← ← Jim Diamond, Jodrey School of Computer Science, Acadia University

  12. Chapter 8 137 The 0–1 Knapsack Problem: 1 • Problem: given n items of known weights w 1 , . . . , w n and values v 1 , . . . , v n and a knapsack of capacity W , find the most valuable subset of the items that fit into the knapsack. – – values can be non-integer ( Ω(2 n ) time!) • A brute force solution is discussed in Section 3.4 • Q: how can we formulate this as a dynamic programming problem – • Idea: consider just the first i items, for 1 ≤ i ≤ n • That by itself doesn’t give us the necessary recurrence to allow us to state “bigger” instances in terms of “smaller” instances • Note: a more general formulation of the knapsack problem allows any number of each item in the knapsack, not just 0 or 1 Jim Diamond, Jodrey School of Computer Science, Acadia University

  13. Chapter 8 138 The 0–1 Knapsack Problem: 2 • Idea 2: to be able to state “bigger” instances in terms of “smaller” instances, we must not only consider different numbers of objects, but also consider different target weights • Idea: allow the recurrence to be a function of not only the first i items, but the allowed weight: define F ( i, j ) be the value of an optimal solution for items with – weights w 1 , . . . , w i and values v 1 , . . . , v i in a knapsack of capacity j • Now apply the following amazing observation: there are two categories of subsets of the first i items that fit into a knapsack of capacity j : – those that do not contain item i – • The subsets that don’t contain i have optimal value F ( i − 1 , j ) • The subsets that contain i have optimal value F ( i − 1 , j − w i ) but only if j − w i ≥ 0 Jim Diamond, Jodrey School of Computer Science, Acadia University

  14. Chapter 8 139 The 0–1 Knapsack Problem: 3 • These ideas give the following recurrence: � max { F ( i − 1 , j ) , v i + F ( i − 1 , j − w i ) } if w i ≤ j F ( i, j ) = F ( i − 1 , j ) otherwise • As usual, we need some base cases (initial conditions): F (0 , j ) = 0 for j ≥ 0 F ( i, 0) = 0 for i ≥ 0 and • To perform the calculations, we fill a matrix in using a very similar technique to the binomial coefficients example – Jim Diamond, Jodrey School of Computer Science, Acadia University

Recommend


More recommend