Fund.Algo (G22.1170);Yap;Fall 2001;Yap;November 20, 2002 Page 1

MIDTERM with SOLUTION

Please answer all questions. Use ONLY THE FRONT SIDE OF EACH PAGE for your answers. Use the REVERSE SIDE of each page for scratch work. All the best!

GENERAL COMMENTS ABOUT YOUR ANSWERS:

• Remember that grades for this course is curved (normalized) and not based on absolute points.

• Amazingly, many students simply do not read instructions. Question 1 asked for ”brief justifications” but many simply wrote down an answer and left it at that! Question 2 asked for intermediate trees after EACH insertion/deletion/rotation, but some simply skip many crucial intermediate steps. Please realize that you are not entitled to any points when you ignore such explicit instructions (but we have graded more generously than this).

• Each question asks for something specific: Question 1(iii) asked for a set, but most answers never even mention a set. Question 1(iv) asked for a solution a recurrence. You must give us what we asked for. Otherwise, you are not answering the question.

1. 10 points each part. Must give brief justification, but no more than half a page! (i) My pocket calculator tells me that log_{ϕ} 100 = 9.5699 · · ·. What does this tell you about the height of an AVL Tree with 100 nodes? (ii) Define M(h) to be the maximum number of nodes in an AVL tree of height h. Give a formula for M(h). (iii) Consider the recurrence T (n) = aT (n/b) + \frac{n^{4}}{log n} where a > 0 and b > 1. Describe the set S of all pairs (a, b) for which the Master Theorem gives a solution for this recurrence. Do not describe the solutions. (iv) State the solution for T (n) = 16T (n/2) + \frac{n^{4}}{log n}.

(v) Suppose I modify the linear time median algorithm so that each group has 13 elements (instead of 5 elements). Give the recurrence satisfied by its running time T (n). Assume the driving function f(n) is O(n). (vi) Recall the TV Game Show (“Let’s Make a Deal”) where there are 3 veiled stages, one of which has a car. Suppose we now have 4 veiled stages. You first make a choice, and then the game master opens the curtain of one other stage, which has no car, of course. You are now given a chance to switch your choice. What should you do? What is your probability of getting a car?

ANSWER:

(i) The height is at most 9. Justification: If an AVL Tree has height h and n nodes then n ≥ μ(h) ≥ ϕ^{h}. Taking logs, log_{ϕ} n ≥ h. So 9.5699 ⋯ = log_{ϕ} 100 ≥ h. Since h is an integer, this implies the AVL tree of 100 nodes has height ≤ 9.

PITFALLS: Note that we say at most 9. Do not conclude that the height is equal to 9. You cannot even claim that the “maximum height of an AVL tree of 100 nodes” is equal to 9. All we know is log_{ϕ} 100 is UPPER BOUND on h, but we never show that it was a tight bound. Some people conclude the height must be ⌈log_{ϕ} 100⌉ = 10, which is also wrong. (ii) The formula is M(h) = 2^{h+1} − 1. Justification: The maximum number of nodes occurs precisely when there are exactly 2^{i} nodes at level i for i = 0, 1, …, h. Thus M(h) = ∑_{i=0}^{h} 2^{i} = 2^{h+1} − 1. Alternatively, you can note the recurrence relation M(h) = 1 + 2M(h − 1) and solving for this, we get the formula above.

PITFALLS: If you stop at the recurrence relation, this is partial credit. But many students make the mistake of writing M(h) = 1 + M(h − 1) + M(h − 2), the recurrence for μ(h)!

(iii) The set is S = {(a, b) : a > 0, b > 2, log_{b} a ≠ 4}. Justification: There is a solution iff log_{b} a > 4 (case −) or log_{b} a < 4 (case +) When log_{b} = 4, the Master Theorem tells you nothing about the solution.

PITFALLS: As I said, many people did not even try to describe a set. Here is one for which I give (reluctantly) some partial credit: someone said that

S = {(a, b) : f(n) = O(w(n)n^{−ε})or f(n) = Ω(w(n)n^{ε})or f(n) = Θ(w(n))}

where f(n) = n^{4}/ log n and w(n) = n^{log_{b} a}. This answer is perfectly accurate but basically yields no insights. It does not even realize that f(n) = Θ(w(n)) is impossible. (iv) The solution is T (n) = Θ(n^{2} log log n). Justification: We prove the general form of this recurrence in homework 2.

PITFALLS: This cannot be solved by the Master Theorem at all. Yet many think that this is Case −1 of Master Theorem. You must not make this elementary mistake: 1/logn is NOT growing slower than n^{−ε} for any ε > 0. You get partial credit if you try to solve this by the various transformations, but failed.

(v) The recurrence is T (n) = T (n/13) + T (19n/26) + O(n). Justification: You recursively to solve a problem of size n/13 and another one of size at most 19n/26. Why 19n/26? This is because the median of the medians has at least 7×(n/13)×(1/2) = 7n/26 elements larger and at least the same number smaller than it. So in the second recursive call, at least 7n/26 elements are eliminated, leaving 19n/26.

