






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 above 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 first half of the lg^3 terms — or, to be precise, the first m = b log 23 nc terms — are each at least as big as n lg^3
n = (n lg^3 n)/ 8 — or, to be 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 appearing as exercise 4.4-2 on page 72 of CLR. It is also possible to solve this problem using the tree method. However, we should be careful to show both a lower and an upper asymptotic bound 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 be done by considering the cummulative cost of just half the tree levels, in a manner similar to our previous derivation using the iteration method. 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 does 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 be approximated as O(lg^3 n). A simple way to analyse this would be to observe that each term is O(lg^3 n) and there are O(lg n) such terms, yielding an upper bound of O(lg^4 n). Graded by Suresh.
Using the tree method 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 be unbalanced and that the subtree corresponding to T 3 (bn/ 2 c) will be deeper (i.e. it will have more levels). At all levels after the shallow subtree — the one corresponding to T 3 (bn/ 4 c) — becomes exhausted, each level will cost no more than
( 3 4
)i n. So we have
T 3 (n) ≤
∑^ ∞
i=
( 3 4
)i 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 loose. There are O(lg n) levels, but the cost at each level decreases. In fact, the cost at level i is n
( 3 4
)i
. Summing up, we obtain the above bound of O(n).
Graded by Suresh.
The answer is True. In fact, the adversary does 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 depends on the internal, random selection of pivots; hence, it is not impossible (i.e. there is a positive probability) that the selection will be bad at every single recursive step, partitioning the array into subproblems of sizes n − 1 and 1. Also, if the input consists of n repetitions of the same number, then a Quick-Sort which uses the Partition procedure shown in class will definitely take Ω(n^2 ) steps, whether it’s deterministic or randomized. Graded by David.
The answer is True. In fact, any array (i.e. not necessarily a heap) can be 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 procedure based on a new Heapify which, in turn, guarantees that every parent is no greater than its children. Although it is possible 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 possible 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 appears on the right side in the figure below. Reversing the contents of the array produces 4 , 1 , 5 which is not a min-based heap since the root, 4, is bigger that its left child, 1. Graded by David.
The answer is False. Or, to be more precise, not necessarily. For example, the left heap in the figure below produces
5 , 4 , 1
during a preorder walk, while the right heap produces
5 , 1 , 4.
5
1 1 4
5
4
One counterexample suffices to show the fallacy of the claim. Another, more general way to prove that no tree walk (whether inorder, preorder, or postorder) can output the nodes 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.
The answer is True. If we have α > 1 then the number of elements to be hashed is greater than the number of slots available, so we can’t use open addressing. Even when α doesn’t exceed 1 but it is close to 1 , chaining is much better since clustering will make open addressing very inefficient. Students who argued the inefficiency of open addressing for α close to 1 but didn’t say that open addressing fails for α > 1 lost only 1 point. Graded by David.
The answer is True. In fact we don’t even need the inorder output because 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 root of the tree before either subtree, then to reconstruct the tree we only need to do the following:
Common errors:
RADIX-SORT does not run in “linear” time Given n input numbers that are d digits long, radix sort runs in time O(dn + kd), 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 modifications:
Assume that the hint is true. We will show that the running time is O(m + h). Partition the “visited” nodes according to whether key(x) < a, a ≤ key(x) ≤ b or key(x) > b. By definition, there are m nodes of the second type. By the hint, there are at most h nodes of the first type as
they all lie in the same path. Similarly, there are at most h nodes of the third type. Therefore, the running time is O(h + m).
To prove the hint, assume that y and w are “visited” nodes with key(y), key(w) < a and suppose that their least common ancestor is z. Clearly, z 6 = y, w. Suppose that y is to the left of z. Then by the properties of the binary search tree, key(z) ≤ key(w) < a. But then we get a contradiction as our algorithm will not visit lef t(z) and therefore it will not visit y.
Graded by David.