Mohamed G. Gouda CS 313K Fall 2012 Exercise 11 1. Consider the recurrence equation, T(0) = 7 T(n+1) = 2*T(n) for n >= 0 Prove, by induction, that the closed equation for this recurrence is T(n) = 7*(2)^n for n >= 0 Sol: Let P(n) be the predicate T(n) = 7*(2)^n. Base Case: n=0: P(0) <=> T(0) = 7*(2)^0 <=> T(0) = 7 <=> T Induction Step: For n >= 0, P(n) => P(n+1) T => {Definition of T(n+1)} T(n+1) = 2*T(n) => {Induction Hypothesis P(n)} T(n+1) = 2*7*(2)^n => {Arithmetics} T(n+1) = 7*(2)^(n+1) => {Definition of P(n+1)} P(n+1) 2. Consider the recurrence equation T(0) = 4 T(1) = 3 T(n+2) = 3*T(n+1) + 4*T(n) Prove, by induction, that the closed equation for T(n) is: T(n) = (13*(-1)^n + 7*(4)^n)/5 Sol: Let P(n) be the predicate T(n) = (13*(-1)^n + 7*(4)^n)/5. Base Case: n=0 and n=1: P(0) <=> T(0)=(13+7)/5 <=> T(0)=4 <=> T P(1) <=> T(1)=(-13+28)/5 <=> T(1)=3 <=> T Induction Step: For n>=0, P(n) and P(n+1) => P(n+2) T => {Definition of T(n+2)} T(n+2) = 3*T(n+1) + 4*T(n) => {Induction Hypothesis P(n) and P(n+1)} T(n+2) = 3*(13*(-1)^(n+1) + 7*(4)^(n+1))/5 + 4*(13*(-1)^n + 7*(4)^n)/5 = ((-39)*(-1)^n + 84*(4)^n + (52)*(-1)^n + 28*(4)^n)/5 = (13*(-1)^n + 112*(4)^n)/5 = (13*(-1)^(n+2) + 7*(4)^(n+2))/5 => {Definition of P(n+2)} P(n+2) 3. Consider the recurrence: T(1) = 1/2, T(2) = 2/3, T(3) = 3/4, ... Define the closed equation and recurrence equations for this recurrence. Sol: The closed equation for T(n) is: T(n) = n/(n+1) for n >= 1 The recurrence equation is: T(1) = 1/2 T(n+1) = (n+1)/(n+2) = ((n+1)*n) / ((n+2)*n) = (n) / ((n+2)*(n/(n+1))) = (n) / ((n+2)*T(n)) for n >= 1 4. Consider the recurrence equation: T(1) = 1 T(n+1) = T(n) + 2 for n >= 1 Derive the closed equation for T(n). Sol: 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) = 1 + 2*(n-1) Thus, the closed equation for T(n) is: T(n) = 2*n - 1 for n >= 1 5. Consider the recurrence equation: T(1) = 1 T(n+1) = T(n) + 2 Prove, by induction, that the closed equation for T(n) is: T(n) = 2*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 = 1 <=> T Induction Step: For n >= 1, P(n) => P(n+1) T => {Definition of T(n+1)} T(n+1) = T(n) + 2 => {Induction Hypothesis P(n)} T(n+1) = 2*n - 1 + 2 = 2*(n+1) - 1 => {Definition of P(n+1)} P(n+1) 6. Consider the recurrence equation: T(0) = 0 T(n+1) = T(n) + (n+1) for n >= 0 Derive the closed equation for T(n). Sol: T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n ... = T(n-i) + (n-i+1) + ... + n = {Choosing i to be n} T(0) + 1 + 2 + ... + n = 0 + 1 + 2 + ... + n = n*(n+1)/2 Thus, the closed equation for T(n) is: T(n) = n*(n+1)/2 for n >= 0 7. Consider the recurrence equation: T(0) = 0 T(n+1) = T(n) + (n+1) for n >= 0 Prove, by induction, that the closed equation for T(n) is: T(n) = n*(n+1)/2 Sol: Let P(n) be the predicate T(n) = n*(n+1)/2. Base Case: n=0: P(0) <=> T(0) = 0 <=> T Induction Step: For n >= 0, P(n) => P(n+1) T => {Definition of T(n+1)} T(n+1) = T(n) + (n+1) => {Induction Hypothesis P(n)} T(n+1) = (n*(n+1) + 2*(n+1))/2 = (n+1)*(n+2)/2 => {Definition of P(n+1)} P(n+1) 8. Consider the recurrence equation (of the Tower of Hanoi game): T(1) = 1 T(n+1) = 2*T(n) + 1 for n >= 1 Derive the closed equation for T(n). Sol: T(n) = 2*T(n-1) + 1 = 2*(2*T(n-2) + 1) + 1 = 2^2*T(n-2) + 2 + 1 = 2^2*(2*T(n-3) + 1) + 2 + 1 = 2^3*T(n-3) + 2^2 + 2 + 1 ... = 2^i*T(n-i) + 2^(i-1) + 2^(i-2) + ... + 2^0 = {Choosing i to be (n-1)} 2^(n-1)*T(1) + 2^(n-2) + 2^(n-3) + ... + 2^0 = 2^(n-1) + 2^(n-2) + ... + 2^0 (1) Multiplying both sides by 2, we get: 2*T(n) = 2^n + 2^(n-1) + ... + 2^1 (2) Subtracting (1) from (2), we get: T(n) = 2^n - 1 9. Consider the recurrence equation (of the Tower of Hanoi game): T(1) = 1 T(n+1) = 2*T(n) + 1 for 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) 10. Consider a vending machine that accepts only $1's and $5's. Let T(n) be the number of sequences of $1's and $5's whose sum is $n. Derive the recurrence equation for T(n). Sol: Clearly, T(1) = 1, T(2) = 1, T(3) = 1, T(4) = 1, T(5) = 2, T(6) = 3, ... Note that the three sequences of T(6) are (1,5), (5,1), and (1,1,1,1,1,1). To define T(n), note that the last element in any sequence of T(n) is either 1 or 5. The number of sequences, whose last element is 1, is T(n-1). Also, the number of sequences, whose last element is 5, is T(n-5). Thus, T(n) = T(n-1) + T(n-5). Therefore, the recurrence equation for T(n) is: T(1) = 1 T(2) = 1 T(3) = 1 T(4) = 1 T(5) = 2 T(n+5) = T(n+4) + T(n) for n >= 1 11. Consider a grid of 1*n squares and assume that each square is colored white or black such that no two adjacent squares are colored white. Let T(1), T(2), ... be the recurrence where T(n) denotes the number of different ways that this grid can be colored. Derive the recurrence equation of this recurrence. Sol: Clearly, T(1) = 2, T(2) = 3, T(3) = 5, .... To count the number of ways that this grid can be colored, note that (1) if the n-th square is colored black, then the remaining n-1 squares can be colored in T(n-1) different ways. (2) if the n-th square is colored white, then the n-1 square is colored black and the remaining n-2 squares can be colored in T(n-2) different ways. Thus, T(n) = T(n-1) + T(n-2). The recurrence equation is: T(1) = 2 T(2) = 3 T(n+2) = T(n+1) + T(n) for n >= 1