CS 270 CS 270 General remarks Algorithms Algorithms Oliver Oliver Kullmann Kullmann Week 9 Making Making change change A general A general framework framework Dynamic Programming We learn about dynamic programming . Floyd- Floyd- Warshall Warshall algorithm algorithm Reading from CLRS for week 8 1 Chapter 15: We cover Section 15.2, 15.3, 15.4. Making change 1 2 Section 15.1 is a nice introduction, so it would be good if you could also read this. A general framework 2 3 We also treat Section 25.2. Floyd-Warshall algorithm 3 CS 270 CS 270 When greedy algorithms fail: making change Dealing with two coins Algorithms Algorithms Oliver Oliver Kullmann Kullmann Now assume that we have two coins with values d 1 � = d 2 , and Suppose we want to solve the Making Change problem of paying 9 pence with 1, 4 and 6 pence coins. we have to pay the sum of N pence. Consider an optimal Making Making change change solution a 1 · d 1 + a 2 · d 2 (assuming a solution exists at all!), The greedy algorithm gives 6+1+1+1 rather than the A general A general using a 1 + a 2 coins. framework framework optimal 4+4+1. Floyd- Floyd- 1 Either we use coin d 2 or not. Warshall Warshall So a new idea is required. algorithm algorithm 2 That is, either a 2 > 0 or a 2 = 0. We need a more systematic way of searching for a solution. 3 In the second case, only one coin is left and we are done. While we want to avoid (if possible) searching through all 4 So assume a 2 > 0. possible combinations. 5 We know d 2 is used at least once. Perhaps we can solve very simple problems, and then proceed 6 Now for the amount N − d 2 we know that recursively: a 1 · d 1 + ( a 2 − 1) · d 2 is an optimal solution! 1 If 0 pence is to be returned, we just use zero coins 7 Since if there would be a solution using fewer coins, then by (whatever the coins are). using one more coin we would obtain a better solution for 2 If we just have one coin, then the solution is also clear. the original problem.
CS 270 CS 270 Making change: the general idea Making change: the general structure Algorithms Algorithms Oliver Oliver Kullmann Kullmann We arrive at the idea of a general strategy, to solve the problem So we can solve the general Making Change problem as follows: Making Making with coins d 1 , . . . , d n and amount N to be payed: change change A general A general In order to pay the sum of N pence using n distinct coins Look how we do if we use only coins d 1 , . . . , d n − 1 . framework framework ( d 1 , d 2 , . . . , d n ), Floyd- Floyd- Look how we do if we use d n once, decreasing N to n − d n . Warshall Warshall we set up an n × ( N +1) table c [ 1 . . . n , 0 . . . N ]. algorithm algorithm Compare the two possibilities, and choose the better. In this table, c [ i , j ] will hold the minimum number of coins This scheme is to be applied recursively. Note the following: required to pay the amount j using only coins d 1 , . . . , d i . (If no arrangement of such coins makes up 1 In the first case we have a simpler case, since we use fewer j pence, then we shall have c [ i , j ] = ∞ .) coins. 2 In the second case we also have a simpler case, since the The solution will then be contained in c [ n , N ]. amount is decreased (though we do not use fewer coins). The table is filled out recursively according to the So for the recursion basis we must make sure that in “both case-distinction “use last coin or not”. directions” we end up in a case we can solve directly. CS 270 CS 270 Bookkeeping for making change The making-change algorithm Algorithms Algorithms Oliver Oliver Kullmann Kullmann To summarise we fill out the table as follows: Making Making Making-Change ( N , d [1 .. n ]) / Running time O ( nN ) / change change Clearly c [ i , 0] = 0 for every i . 1 for i = 1 to n A general A general � framework framework j div d 1 if j mod d 1 = 0 , 2 c [ i , 0] = 0 Also, for every j , c [1 , j ] = Floyd- Floyd- otherwise. ∞ 3 for j = 1 to N Warshall Warshall algorithm algorithm (Whenever we cannot make change for amount j using 4 if ( j mod d 1 )=0 coins d 1 , . . . , d i , we let c [ i , j ] = ∞ .) 5 c [1 , j ] = j div d 1 For c [ i , j ] ( i > 1, j > 0), we either: 6 else c [1 , j ] = ∞ 7 for i = 2 to n pay j pence using only coins d 1 , . . . , d i − 1 : c [ i , j ] ≤ c [ i − 1 , j ] or use (at least) one coin d i , and reduce the problem to that 8 for j = 1 to N of paying j − d i : c [ i , j ] ≤ 1 + c [ i , j − d i ] . 9 if j < d i 10 c [ i , j ] = c [ i − 1 , j ] As we want to minimise the number of coins, we choose the better of these two options: 11 else c [ i , j ] = min ( c [ i − 1 , j ] , 1 + c [ i , j − d i ]) 12 return c � � c [ i , j ] = min c [ i − 1 , j ] , 1 + c [ i , j − d i ] .
CS 270 CS 270 The making-change algorithm (continued) Determining an optimal solution Algorithms Algorithms The algorithm Making-Change tells us that c [3 , 9] = 3 coins Oliver Oliver Kullmann Kullmann are sufficient to make up 9 pence – but which three coins to use? Making Making change The following algorithm will answer that, retracing the solution change A general A general through the c -table (precomputed by Making-Change ). The framework framework Paying 9 pence using 6, 1 and 4 pence coins (order Example: output is an array a 1 , . . . , a n of natural numbers ≥ 0 such that Floyd- Floyd- irrelevant). Warshall Warshall algorithm algorithm a 1 · d 1 + · · · + a n · d n = N Amount 0 1 2 3 4 5 6 7 8 9 and such that a 1 + · · · + a n is minimal. d 1 = 6 0 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ d 2 = 1 0 1 2 3 4 5 1 2 3 4 Pay-Out ( c [1 .. n , 0 .. N ] , d [1 .. n ]) d 3 = 4 0 1 2 3 1 2 1 2 2 3 1 for ( i = 1; i ≤ n ; ++ i ) a [ i ] = 0; 2 i = n ; j = N ; 3 while j > 0 4 if ( i = 1) a [1] = c [1][ j ]; j = 0 5 else if ( c [ i , j ] = c [ i − 1 , j ]) i = i − 1 6 else a [ i ] = a [ i ] + 1; j = j − d i 7 return a CS 270 CS 270 Complexity of finding an optimal solution Why the greedy algorithm fails Algorithms Algorithms Oliver Oliver Kullmann Kullmann As with problems which can be solved by greedy algorithms, Making-Change has the Making Making change change This algorithm involves stepping back (at most) n rows, A general Optimal Substructure Property: An optimal solution to the A general framework framework and making c [ n , N ] jumps to the left. problem contains optimal solutions to subproblems. Floyd- Floyd- Warshall Warshall Hence it runs in time O ( n + N ). algorithm algorithm However, the greedy-choice property fails. We now have to Thus it is a negligible addition to the O ( nN ) algorithm consider many potential solutions, which requires added Making-Change . bookkeeping: we need to remember past decisions, and also build solutions from the bottom up. Of course, we could have computed the array a 1 , . . . , a n right We do have something though, namely the away directly in Making-Change — we don’t need array c to be completely computed. As an exercise, modify the code of Overlapping Subproblems Property: The space of subproblems Making-Change to do so. is small, so an otherwise-obvious top-down, divide-and-conquer recursive algorithm would solve the same subproblems over and over .
Recommend
More recommend