Mohamed G. Gouda CS 313K Fall 2012 Midterm 5 1. Consider the recurrence equation: T(1) = 1 T(n+1) = 2*T(n) + 1 Prove, by induction, that the closed equation for T(n) is: T(n) = 2^n - 1 for n >= 1 Sol: Let P(n) be the predicate T(n) = 2^n - 1. Base Case: n=1: P(1) <=> T(1) = 2-1 = 1 <=> T Induction Step: For n >= 1, P(n) => P(n+1) T => {Definition of T(n+1)} T(n+1) = 2*T(n) + 1 => {Induction Hypothesis P(n)} T(n+1) = 2*(2^n-1) + 1 = 2^(n+1) - 2 + 1 = 2^(n+1) - 1 => {Definition of P(n+1)} P(n+1) 2. Consider the recurrence T(1), T(2), ... where T(n) = number of slices that can be obtained from a pizza by making n straight cuts (along some diameters of the pizza) using a pizza knife. Define the recurrence equation and derive the closed equation for this recurrence. Sol: T(1) = 2, T(2) = 4, T(3) = 6, ... Each cut makes four slices out of two slices. Thus, the recurrence equation can be defined as: T(1) = 2 T(n+1) = T.n + 2 for n >= 1 A derivation of the closed equation can proceed as follows: T(n) = T(n-1) + 2 = T(n-2) + 2 + 2 ... = T(n-i) + 2^i = {choosing i to be n-1} T(1) + 2^(n-1) = 2 + 2^(n-1) 3. Use the Characteristic Polynomial method to compute the closed equation for the following recurrence equation: T(0) = 2 (1) T(1) = 8 (2) T(n+2) = (-4)*T(n+1) + 5*T(n) for n >= 0 (3) Sol: The Characteristic Polynomial from (3) is: t^2 = (-4)*t + 5 t^2 + 4*t - 5 = 0 (t+5)(t-1) = 0 t1 = -5 and t2 = 1 Expression for T(n): T(n) = x*(-5)^n + y*(1)^n (4) From (1) and (4): 2 = x + y (5) From (2) and (4): 8 = (-5)*x + y (6) From (5) and (6), x = -1 and y = 3 (7) The closed equation from (4) and (7): T(n) = (-1)*(-5)^n + 3 4. Show, by direct inference, that f(x) = floor(x) is Theta(g(x)) where g(x)=x. Sol: Proving f(x) is O(g(x)): |f(x)| = |floor(x)| = floor(x) for x>1 =< x = C*|x| for C=1 = C*|g(x)| for K=1 and C=1 Proving f(x) is Omega(g(x)): |f(x)| = |floor(x)| = floor(x) for x>1 >= (x/2) = C*|x| for C=(1/2) = C*|g(x)| for K=1 and C=(1/2) 5. Let d be any positive number and m be any positive integer. Show, by direct inference, that f(x) = (x+d)^m is O(g(x)) where g(x) = x^m. Sol: |f(x)| = |(x+d)^m| = (x+d)^m for x>0 =< (2*x)^m for x>d = 2^m * x^m = C*|x^m| for C=2^m = C*|g(x)| for K=d and C=2^m