Exam 1 Solutions 1. Base case: n = 1. 1! = 1 and 2#(1-1) = 2#0 = 1, so 1! = 2#(1-1). Induction step: assume n > 1 and n! >= 2#(n-1). Let's show (n+1)! >= 2#n: (n+1)! = (n+1)*n! by definition >= (n+1)*2#(n-1) by induction assumption >= 2*2#(n-1) since n > 1 = 2#n 2. S(n) = S(n-1) + 2#n = [S(n-2) + 2#(n-1)] + 2#n = S(n-2) + 2#(n-1) + 2#n ... = S(n-i) + 2#(n-i+1) + 2#(n-i+2) + ... + 2#(n-1) + 2#n (guessing) = S(0) + 2#(1) + 2#(2) + ... + 2#(n-1) + 2#n (taking i = n) = 2 + 2#(n+1) - 2 (since S(0) = 2 and using the formula for the geometric series 2#0 + 2#1 + ... + 2#n = 2#(n+1) - 1) = 2#(n+1) 3. Base case: n = 0. T(0) = 0 and 0#2 = 0, so T(0) = 0#2. Induction step: assume n > 0 and T(n-1) = (n-1)#2. Let's show T(n) = n#2: T(n) = T(n-1) + 2*n - 1 by definition = (n-1)#2 + 2*n - 1 by induction assumption = n#2 - 2*n + 1 + 2*n - 1 = n#2 4. a. a#n/b#n = (a/b)#n -> 0 as n -> infinity, because a < b. Therefore a#n is in O(b#n) but b#n is not in O(a#n). (Just saying a < b and therefore a#n < b#n is not enough to conclude b#n is not in O(a#n).) b. Since log_b n = (log_a n)/(log_b a), log_a n is in Theta(log_b n). 5. a. Inner loop is executed only 2 or 3 times, so its cost is Theta(1). Outer loop is executed Theta(n) times. Total cost is Theta(n). b. Inner loop is executed Theta(log n) times. Outer loop is executed Theta(n) times. Total cost is Theta(n log n). c. Inner for loop is executed Theta(j) times. Therefore cost of one iteration of outer for loop costs Theta(j) times, and total cost of outer for loop is therefore the sum over j from 1 to n of Theta(j), which is Theta(n#2). While loop is executed Theta(n) times. Total cost is Theta(n#3). d. Each for loop is executed Theta(n) times. While loop is executed Theta(n) times. Total cost is Theta(n#2).