=========================================================================== CSC 236 Sample Solutions for Week 6 Tutorial Fall 2007 =========================================================================== Consider the following algorithm that computes x^y for x, y in N: pow(x, y): if y == 0: return 1 elif y % 2 == 0: t = pow(x, y/2) return t * t else: t = pow(x, (y-1)/2) return t * t * x A. Give a recurrence relation for the worst-case running time of pow(). Define n precisely (as a function of x and/or y), and briefly justify that your recurrence is correct (based on the algorithm). Q: What should n be? A: The variable that gets smaller from one call to the next: n = y. Q: What's the base case? A: T(0) = 3 (counting one step for each of '==', 'if', 'return') Q: What's the general case? A: T(n) = 5 (both 'if' statements and associated comparisons) + 6 (arithmetic in recursive call, assignment to t, return) + T(floor(n/2)) (recursive call) Q: Wait, why floor(n/2)? A: When n even, n/2 = floor(n/2); when n odd, (n-1)/2 = floor(n/2). So both recursive calls to pow can be written the same way. All together: T(0) = 3, T(n) = 11 + T(floor(n/2)) for n > 0. B. Perform repeated substitution to guess a closed form for your recurrence from the previous question. Q: How do we start? A: Expand T(n) according to recurrence, ignoring floors and ceilings. T(n) = 11 + T(n/2) = 11 + 11 + T(n/4) = 11 + 11 + 11 + T(n/8) Q: What's the general pattern? A: After i steps, we expect T(n) = 11 i + T(n/2^i) Q: When is the base case (when does T "disappear")? A: When n/2^i = 0. Problem: n/2^i > 0 for all i. Solution? Stop when n/2^i = 1, i.e., when i = lg n (using "lg" for "log_2"). Q: What expression do we get? A: T(n) = 11 lg n + T(1) = 11 lg n + 11 + T(0) = 11 lg n + 11 + 3 = 11 lg n + 14 C. Based on your guess from the previous question, give a tight bound (i.e., using Theta notation) on the worst-case running time of pow(). Prove your claim using induction -- you may need to prove separate upper and lower bounds. Start with lower bound. Q: What lower bound should we try to prove? A: Try 11 lg n + 14. Q: For what values of n? A: n = 0 makes no sense (lg 0 undefined) so start at n = 1. So, trying to prove T(n) >= 11 lg n + 14 for all n >= 1. { As usual, do this step-by-step from student suggestions. } Base: T(1) = 14 >= 0 + 14 = 11 lg 1 + 14. I.H.: Let n > 1 and suppose T(j) >= 11 lg j + 14 for 1 <= j < n. Step: Since n > 1, 1 <= floor(n/2) < n so T(n) = 11 + T(floor(n/2)) >= 11 + 11 lg(floor(n/2)) + 14 >= 11 lg 2 + 11 lg(floor(n/2)) + 14 >= 11 lg(2 floor(n/2)) + 14 Q: How to get rid of floor? A: One possibility: floor(n/2) >= (n-1)/2. Problem: Gives T(n) >= 11 lg(n-1) + 14, NOT T(n) >= 11 lg n + 14. Q: Solution? A: 1. Trick from class: change lower bound to T(n) >= 11 lg(n+1) + 14 and adjust constants to work for base case. 2. Change lower bound to T(n) >= 11 floor(lg n) + 14. Since first trick covered in class, let's try second one, i.e., show T(n) >= 11 floor(lg n) + 14 for all n >= 1. { Again, step-by-step. } Base: T(1) = 14 >= 14 = 11 floor(lg 1) + 14. I.H.: Let n > 1 and suppose T(j) >= 11 floor(lg j) + 14 for 1 <= j < n. Step: Since n > 1, 1 <= floor(n/2) < n so T(n) = 11 + T(floor(n/2)) >= 11 + 11 floor(lg(floor(n/2))) + 14 >= 11 (1 + floor(lg(floor(n/2)))) + 14 >= 11 floor(1 + lg(floor(n/2))) + 14 >= 11 floor(lg 2 + lg(floor(n/2))) + 14 >= 11 floor(lg(2 floor(n/2))) + 14 Q: How to get rid of floor inside log? A: Consider cases "n even" and "n odd", and use facts stated in class: floor(lg k) = floor(lg(k-1)) and ceil(lg k) = ceil(lg(k+1)) for all odd k >= 3. - If n is even, floor(n/2) = n/2 so T(n) >= 11 floor(lg(2 n/2)) + 14 >= 11 floor(lg n) + 14. - If n is odd, floor(n/2) = (n-1)/2 so T(n) >= 11 floor(lg(2 (n-1)/2)) + 14 >= 11 floor(lg(n-1)) + 14. However, by fact above, floor(lg(n-1)) = floor(lg n) because n > 1 and n odd, so T(n) >= 11 floor(lg n) + 14. In either case, T(n) >= 11 floor(lg n) + 14. Conclusion: T(n) >= 11 floor(lg n) + 14 for all n >= 1. Q: What next? A: Upper bound. Wait! Look again at proof of lower bound: in inductive step, each line was equal to previous one, except possibly for IH; base case was actually equal. Maybe we can find exact closed form for T(n)? Claim: T(n) = 11 floor(lg n) + 14 for all n >= 1. Proof? Same as above, except each line = previous line! So no need for separate upper and lower bounds since we know exact closed form for T(n).