We can do better were re computing values in our

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ient The RecursiveChange Tree 77 •  It recalculates the optimal coin combination for a given amount of money repeatedly 76 75 74 73 69 74 72 68 73 68 66 62 72 70 66 70 71 67 70 68 64 72 70 66 69 67 63 68 66 62 66 64 60 62 60 56 66 64 60 Example: M = 77, c = (1,3,7) Optimal coin combo for 70 cents is computed 9 times! We Can Do Better •  We’re re-computing values in our algorithm more than once •  Save results of each computation for 0 to M •  Reference already computed values instead of recomputing •  Running time: M x d, where M is the value of money and d is the number of denominations DPChange: Example 0 0123456 0 0 01 0 1 0 2 1 2 1 1 3 2 2 2 1 2 1 2 3 2 1 0 1 2 1 2 3 2 1 2 0123456789 1 0 2 012345 0 2 1 01234 0 1 012345678 0123 0 2 01234567 1 012 0 1 1 2 3 1 2 1 2 3 2 1 2 3 c = (1,3,7) M=9 4 The Change Problem: Dynamic Programming DPChange: Example 0 0123456 0 DPChange(M,c,d) bestNumCoins0 0 for m 1 to M bestNumCoinsm infinity for i 1 to d i f m ≥ ci if bestNumCoinsm – c + 1 < bestNumCoinsm i bestNumCoinsm bestNumCoinsm – ci+ 1 return bestNumCoinsM 0 01 0 1 0 2 1 2 1 2...
View Full Document

Ask a homework question - tutors are online