PITFALLS: We can understand if you did not get 19n/26 exactly. But you should not forget the driving term O(n) nor fail to get T (n/13). (vi) You should always switch. The probability of getting a car is 3/8. Justification: If you never switch, your chance of getting a car is 1/4 = 2/8. This is less than 3/8. How do we get the probability 3/8? After the curtain is revealed, and you switch, your probably of being among the cars is 3/4. But one has been eliminated, so you have 1/2 chance of switching to the correct stage. [Of course, you can also analyze this using the tree diagram we discussed in class.]

PITFALLS: Some people did not use the tree diagram correctly. We will discuss this in class.

2. 20 points

Starting with an empty AVL tree, insert the following set of keys, in this order:

13, 18, 19, 12, 17, 14, 15, 16

Now delete key 18. Please show the tree at the end of each insertion/deletion. It is also recommended that you show intermediate trees after each rotation.

ANSWER:

PITFALLS: How can we give you partial credit if you do not show us your transformation in a step-by-step way? E.g., some students made a mistake early in the insertions, but assuming the mistake was still a valid AVL Tree, we can give your full credit for correct insertion/deletion/rotation into this wrong tree. You MUST know the algorithm for insertion and deletion. You cannot rotate by eye-ing it (that is my impression on grading some answers). Moreover, whatever you do, you must make sure that the trees at every step is REALLY a BST.

3. 15+10+15 points To generalize Karatsuba’s algorithm, consider splitting an n-bit integer X into m equal parts (assuming m divides n). Let the parts be X_{0}, X_{1}, …, X_{m−1} where X = ∑_{i=0}^{m−1} X_{i}2^{in/m}. Similarly, let Y = ∑_{i=0}^{m−1} Y_{i}2^{in/m}. Let us define Z_{i} = ∑_{j=0}^{i} X_{j}Y_{i−j} for i = 0, 1, …, 2m−2. In the formula for Z_{i}, assume X_{ℓ} = Y_{ℓ} = 0 when ℓ ≥ m.

rot^{2}(15) Del(18)  rot(15) Ins(15, 16) Ins(12, 17, 14)  rot^{2}(17)  rot(18) 12  14 15 16 17  18 19  17 13 18  19 12  17 13 12  14 1515 19  18 19 13 16 14 12 13  16 14 17  14 12 13 17  18 19 13 13  Ins(18)  Ins(19)  18  19 19 18 13 18  13 12  19 17 16 15 14 13

Figure 1: Insertion Sequence and Deletion

(i) How much time, as a function of m and n, does it take to compute the product Z = XY when you are given Z_{0}, Z_{1}, …, Z_{2m−2}? (ii) It is known that we can compute {Z_{0}, Z_{1}, …, Z_{2m−2}} from the X_{i}’s and Y_{j}’s using O(m log m) multiplications and additions involving (n/m)-bit integers. From the preceding remarks, give a recurrence relations for the time T (n) to multiply two n-bit integers. (iii) Conclude that for every ε > 0, there is an algorithm for multiplying any two n-bit integers in time T (n) = Θ(n^{1+ε}).

ANSWER:

(i) Note that Z = ∑_{i=0}^{2m−2} Z_{i}2^{in/m}. We compute Z in two steps. STEP 1: Compute the numbers Z′_{i}:= Z_{i}2^{in/m} in time O(in/m) = O(n) (this is just appending 0’s to Z_{i}). Hence the total time (for i = 0, …, 2m − 2) is O(mn). STEP 2: Add these Z′_{i} together. We can initialize Z to 0, and add Z′_{i} to Z, for i = 0, …, 2m − 2. The final sum has 2n bits, so that each addition takes O(n) time. So the total time for 2m − 1 additions is O(mn). Hence STEPS 1 and 2 together costs O(mn) time.

PITFALLS: Some people say: this step takes O(m) time. Their reason? There are O(m) arithmetic operations here. True, but there is a basic assumption in the analysis of Karatsuba which must be understood: T (n) here is in terms of BIT COMPLEXITY. You must not treat each arithmetic operation (e.g., Z_{i} times 2^{in/m}) as one operation, but as O(in/m) bit operations. As another hint, we also told you to express this complexity in terms of BOTH m and n (n is clearly a bit measure). (ii) To compute the Z_{0}, …, Z_{2m−2}, takes O(m log mT (n/m)) time for the multiplications, and O((m log m)(n/m)) = O(n log m) time for the additions. From part (a), the time to put the Z_{i}’s together takes O(mn) time. Hence the recurrence is

T (n) = O(m log mT (n/m)) + O(n log m) + O(mn).

Ignoring constant factors, we write the recurrence as

T (n) = m lg mT (n/m) + mn.

(iii) For any fixed m, the watershed function w(n) for the final recurrence in (b) is n^{log_{m}(m lg m)} = n^{1+log_{m}(lg m)}. Hence, by the Master Theorem, the solution is T (n) = Θ(n^{1+α}) where α = log_{m}(lg m). For any ε > 0, if we choose m so that α ≤ ε, then we would have shown the desired result. This amounts to ensuring that log_{m}(lg m) ≤ ε or, lg m ≤ m^{ε}. This inequality holds for m large enough.