This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 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 ) = ≤ 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 infiniteprecision 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 ≤ α ≤ b n c , 0 ≤ β ≤ b n/π c . 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.) TableEntry ( α,β ) 1 if ≤ n < 4 2 then p [ α,β ] ← 1 3 elseif p [ α,β ] = unused 4 then p [ α,β ] ← TableEntry ( α + 1 ,β ) + TableEntry ( α,β + 1) 5 return p [ α,β ] The answer we seek is the return value of TableEntry (0 , 0). Note that O (1) time is spent in the body of each call to TableEntry , if we don’t count recursive calls. Thus the running time is O (1) times the number of calls to TableEntry : 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 α ← b n c downto 2 do p [ α, b n/π c ] ← 3 for β ← b n/π c  1 downto 4 do p [ b n βπ c ,β ] ← 5 for α ← b n βπ c  1 downto 6 do p [ α,β ] ← p [ α + 1 ,β ] + p [ α,β + 1] 7 return p [0 , 0] Problem 2 . (Coin Changing) CLRS 161 1 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 = b n/ 25 c and n Q = n 25 q , D = b n Q / 10 c 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 ....
View
Full
Document
This note was uploaded on 04/09/2010 for the course CS 12345 taught by Professor Thomas during the Spring '10 term at École Normale Supérieure.
 Spring '10
 Thomas
 Algorithms

Click to edit the document details