Q
Transcription
Q
Sample midterm questions and solutions -- FA summer 04, Dan Barrish-Flood Q. An algorithm's average-case running time is O(n2). Give the most specific info you can about its best- and worst-case running times. A. The avg-case is bounded above by n2 so its best-case time must be, as well. We can conclude nothing about the worst-case. To summarize: 2 best-case: O(n ) worst-case: nothing Q. Write the recurrence for the running time of the following procedure, but do not solve it: G(n): if n = 1 then return 1 else x = 0 for i = 1 to n do x = x + G(n/4) return x A. The loop happens n times, and each time the function is called recursively: T(1) = 1 T(n) = n T(n/4) + n, n ≥ 2 Q. Using Θ notation, write down the asymptotic solution for the following recurrence (sqrt(n) means "the square root of n"): T(1) = 1 T(n) = 2T(n/4) + sqrt(n), n ≥ 2 A. (in a rush, I'm using ^ for "raise to power" and "sqrt" for "the square root of") This can be solved using the master theorem from CLRS (your text), which I will say a few words about, and you may use it (unless I specifically say not to in the wording of the question), but I strongly urge you to solve this by first applying a domain transformation, then a range transformation (i.e. do the telescoping thing). You should first make the domain trans n = 4^k, and note that sqrt(n) = 2^k. Then T(n) = T(4^k) = S(k). Now you have S(0) = 1 S(k) = 2S(k-1) + 2^k, k ≥ 1 Need a range trans! Use summation factor 2-k, and you come out with U(0) = 1 U(k) = U(k-1) + 1, k ≥ 1 this is the "world's simplest recurrence"! U(k) = k + 1, you know this old friend. Back substituting gives you k S(k) = 2 (k+1) then back sub again T(n) = sqrt(n)(log4n + 1) (log base 4, but the base doesn't matter) Final answer in Θ notation: T(n) = Θ(sqrt(n) lgn) Q. Explain how to sort n integers, each in the range 0 .. n4- 1, in time Θ(n). Think radix sort. A. Use radix sort, treating each number as a four-digit number in base n. Then d = 4 and k = n, for an overall running time of Θ(n). Q. Consider the follow routine, excerpted from notes of Richard Cole and Alan Siegel: function Rand(N); if N ≤ 1 then return (1) else assign x one of the values 0,1,2, each w/probability 1/3 if x = 0 then return (Rand(n)) elseif x = 1 then return (Rand(N-1) + 1) elseif x = 2 then return (3 x Rand(N-1) + Rand(N-1) + 1) end_Rand; A. Note: I will talk about this question this Thurs, june 17, '04. T (1) = 1 T (n) T (n − 1) 2T (n − 1) T ( n) = + + +1 3 3 3 for n ≥ 2 Q. Consider this portion of a decision tree, just like the one from CLRS, p. 166, but not as neat. Suppose I wish to sort the numbers 6, 7, and 8. Which input order corresponds to the final sorted sequence at the leaf labelled "x".? .."y"? Why is it the case that not all leaves in a decision tree are at the same depth? (Note: the "depth" of the root is 0, the depth of the root's children is 1, etc. "Height" is used a bit differently. See the appendix in CLRS for an exact explanation of these term, which differs widely from book to book.) A. The input sequence that gets us to leaf "x" is a1 = 9, a2= 7, a3=8, the one that gets us to leaf "y" is a1 = 8, a2= 7, a3=9 Leaves are at different depths (levels, heights, whatever), because, depending on the input sequence, fewer or more comparisons may be needed to determine the final sorted order, as in the two cases above. Note that the worst(!) is what we used to derive the nlgn lower bound on comparison-based sorting. Q. Suppose you draw a decision tree for an input size of four distinct elements. How many leaves will there be? A. There are n! permuations, so 4! gets you 24 leaves. Q. Prove by induction: SUM(i = 1 to n) i = n(n+1)/2 (ASIDE: this can be shown very elegantly without induction, as rumor has was done by Karl Gauss when he was 8 years old. I'll show you one day how Gauss did this if you're interested, but you've probably seen it a hundred times :) A. first show the base case 1 ∑ i = 1 and i =1 1(1 + 1) = 1, so base case OK. 2 the inductive case n +1 n i =1 i =1 ∑ i = ∑ i + (n + 1) n n(n + 1) n(n + 1) = + n + 1 (here I use the inductive assumption that ∑ i = 2 2 i =1 n(n + 1) +3n +1 ( n +1)( n + 2) (this last expression is =n = with the n' s replaced by n + 1) 2 2 2 QED 2 Q. Referring to the code for counting sort (CLRS p. 168, or in the 3-Sorting file): Referring to this input array A, and the values range from 0..5, where k=5. (A). What are the subscripts of the array C? (B). After line 4 of the code, what is the sum of the elements of C? (C). After line 7, what is the value of C[5]? (D). Can't this guy just do "insert table"? A. (A) 0..5 (B) 8 (the size of the input array A) (C) 8 (D) Depends on what phase the moon is in :) Q. (haven't covered this as of 6/15/2004) What is the worst-case running time for RANDOMIZED_SELECT, and why? A. the worst-case running time for RANDOMIZED_SELECT = Θ(n2) because we might be very unlucky, and always partition aroudn the largest remaining element, and partitition takes Θ(n) time.This could even happen (not likely!) if we were trying to find the minimum. Q. (haven't covered this as of 6/15/2004) Draw two different Binary Search Trees containing the keys 4, 6, 8, 10 A.