Design and Analysis of Computer Algorithms
Midterm Exam Solution
13:10-15:00 pm
Monday, April 14, 2008
Name:
ID #:
This is a Close Book examination. Only an A4 cheating sheet belonging to you is acceptable.
You can write your answers in English or Chinese.
Maximum
Score
Problem 1
24
24
Problem 2
16
16
Problem 3
10
10
Problem 4
10
10
Problem 5
15
15
Problem 6
15
15
Problem 7
10
10
Total
100
100
1
1. (24 pts) Please consider the following equalities. Mark by T(=true) or F(=False) each of the
following statements. You don’t need to prove it.
P √
3
(1) ni=1 i = O(n 2 )
(2) nn = O(2n )
(3) n! = O(nn )
(4) n2 log n = Θ(n2 )
(5) n2 / log n = Θ(n2 )
(6) 33n3 + 4n2 = Ω(n2 )
Question
(1)
(2)
(3)
(4)
(5)
(6)
Answer
T
F
T
F
F
T
2. (16 pts) Suppose A is a problem with a well-known lower bound Ω(T (n)) and B is a problem
of which the lower bound is unknown. However, we can find a O(n)-time reduction from A
to B. Mark by T(=true) or F(=False) each of the following statements:
(1) If B can be solved in O(n2 ), then A can be solved in O(n2 ).
(2) If A can be solved in O(n2 ), then B can be solved in O(n2 ).
(3) If T (n) = log n, then B has an Ω(log n) time lower bound.
(4) If B has an Ω(log n) time lower bound, then T (n) = log n.
Question
(1)
(2)
(3)
(4)
Answer
T
F
F
F
2
3. (10 pts) Use a recursion tree to determine a good asymptotic upper bound on the recurrence
2n
n
T (n) = T ( ) + T ( ) + O(n).
3
3
Furthermore, use the substitution method to verify your answer.
Sol:
The recursion tree is in Figure 1. Summing up all the terms on the right-hand side of the
tree, we can guess
cn
c(n/3)
c(n/9)
cn
c(2n/3)
c(2n/9)
cn
c(2n/9)
c(4n/9)
log3/2 n
Total: O(n log n)
Figure 1: The recursion tree for the recurrence relation in problem 4.
T (n) ≤ O(cn log 3 n) = O(n log n).
2
Now, we verify this by the substitution method. Suppose T (n) ≤ dn log n, for some constant
d. Then, assume T (k) ≤ dk log k for k < n. Then,
2n
n
T (n) ≤ T ( ) + T ( ) + cn
3
3
n
n
2n
2n
≤ d log + d log
+ cn
3
3
3
3
2
= dnlog n − dn(log 3 − ) + cn
3
≤ dn log n,
as long as d ≥
c
.
log 3− 23
Therefore, T (n) = O(n log n).
4. (10pts) Show that if n is a power of 2, say n = 2k , then
k
X
i=0
log
n
= Θ(log2 n).
2i
3
Sol:
k
X
log
i=0
n
= (k + 1) log n − (1 + 2 + · · · + k)
2i
= (k + 1)k −
k(k + 1)
2
k(k + 1)
2
log n(log n + 1)
=
2
2
= Θ(log n)
=
5. (15 pts) Given three disjoint sets [1] = {1, 4, 7}, [2] = {3, 6, 9}, and [5] = {8, 10, 11, 12}
represented by the rooted trees as shown in Figure 2, please perform the following operations
in order and show the results by drawing. Suppose we apply the union-by-rank and path
compression heuristics. The ranks of the nodes in Figure 2 are their current heights in the
trees respectively. The operations are:
1
4
2
7
3
5
6
9
8
10
11
12
Figure 2: The disjoint sets represented by rooted trees.
(1) Union(1,9)
(2) Find-set(11)
(3) Union(7,10)
Sol:
(1) The result of Union(1,9) is shown in Figure 3.
(2) Figure 4 presents the resulting trees after the operation Find-set(11).
(3) The result of Union(7,10) is shown in Figure 5.
4
2
1
3
4
5
6
9
8
10
11
7
12
Figure 3: Union(1,9).
2
1
4
3
5
6
9
8
11
10
12
7
Figure 4: Find-set(11)=[5].
5
2
7
1
3
11
6
9
8
10
12
4
Figure 5: Union(7,10).
5
6. (15 pts) Let T be an n-node minimum heap that contains n distinct items. Given any integer
k ≤ n, please provide an O(k log k) time approach to select the kth smallest item in T .
Sol:
We can use an auxiliary heap, say H that initially contains only the item at the root of
T . Recall that the operation Extract-Min on a heap deletes and simultaneously returns its
smallest element. We iterate k − 1 times the following step:
We do an Extract-Min on H, say it returns the item at node v of T . We insert in H the
items at the two children of v (if v has children).
The final Extract-Min on H returns the kth smallest element of T .
Time Complexity: We perform O(k) operations on H, so the total time taken by the algorithm
is O(k log k).
Correctness: First, observe that, after the end of the ith iteration, we have the following:
(a) The items that were put in H form a contiguous (connected) subtree T ′ of T that includes
the root of T .
(b) The items that are currently in H are the leaves of T ′ ,
(c) The items that were removed from H by previous Extract-Min operations are the internal
nodes of T ′ and are all smaller that any item currently in H.
(d) An item x of T that was not yet put in H is the descendant in T of an item currently
in H (hence x is larger than that element of H).
The above implies that, after ith iteration, the (i + 1)st smallest item is in H and will be
returned by the next Extract-Min that is performed on H. By setting i = k − 1, we conclude
that the kth Extract-Min operation that we perform does return the kth smallest element in
T.
7. (10pts) Design a data structure to support the following two operations for a set S of integers:
(a) INSERT(S,x) inserts x into set S.
(b) DELETE-LARGER-HALF(S) deletes the largest ⌈|S|/2⌉ elements from S.
Explain how to implement this data structure so that any sequence of m operations runs in
O(m) time. (In this problem, you can assume that there exists a linear time algorithm for
finding the median for an m-element array.)
Sol:
We can use a simple array A to be our underlying data structure. Array A contains a set S
of integers. The operations are as follows.
INSERT(S,x): The element can be inserted to the last element in A.
DELETE-LARGER-HALF(S): First, find the median mid of S using array A. We then
partition S into two subsets, S1 and S2 , where all the elements in S1 are less than mid
and the elements in S2 are larger than and equal to mid by comparing each element in
A with mid. Last, we delete S2 and set S = S1 .
6
Suppose there are m operations performed on S. For each i = 1, 2, . . . , n, let ci be the actual
cost of the ith operation and Ai be the data structure that results after applying the ith
operation to data structure Ai−1 . Since we use the linear time algorithm for finding the
median, the running time of the algorithm for discarding the larger half is O(m) for an melement array. Suppose the actual time for discarding the larger half is km for some constant
k. We define the potential function Φ on each data structure Ai to be
Φ(Ai ) = 2k × |Ai |,
where |Ai | is the number of elements in array Ai . For an operation i, we analyze the cost by
considering the following two cases.
INSERT(S,x): The actual cost ci for insertion is 1. Then, the amortized cost
ĉi = ci + Φ(Ai ) − Φ(Ai−1 )
= 1 + 2k × |Ai | − 2k × |Ai−1 |
= 1 + 2k,
since |Ai | = |Ai−1 | + 1 when inserting an element to Ai−1 . The amortized cost therefore
is O(1).
DELETE-LARGER-HALF(S): As mentioned above, the actual cost for deleting the larger
half is km if there are m elements. Then, the amortized cost
ĉi = ci + Φ(Ai ) − Φ(Ai−1 )
= k × |Ai−1 | + 2k × |Ai | − 2k × |Ai−1 |
1
= k × |Ai−1 | + 2k × |Ai−1 | − 2k × |Ai−1 |
2
= k × |Ai−1 | + k × |Ai−1 | − 2k × |Ai−1 |
=0
since |Ai | = 12 |Ai−1 | when deleting the larger half of Ai−1 . The amortized cost therefore
is 0.
Therefore, the m operations run in O(m) time.
7
© Copyright 2026 Paperzz