hw2-sol
hw2-sol
Problem 1
Consider a sequence A = (a1 , a2 , . . . , an ) consisting of distinct positive integers. Let each element ai of
A, where i = 2k for some positive integer k, be positioned in the sorted list of A such that it is either (i)
at its sorted position or (ii) within two positions (either to the left or right) from its sorted position.
For example, given a sequence A := (a1 , . . . , a10 ), the possible positions of the element a2 in the sorted
list of A can be 1, 2, 3, 4.
Show that the Ω(n log n) lower bound for the general comparison sort problem still holds for the con-
strained input described above. Intermediate steps in mathematical proof are required.
Solution
Suppose the decision tree if of height h with l reachable leaves corresponding to a comparison sort on n
elements a1 , ..., an .
Each element ax , where x is the exact power of 2, is at most two positions away from its correct position.
This implies that there are at most 5 possible positions for ax (x = 2i for some integer i) in the sorted
list. In an array of n operations there are ⌊log n⌋ (we use log to represent log2 here) exact powers
of 2, namely 2, 4, ..., 2⌊log n⌋ . Therefore, the total number of possible permutations is no greater than
5⌊log n⌋ · (n − ⌊log n⌋)!. Each of the permutations of the input would appear as at least one leaf in the
tree. So we have 5⌊log n⌋ · (n − ⌊log n⌋)! ≤ l ≤ 2h .
h ≥ log(5⌊log n⌋ · (n − ⌊log n⌋)!)
≥ log((n − ⌊log n⌋)!) (because 5⌊log n⌋ ≥ 1)
(It is difficult to prove the lower bound directly from this point. We will consider another case as an
intermediate step.)
Let consider another ”nearly” sorted case, where every other element is already at the correct position
starting from the first element. That is, elements a1 , a3 , a5 , ... are already at the correct positions. In
this case, we can easily derive the inequality
n
(⌊ ⌋)! ≤ l ≤ 2h .
2
Then we have
n
h ≥ log((⌊ ⌋)!)
r2
n n n 1
= log( 2π ( 2 ) 2 (1 + Θ( n )))
2 e 2
1 n n 2
= log(πn) + log( ) + log(1 + Θ( )) (using Stirling’s approximation. can also start from the definition of facto
2 2 2e n
n n n
≥ log − log e
2 2 2
= Ω((n/2) log(n/2))
= Ω(n log n)
It is easy to see that (⌊ n2 ⌋)! ≤ (n − ⌊log n⌋)! (intuitively, there are less elements to be sorted in the newly
introduced “nearly” sorted case.) Therefore, we have
h ≥ log((n − ⌊log n⌋)!)
n
≥ (⌊ ⌋)!
2
= Ω(n log n)
1
Problem 2
Consider two stacks A and B manipulated using the following operations (n is the size of A and m the
size of B, and both stacks are empty at the beginning):
(a) What is the worst-case running time of the operations M ultiP opA, M ultiP opB and T ransf er?
(b) Using accounting method to calculate the amortized cost for each of these five operations. You also
need to show that
(1) the total amortized cost is no less than the actual total cost, and
(2) the total amortized cost is bounded by O(m + n).
Solution
(a) Consider M ultiP opA and M ultiP opB have to pop the entire stack, the running time of each opera-
tion is the size of the stack, so worst case M ultiP opA runs in O(n) and M ultiP opB runs in O(m) time.
T ransf er operation pops elements off of A and pushes them onto B. The worst case is to transfer the
entire stack A, which involves n pops and n pushes for a worst case running time of O(n).
• M ultiP opA(k): 0
• M ultiP opB(k): 0
• T ransf er(k): 0
The charge of P ushA(x) can pay for its actual cost (which is 1) and also prepay for T ransf er and
M ultiP opA. Each element pushed on to A will (1) stay on the stack (no other cost); (2) be popped by
M ultiP opA; or (3) be transferred to B by T ransf er. We overcharge P ushA(x) by 3, which can cover the
future cost of popping the element (which will be 1) or transfer the element(which will be 2). Similarly,
the charge of P ushB(x) can pay for its actual cost (which is 1) and also prepay for future pop. Therefore,
the amortized cost is no less than the actual cost.
In addition, the amortized cost is at most 4 · (m + n) = O(m + n). (There is a typo in the problem. It
should be “bounded by O(n + m)”)
Common error: Note that it is incorrect by setting the amortized cost of P ushA(x) less than 3 and
the amortized cost of T ransf er(k) a constant (0 or 2). It is because the overcharge of P ushA(x) may
not cover the future cost of transfer that element. In addition, T ransf er(k) is one operation, not k
operations (though there are k pops and k pushes in the process of this operation.)