07-cs481-dynamic_programming-manhattan_grid_edit_distance-lcs.pdf

This preview shows page 1 - 15 out of 71 pages.

The Change Problem Goal : Convert some amount of money M into given denominations, using the fewest possible number of coins Input : An amount of money M, and an array of d denominations c = (c 1 , c 2 , …, c d ), in a decreasing order of value (c 1 > c 2 > … > c d ) Output : A list of d integers i 1 , i 2 , …, i d such that c 1 i 1 + c 2 i 2 + … + c d i d = M and i 1 + i 2 + … + i d is minimal
Change Problem: Example Given the denominations 1, 3, and 5, what is the minimum number of coins needed to make change for a given value? 1 2 3 4 5 6 7 8 9 10 1 1 1 Value Min # of coins
Change Problem: Example (cont d) Given the denominations 1, 3, and 5, what is the minimum number of coins needed to make change for a given value? 1 2 3 4 5 6 7 8 9 10 1 2 1 2 1 2 2 2 Value Min # of coins
Change Problem: Example (cont d) 1 2 3 4 5 6 7 8 9 10 1 2 1 2 1 2 3 2 3 2 Value Min # of coins Lastly, three coins are needed to make change for the values 7 and 9 Given the denominations 1, 3, and 5, what is the minimum number of coins needed to make change for a given value?
Change Problem: Recurrence This example is expressed by the following recurrence relation: minNumCoins(M) = minNumCoins(M-1) + 1 minNumCoins(M-3) + 1 minNumCoins(M-5) + 1 min of
Change Problem: Recurrence (cont d) Given the denominations c: c 1 , c 2 , …, c d , the recurrence relation is: minNumCoins(M) = minNumCoins(M-c 1 ) + 1 minNumCoins(M-c 2 ) + 1 minNumCoins(M-c d ) + 1 min of
Change Problem: A Recursive Algorithm 1. RecursiveChange ( M , c , d ) 2. if M = 0 3. return 0 4. bestNumCoins infinity 5. for i 1 to d 6. if M c i 7. numCoins RecursiveChange ( M c i , c , d ) 8. if numCoins + 1 < bestNumCoins 9. bestNumCoins numCoins + 1 10. return bestNumCoins
RecursiveChange Is Not Efficient It recalculates the optimal coin combination for a given amount of money repeatedly i.e., M = 77, c = (1,3,7): Optimal coin combo for 70 cents is computed 9 times!
The RecursiveChange Tree 74 77 76 70 75 73 69 73 71 67 69 67 63 74 72 68 72 70 66 68 66 62 72 70 66 70 68 64 66 64 60 68 66 62 66 64 60 62 60 56 . . . . . . 70 70 70 70 70
We Can Do Better We’re re-computing values in our algorithm more than once Save results of each computation for 0 to M This way, we can do a reference call to find an already computed value, instead of re-computing each time Running time M * d , where M is the value of money and d is the number of denominations
The Change Problem: Dynamic Programming 1. DPChange( M , c , d ) 2. bestNumCoins 0 0 3. for m 1 to M 4. bestNumCoins m infinity 5. for i 1 to d 6. if m ≥ c i 7. if bestNumCoins m – c i + 1 < bestNumCoins m 8. bestNumCoins m bestNumCoins m – c i + 1 9. return bestNumCoins M
DPChange: Example 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9 0 1 0 0 1 2 0 1 2 1 0 1 2 1 2 0 1 2 1 2 3 0 1 2 1 2 3 2 0 1 2 1 2 3 2 1 0 1 2 1 2 3 2 1 2 0 1 2 1 2 3 2 1 2 3 c = (1,3,7) M = 9
Manhattan Tourist Problem (MTP) Imagine seeking a path (from source to sink) to travel (only eastward and southward) with the most number of attractions ( * ) in the Manhattan grid Sink * * * * * * * * * * * Source *
Manhattan Tourist Problem (MTP) Imagine seeking a path (from source to sink) to

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture