Mohamed G. Gouda CS 313K Summer 2013 Homework 3 1) Derive, from the recurrence equation of the Fibonacci numbers, a closed equation of these numbers. Sol: The recurrence equation of the Fibonacci numbers is defined as follows: R(0) = 0 (1) R(1) = 1 (2) R(n+2) = R(n+1) + R(n) for n>=0 (3) From (3), the characteristic polynomial is r^2 = r+1 This polynomial has 2 distinct roots: r1 = (1+sqrt(5))/2 (4) r2 = (1-sqrt(5))/2 (5) From (4) and (5), the closed equation is R(n) = x*(r1)^n + y*(r2)^n (6) From (1), (2), (4), (5), and (6), we get x and y as follows: x = 1/sqrt(5) (7) y = -1/sqrt(5) (8) 2) Consider an algorithm for a recursively sorting an array x in an ascending order using interchange steps. In each interchange step, the values of two neighboring elements x[i] and x[i+1] are compared and the value of x[i] is found to be larger than the value of x[i+1], and the value of x[i] and x[i+1] are interchanged. Let R(n) denote the maximum number of the interchange steps that need to be executed to sort array x with n elements. Thus, R(1) = 0, R(2) = 1, R(3) = 3, and so on. Write the recurrence equation of R(n). Then use Poor-man's induction to derive a maybe correct closed equation for this recurrence equation. Sol: The recurrence equation is defined as R(1) = 0 (1) R(n+1) = R(n) + n for n>=1 (2) From (1) and (2), derive a maybe-correct closed equation using Poor-man's induction: R(n) = {from (2)} R(n-1) + (n-1) = {from (2)} R(n-2) + (n-2) + (n-1) = R(n-2) + 2*n - (2+1) = {from (2)} R(n-3) + 3*n - (3+2+1) = {using Poor-man's induction} R(n-i) + i*n - (i+...+2+1) for i>=1 = {choosing i to be (n-1)} R(1) + (n-1)*n - ((n-1)+...+2+1) = {from (1)} n-1*n-((n-1)*n/2) = (n-1)*n/2 The resulting closed equation (R(n) = (n-1)*n/2) still needs to be proven correct using induction. 3) Use induction to prove that the derived closed equation in Problem 2 is indeed correct. Sol: We use induction to prove that from the recurrence equation R(1) = 0 (1) R(n+1) = R(n) + n for n>=1 (2) we can infer the closed equation R(n) = (n-1)*n/2 for n>=1 (3) Proof: (by induction): Let P(n), for n>=1, be the predicate (R(n) = (n-1)*n/2) (4) Base case: (n=1): P(1) <=> {from (4)} R(1) = 0 <=> {from (1)} T Induction step: Prove for n>=1, P(n) => P(n+1) T => {from (2)} R(n+1) = R(n) + n => {from definition of P(n) in (4)} R(n+1) = ((n-1)*n/2 + n) = (n*(n+1)/2) => {from definition of P(n+1) in (4)} P(n+1) 4) Show by direct inference that the function f(n) = 3*n^2 + 5n - 100 is Theta(g(n)) where g(n) = n^2 Sol: |f(n)| = |3*n^2 + 5n - 100| = 3*n^2 + 5n - 100 for n > 5 =< 3*n^2 + 5n =< 3*n^2 + 5*n^2 = 8*|n^2| = C * |n^2| for C = 8 = C * |g(n)| for K = 5 and C = 8 |f(n)| = |3*n^2 + 5n - 100| = 3*n^2 + 5n - 100 for n > 5 >= 3*n^2 for n > 20 = C * |n^2| for C = 3 = C * |g(n)| for K = 20 and C = 3