Handout 36: Final Exam Solutions: Problem 1. Recurrences
Handout 36: Final Exam Solutions: Problem 1. Recurrences
√
(a) T (n) = 2T (n/8) + 3
n.
Solution: Θ(n).
√ " √
Solution: Θ n3 / M . The recursion tree has approximately lg n − lg M =
!
! √ "
lg n/ M levels. In the tree, every internal node has 8 children, each with cost
√ ! √ "3
O(1). At the bottom of the tree, there are approximately 8lg(n/ M ) = n/ M
leaves. Since#each leaf costs$ M , and the total cost is dominated by the leaves, the
! √ "3 √ "
solution is Θ M n/ M = Θ n3 / M .
!
Handout 36: Final Exam Solutions 3
Heapsort B: O(n)
Strassen’s D: O(n2 )
Bellman-Ford E: O(n2 lg n)
Floyd-Warshall G: O(nlg 7 )
Johnson’s H: O(n3 )
Prim’s I: O(n3 lg n)
T (n) = 4T (n/2) + n2 .
Solution: By the master method, we know T (n) = Θ(n2 lg n). Therefore, our induction hypoth-
esis is T (m) ≥ cm2 lg m for all m < n.
For m = 1, we have the base case that T (1) = 1 > c12 lg 1 for all c > 0.
For the inductive step, assume for all m < n, T (m) ≥ cm2 lg m. The induction hypothesis yields
T (n) = 4T (n/2) + n2
# $2 # $
n n
≥ 4c lg + n2
2 2
= cn2 lg n − cn2 lg 2 + n2
= cn2 lg n + (1 − c)n2 .
For c < 1, this quantity is always greater than cn2 lg n. Therefore, T (n) = Ω(cn2 lg n).
Handout 36: Final Exam Solutions 5
T F Let A1 , A2 , and A3 be three sorted arrays of n real numbers (all distinct). In the comparison
model, constructing a balanced binary search tree of the set A1 ∪A2 ∪A3 requires Ω(n lg n)
time.
Solution: False. First, merge the three arrays, A1 , A2 , and A3 in O(n) time. Second,
construct a balanced binary search tree from the merged array: the median of the array
is the root; recursively build the left subtree from the first half of the array and the right
subtree from the second half of the array. The resulting running time is T (n) = 2T (n/2)+
O(1) = O(n).
T F Let T be a complete binary tree with n nodes. Finding a path from the root of T to a given
vertex v ∈ T using breadth-first search takes O(lg n) time.
Solution: False. Breadth-first search requires Ω(n) time. Breadth-first search examines
each node in the tree in breadth-first order. The vertex v could well be the last vertex
explored. (Also, notice that T is not necessarily sorted.)
Handout 36: Final Exam Solutions 6
T F Given an unsorted array A[1 . . n] of n integers, building a max-heap out of the elements of
A can be performed asymptotically faster than building a red-black tree out of the elements
of A.
Solution: True. Building a heap takes O(n) time, as described in CLRS. On the other
hand, building a red-black tree takes Ω(n lg n) time, since it is possible to produce a sorted
list of elements from a red-black tree in O(n) time by doing an in-order tree walk (and
sorting requires Ω(n lg n) time in a comparison model).
T F Suppose we use a hash function h to hash n distinct keys into an array T of length m.
Assuming simple uniform hashing, the expected number of colliding pairs of elements is
Θ(n2 /m).
Solution: True. Let Xi,j be an indicator random variable equal to 1 if elements i and j
collide, and equal to 0 otherwise. Simple uniform hashing means that the probability of
element i hashing to slot k is 1/m. Therefore, the probability that i and j both hash to the
same slot Pr(Xi,j ) = 1/m. Hence, E [Xi,j ] = 1/m. We now use linearity of expectation
to sum over all possible pairs i and j:
⎡ ⎤
n )
n
E [number of colliding pairs] =
)
E⎣ Xi,j ⎦
i=1 j=i+1
n
) )n
= E [Xi,j ]
i=1 j=i+1
)n ) n
= 1/m
i=1 j=i+1
n(n + 1)
=
2m
= Θ(n2 /m)
Handout 36: Final Exam Solutions 7
Solution: True. Let d be the depth of the network. Since there are n inputs to the
network, there can be at most nd comparators. (One way of seeing this is by the pigeon-
hole principle: if there are n wires and more than nd comparators, then some wire must
traverse more than d comparators, resulting in a depth > d.)
In the comparison-based model, it is possible to simulate the sorting network one compara-
tor at a time. The running time is equal to the number of comparators (times a constant
factor overhead). Therefore, every sorting network must have at least Ω(n lg n) compara-
tors, by the lower-bound for sorting in the comparison-based model.
Therefore, nd > Ω(n lg n), implying that the depth d is Ω(lg n).
Solution: False. The property which implies that locally optimal solutions are globally
optimal is the greedy-choice property. Consider as a counterexample the edit distance
problem. This problem has optimal substructure, as was shown on the problem set, how-
ever a locally optimal solution—making the best edit next—does not result in a globally
optimal solution.
Handout 36: Final Exam Solutions 8
Solution: False. The technique of reweighting preserves the shortest path by assigning
a value h(v) to each vertex v ∈ V , and using this to calculate new weights for the edges:
w̃(u, v) = w(u, v) + h(u) − h(v). However, to determine values for h(v) such that the
edge weights are all non-negative, we use Bellman-Ford to solve the resulting system
of difference constraints. Since the technique of reweighting relies on Bellman-Ford, it
cannot run faster than Bellman-Ford.
Handout 36: Final Exam Solutions 9
(a) Assign the keys 2, 3, 5, 7, 11, 13, 17, 19 to the nodes of the binary search tree below so
that they satisfy the binary-search-tree property.
nil nil
nil nil
nil nil
(b) Explain why this binary search tree cannot be colored to form a legal red-black tree.
(c) The binary search tree can be transformed into a red-black tree by performing a single
rotation. Draw the red-black tree that results, labeling each node with “red” or “black.”
Include the keys from part (a).
Solution: Rotate right around the root. There are several valid ways to color the
resulting tree. Here is one possible answer (all nodes are colored black except for 7).
5 points for the correct answer. 2 points for the correct rotation but incorrect coloring.
Handout 36: Final Exam Solutions 12
Solution: There are several ways to solve this problem in Θ(n) time. You can find the median k
of B using the deterministic Θ(n) select algorithm and partition B around the median k into two
equal sized sets Blow and Bhigh . Assign A[1] ← k. Then for each i > 1, if i is even, assign an
element from Bhigh to A[i], otherwise assign an element from Blow to A[i]. Since all elements in
Bhigh are greater than or equal to all elements in Blow and the median is less than or equal to all
elements in Bhigh , the array is wiggly. The overall running time is Θ(n).
10 points for correct Θ(n) solution and correct analysis. 8-9 points for correct solution but missing
a minor step in the analysis.
5 points for correct Θ(n lg n) solution and correct analysis. 2-4 points for correct solution but
incomplete analysis.
Handout 36: Final Exam Solutions 13
x1 − x4 ≤ −1
x1 − x5 ≤ −4
x2 − x1 ≤ −4
x2 − x3 = −9
x3 − x1 ≤ 5
x3 − x5 ≤ 2
x4 − x3 ≤ −3
x5 − x1 ≤ 5
x5 − x4 ≤ 1
Solution: No solution to this system exists because the constraint graph has a negative-
weight cycle. For example, x1 ❀ x3 ❀ x4 ❀ x5 ❀ x1 has weight 5−3+1−4 = −1.
A full-credit solution (6 points) must exhibit the negative weight cycle.
Handout 36: Final Exam Solutions 14
i=0
To add 1 (modulo 2k ) to x, we use the following procedure:
I NCREMENT (A, k)
1 i←0
2 while i < k and A[i] = 1
3 do A[i] ← 0
4 i← i+1
5 if i < k
6 then A[i] ← 1
Given a number x, define the potential Φ(x) of x to be the number of 1’s in the binary representation
of x. For example, Φ(19) = 3, because 19 = 100112. Use a potential-function argument to prove
that the amortized cost of an increment is O(1), where the initial value in the counter is x = 0.
Solution: Φ(x) is a valid potential function the number of 1’s in the binary representation of x is
always nonnegative, i.e., Φ(x) ≥ 0 for all x. Since the initial value of the counter is 0, Φ 0 = 0.
Let ck be the real cost of the operation I NCREMENT (A, k), and let dk be the number of times the
while loop body in Lines 3 and 4 execute (i.e., dk is the number of consecutive 1’s counting from
the least-significant bit of the binary representation of x). If we assume that executing all of Lines
1, 2, 5, and 6 require unit cost, and executing the body of the while loop requires unit cost, then
the real cost is ck = 1 + dk ,
The potential decreases by one every time the while loop is executed. Therefore, the change in
potential, ∆Φ = Φk − Φk−1 is at most 1 − dk . More specifically, ∆Φ = 1 − dk if we execute Line
6 , and ∆Φ = −dk if we do not.
Thus, using the formula for amortized cost, we get
cˆk = ck + ∆Φ
= 1 + dk + ∆Φ
≤ 1 + dk + 1 − dk
= 2.
Solution: Proof by contradiction. Assume for the sake of contradiction that (v i , vi+1 ) does be-
long to the minimum spanning tree T . Removing (vi , vi+1 ) from T divides T into two connected
components P and Q, where some nodes of the given cycle are in P and some are in Q. For any
cycle, at least two edges must cross this cut, and therefore there is some other edge (v j , vj+1) on
the cycle, such that adding this edge connects P and Q again and creates another spanning tree T ′ .
Since the weight of (vj , vj+1 ) is less than (vi , vi+1 ), the weight of T ′ is less than T and T cannot
be a minimum spanning tree. Contradiction.
Handout 36: Final Exam Solutions 16
T RANS(X, Y, N)
1 for i ← 1 to N
2 do for j ← 1 to N
3 do Y [j, i] ← X[i, j]
Consider the cache-oblivious two-level memory model with a cache of M elements and blocks of
B elements. Assume that both matrices X and Y are stored in row-major order, that is, the linear
order of X in memory is X[1, 1], X[1, 2], . . . , X[1, N], X[2, 1], X[2, 2], . . . , X[2, N], . . . , X[N, 1],
X[N, 2], . . . , X[N, N], and similarly for Y .
(a) Analyze the number MT(N) of memory transfers incurred by T RANS when N ≫ M.
Solution: Since the loop scans through matrix X one row at a time, the number of
memory transfers required for accessing X is O(N 2 /B). We incur O(N 2 ) memory
transfers to access Y , however, because Y is accessed one column at a time. When we
access the block containing Y [j, i], since N ≫ M and we scan through all of column
i before accessing Y [j + 1, i], each access to Y may incur a memory transfer.
Therefore, MT (N) = O(N 2 ).
Handout 36: Final Exam Solutions 18
Now, consider the following divide-and-conquer algorithm for computing the transpose:
R-T RANS(X, Y, N)
1 if N = 1
2 then Y [1, 1] ← X[1, 1]
3 else Partition X into four (N/2) × (N/2) submatrices X11 , X12 , X21 , and X22 .
4 Partition Y into four (N/2) × (N/2) submatrices Y11 , Y12 , Y21 , and Y22 .
5 R-T RANS (X11 , Y11 , N/2)
6 R-T RANS (X12 , Y21 , N/2)
7 R-T RANS (X21 , Y12 , N/2)
8 R-T RANS (X22 , Y22 , N/2)
(b) Give and solve a recurrence for the number MT(N) of memory transfers incurred by
R-T RANS when N ≫ M.
The recursion tree has approximately lg N − lg( M/c) levels. Since every level
+
, √ -2
M N c
MT (N) = √
B M
2
cN
=
B, -
N2
= O .
B
Handout 36: Final Exam Solutions 19
P-T RANS(X, Y, N)
1 if N = 1
2 then Y [1, 1] ← X[1, 1]
3 else Partition X into four (N/2) × (N/2) submatrices X11 , X12 , X21 , and X22 .
4 Partition Y into four (N/2) × (N/2) submatrices Y11 , Y12 , Y21 , and Y22 .
5 spawn P-T RANS (X11 , Y11 , N/2)
6 spawn P-T RANS (X12 , Y21 , N/2)
7 spawn P-T RANS (X21 , Y12 , N/2)
8 spawn P-T RANS (X22 , Y22 , N/2)
9 sync
(c) Give and solve recurrences describing the work T1 (N) and critical-path length T∞ (N)
of P-T RANS . What is the asymptotic parallelism of the algorithm?
Solution:
N
# $
T1 (N) = 4T1 + O(1)
2
= Θ(N 2 ).
N
# $
T∞ (N) = T∞ + O(1)
2
= Θ(lg N).
Professor Kellogg inadvertantly places two additional sync statements into his code as follows:
K-T RANS(X, Y, N)
1 if N = 1
2 then Y [1, 1] ← X[1, 1]
3 else Partition X into four (N/2) × (N/2) submatrices X11 , X12 , X21 , and X22 .
4 Partition Y into four (N/2) × (N/2) submatrices Y11 , Y12 , Y21 , and Y22 .
5 spawn K-T RANS (X11 , Y11 , N/2)
6 sync
7 spawn K-T RANS (X12 , Y21 , N/2)
8 sync
9 spawn K-T RANS (X21 , Y12 , N/2)
10 spawn K-T RANS (X22 , Y22 , N/2)
11 sync
(d) Give and solve recurrences describing the work T1 (N) and critical-path length T∞ (N)
of K-T RANS . What is the asymptotic parallelism of this algorithm?
The critical path increases, however, because we only solve one of the subproblems in
parallel.
N
# $
T∞ (N) = 3T∞ + O(1)
2
= Θ(N lg 3 ).