Exercises from Chapter 3 3.3 3.4 3.9 3.10 3.12 Solutions 3.3 2 log_3 n log_2 n n#(2/3) 20n 4n#2 3#n n! 3.4 (a) Let m be the number of inputs that a machine that is 64 times faster can process in the same time that the old machine takes to process n inputs. Then T(m)/64 = T(n), or 3*2#(m-6) = 3*2#n. Solving for m, m = n+6. (b) This time, the equation is m#2/64 = n#2, so m = 8n. (c) This time the equation is 8m/64 = 8n, so m = 64n. 3.9 (a) log n#2 = 2log n, so f(n) is in Theta(g(n)). (b) f(n) = n#(1/2), g(n) = 2log n, and g(n)/f(n) is asymptotic to 0 (using calculus: compare the derivatives of f(n) and g(n) or just use l'Hospital's rule), so f(n) is in Omega(g(n)) but not in O(g(n)). (c) g(n)/f(n) = 1/log n, which is asymptotic to 0, so f(n) is in Omega(g(n)) but not in O(g(n)). (d) Same reasoning as in (b) and (c). (e) Same reasoning as in (b), (c), and (d). (f)Both f(n) and g(n) are constants, i.e., they are Theta(1), so f(n) is in Theta(g(n)). (g) Same reasoning as in (b), (c), (d), and (e). (h) f(n)/g(n) = (2/3)#n, which is asymptotic to 0, so f(n) is in O(g(n)), but not in Omega(g(n)). 3.10 (a) Theta(1). (b) Theta(n) because the inner loop takes Theta(n) but the outer loop is executed only Theta(1) times. (c) Theta(n#2) (d) Theta(n#2 log n) because the inner loop takes Theta(n) and the call to sort takes Theta(nlog n). Therefore the total time of one execution of the outer loop is Theta(n + nlog n) = Theta(nlog n) by rule 3 The outer loop is executed Theta(n) times, so the total time is Theta(n#2 log n) by rule 4. (e) Theta(n#2) because each execution of the inner loop takes Theta(j), where j is the index such that A[j] = i. Since A is a permutation of 0,...,n-1, each of the numbers 0,...,n-1 is used once as the index j such that A[j] = i. Therefore the total cost of the outer loop is Theta(0) + Theta(1) + ... + Theta(n-1) = Theta(n#2) by equation (2.1). (f) This is a weird one because the run time is not asymptotic to any algebraic function. But we can answer it this way. Let f(n) be defined as f(n) = n if n is even = 1 if n is odd Then the (average) run time is Theta(f(n)). More to the point, The best case time is in Omega(1) and the worst case time is in O(n). 3.12 Unwinding the recurrence, after i steps, T(n) = T(n/(2#i)) + sqrt(n)[1 + (2#(-1/2)) + ... + (2#(-1/2))#(i-1)] and eventually when i = log n, = T(1) + sqrt(n)[1 + (2#(-1/2)) + ... + (2#(-1/2))#(log n - 1)] = 1 + sqrt(n)[(n#(-1/2) -1)/(2#(-1/2) - 1)] by equation (2.6) = Theta(sqrt(n)).