






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







′′
The last sum in the ab ove expression contains (lg n) terms, each no greater than n lg 3 n, and so T 2 (n) = O (n lg 4 n). On the other hand, the rst half of the lg 3 terms | or, to b e precise, the rst m = b log 2 3 nc terms | are each at least as big as n lg 3
p n = (n lg 3 n)= 8 | or, to b e precise again, at least as big as
n lg 3
n 3 m ^1
:
So T 2 (n) = (n lg 4 n). Together we get T 2 (n) = (n lg 4 n). We can also get this result directly using the version of the Master theorem given in class, and app earing as exercise 4.4-2 on page 72 of CLR. It is also p ossible to solve this problem using the tree metho d. However, we should b e careful to show b oth a lower and an upp er asymptotic b ound of n lg 4 n. That is, arguing that the tree has depth at least log 3 n, with each level costing at most n lg 3 n successfully proves that T 2 (n) = O (n lg 4 n) | but we must also show that T 2 (n) = (n lg 4 n) to conclude that T 2 (n) = (n lg 4 n). This can b e done by considering the cummulative cost of just half the tree levels, in a manner similar to our previous derivation using the iteration metho d. Common error: We cannot apply the basic form of the Master theorem (page 63 of CLR) since
a = 3 b = 3 f (n) = n lg 3 n nlogb^ a^ = nlog^3 3 = n
and, even though n lg 3 n = (n), it is not (n1+^ ) for any > 0. Why? Because if n lg 3 n = (n1+^ ), we could deduce that lg 3 n = (n^ ), which contradicts the formula on page 35 of CLR. So case 3 do es not apply. Common error 2: When we obtain the summation
n lg 3 n + n lg 3 (n=3) + n lg 3 (n=9) + + n lg 3 (n=(n=3))
in the recurrence, this cannot b e approximated as O (lg 3 n). A simple way to analyse this would b e to observe that each term is O (lg 3 n) and there are O (lg n) such terms, yielding an upp er b ound of O (lg 4 n). Graded by Suresh.
Using the tree metho d we get the following tree
n
n/2 n/
n/4 n/8 n/8 n/
n
3n/
9n/
(3/4) ni
Note that the tree will b e unbalanced and that the subtree corresp onding to T 3 (bn= 2 c) will b e deep er (i.e. it will have more levels). At all levels after the shallow subtree | the one corresp onding to T 3 (bn= 4 c) | b ecomes exhausted, each level will cost no more than
4
n. So we have
T 3 (n)
i=
3 4
n = O (n):
Now the top level costs n, and hence
T 3 (n) n = (n):
Combining the two, we concude that
T 3 (n) = (n):
Common error: \Since there are O (lg n) levels, and each level costs O (n), the total cost is O (n lg n)". The problem with this argument is that it is lo ose. There are O (lg n) levels, but the cost at each level decreases. In fact, the cost at level i is n
3 4
. Summing up, we obtain the ab ove b ound of O (n).
Graded by Suresh.
True False The answer is True. In fact, the adversary do es not even have to think hard: any input s/he gives to randomized Quick-Sort has a non-zero probability of forcing the algorithm through (n^2 ) steps. The reason is that the running time of randomized Quick-Sort dep ends on the internal, random selection of pivots; hence, it is not imp ossible (i.e. there is a p ositive probability) that the selection will b e bad at every single recursive step, partitioning the array into subproblems of sizes n 1 and 1. Also, if the input consists of n rep etitions of the same numb er, then a Quick-Sort which uses the Partition pro cedure shown in class will de nitely take (n^2 ) steps, whether it's deterministic or randomized. Graded by David.
True False The answer is True. In fact, any array (i.e. not necessarily a heap) can b e turned into a min-based heap in linear time. The way to do this is to simply run Build-Heap on the array, which takes linear time. Here, of course, we are using a Build-Heap pro cedure based on a new Heapify which, in turn, guarantees that every parent is no greater than its children. Although it is p ossible to build a min-based heap by extracting the elements of the max-based heap in reverse sorted order, this algorithm takes (n lg n) steps. It is not p ossible to convert a max-based heap into a min-based heap by reversing the elements in the heap array. As a counterexample, consider the heap stored as
5 ; 1 ; 4
This heap app ears on the right side in the gure b elow. Reversing the contents of the array pro duces 4 ; 1 ; 5
which is not a min-based heap since the ro ot, 4, is bigger that its left child, 1. Graded by David.
True False The answer is False. Or, to b e more precise, not necessarily. For example, the left heap in the gure b elow pro duces
5 ; 4 ; 1
during a preorder walk, while the right heap pro duces
5 ; 1 ; 4 :
5
1 1 4
5
4
One counterexample suces to show the fallacy of the claim. Another, more general way to prove that no tree walk (whether inorder, preorder, or p ostorder) can output the no des in reverse sorted order is to simply observe that a heap guarantees only that a parent is no smaller that its children; there is no guarantee as to the relative ordering of the two children. Graded by David.
True False The answer is True. If we have > 1 then the numb er of elements to b e hashed is greater than the numb er of slots available, so we can't use op en addressing. Even when do esn't exceed 1 but it is close to 1 , chaining is much b etter since clustering will make op en addressing very inecient. Students who argued the ineciency of op en addressing for close to 1 but didn't say that op en addressing fails for > 1 lost only 1 p oint. Graded by David.
True False The answer is True. In fact we don't even need the inorder output b ecause we have all the information we need to reconstruct the tree from the preorder output. (Actually, we could get the inorder output by sorting the preorder output, if we really wanted to.) Now, since a preorder traversal outputs the ro ot of the tree b efore either subtree, then to reconstruct the tree we only need to do the following:
Create an array B [0; : : : ; n] of b o oleans, all initialized to False. Go through all elements of A, extracting each A[i] bit by bit and interpreting it as a binary numb er; then set B [A[i]] to True. Finally, go through all elements of I nA, and nd the one, say it's B [j ], which is still False; the missing string is the binary representation of j. Perform k passes over A. During pass j , extract the j th bit of all the elements of A, counting separately the zeros and the ones. If there are more (less) zeros than ones, conclude the pass by setting the j th bit of the missing string to one (zero). Set the missing string M to 0. Go through all elements of A, extracting each A[i] bit by bit and interpreting it as a binary numb er; then X O R this numb er with M , storing the result back in M. When you are done, M contains the missing string. Use a non-comparative sort to order the elements of A, interpreted as binary numb ers, in increasing order. Then lo ok at the least signi cant bits of adjacent elements in the sorted array A. If you ever nd two identical bits in adjacent elements A[i] and A[i + 1], this means that the missing string is A[i] + 1. To cover the cases where either the string 00 : : : 00 or the string 11 : : : 11 is missing, simply compare the rst and last elements of A against 00 : : : 01 and 11 : : : 10 , resp ectively.
Common errors:
RADIX-SORT do es not run in \linear" time Given n input numb ers that are d digits long, radix sort runs in time O (dn + k d), where k = O (2d^ ) is the range of each digit. In our case, d = lg n, hence RADIX sort, runs in time O (n lg n).
Graded by Suresh.
We do an inorder walk with the following mo di cations:
If k ey (x) < a then we do not visit l ef t(x). If k ey (x) > b then we do not visit r ig ht(x).
Assume that the hint is true. We will show that the running time is O (m + h). Partition the \visited" no des according to whether k ey (x) < a, a k ey (x) b or k ey (x) > b. By de nition, there are m no des of the second typ e. By the hint, there are at most h no des of the rst typ e as
they all lie in the same path. Similarly, there are at most h no des of the third typ e. Therefore, the running time is O (h + m).
To prove the hint, assume that y and w are \visited" no des with k ey (y ); k ey (w ) < a and supp ose that their least common ancestor is z. Clearly, z 6 = y ; w. Supp ose that y is to the left of z. Then by the prop erties of the binary search tree, k ey (z ) k ey (w ) < a. But then we get a contradiction as our algorithm will not visit l ef t(z ) and therefore it will not visit y.
Graded by David.