{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

problems and solutions ########

# problems and solutions ######## - CS 38 PROBLEM SET 2...

This preview shows pages 1–3. Sign up to view the full content.

CS 38 PROBLEM SET 2: SOLUTIONS Problem 1 . (Pibonacci numbers) [10 points] For a nonnegative real number n , we define the pibonacci number P ( n ) as follows: P ( n ) = 0 0 n < 4 P ( n - 1) + P ( n - π ) n 4 Find an algorithm to compute P ( n ) in time quadratic in n . You may assume that infinite-precision arithmetic operations can be computed in constant time. As in many dynamic programming problems, we’ll want to compute a table of partial answers: specifically, p [ α, β ] = P ( n - α · 1 - β · π ). We wish to compute p [ α, β ] based on p [ α + 1 , β ] and p [ α, β + 1]. Our first step is to allocate the table p , addressed by 0 α n , 0 β n/π . The easiest way to solve this problem is to write a recursive algorithm with caching. Initially, we set all table elements to the sentinel value unused . (This is a small burden; in our case, unused = 0 works.) Table-Entry ( α, β ) 1 if 0 n < 4 2 then p [ α, β ] 1 3 elseif p [ α, β ] = unused 4 then p [ α, β ] Table-Entry ( α + 1 , β ) + Table-Entry ( α, β + 1) 5 return p [ α, β ] The answer we seek is the return value of Table-Entry (0 , 0). Note that O (1) time is spent in the body of each call to Table-Entry , if we don’t count recursive calls. Thus the running time is O (1) times the number of calls to Table-Entry : the original plus at most two recursive calls per table slot. This amounts to O ( n 2 ). A more efficient way to solve the problem is to avoid the overhead of making recursive function calls and initializing the table: instead, one can simply solve the subproblems in order. Pibonacci ( n ) 1 for α n downto 0 2 do p [ α, n/π ] 0 3 for β n/π - 1 downto 0 4 do p [ n - βπ , β ] 0 5 for α n - βπ - 1 downto 0 6 do p [ α, β ] p [ α + 1 , β ] + p [ α, β + 1] 7 return p [0 , 0] Problem 2 . (Coin Changing) CLRS 16-1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 CS 38 PROBLEM SET 2: SOLUTIONS Consider the problem of making change for n cents using the fewest number of coins. Assume that each coin’s value is an integer. (a) [2 points] Describe a greedy algorithm to make change consisting of quarters, dimes, nickels, and pennies. Prove that your algorithm yields an optimal solution. Imagine a change machine that had to spit out n cents, one coin at a time. At each step, the greedy choice is the largest coin whose denomination doesn’t exceed the remaining amount of money. On a computer, you instead compute a vector ( Q, D, N, P ) determining the number of each coin to use. The equivalent greedy strategy is to set Q = n/ 25 and n Q = n - 25 q , D = n Q / 10 and n D = n Q - 10 D , etc. In this formulation, we have four subproblems: finding the optimal Q when making n = 25 Q + 10 D + 5 N + 1 P ; finding the optimal D when making n Q = 10 D + 5 N + 1 P ; finding the optimal N when making n D = 5 N + 1 P ; finding the optimal P when making n N = 1 P . These problems have an optimal substructure: given a fixed set of large coins, you want to use as few small coins as possible. We prove in reverse or- der that the greedy choice is appropriate for each subproblem. The optimal substructure and greedy choice properties will justify the greedy algorithm.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern