MIT6 006F11 Quiz1 Sol
MIT6 006F11 Quiz1 Sol
MIT6 006F11 Quiz1 Sol
Quiz 1 Solutions
Problem 1. [2 points] Write your name on top of each page.
(a) [10 points] Rank the following functions by increasing order of growth. That is, find
any arrangement g1 , g2 , g3 , g4 , g5 , g6 , g7 , g8 of the functions satisfying g1 = O(g2 ),
g2 = O(g3 ), g3 = O(g4 ), g4 = O(g5 ), g5 = O(g6 ), g6 = O(g7 ), g7 = O(g8 ).
π n n √
f1 (n) = n f2 (n) = π f3 (n) = f4 (n) = 2 n
5
n 4 2 4 n
f5 (n) = f6 (n) = 2log n
f7 (n) = n5(log n) f8 (n) = n
n−4 4
Solution: Draw recursion tree. At each level, do Θ(n) work. Number of levels is
log3/2 n = Θ(lg n), so guess T (n) = Θ(n lg n) and use the substitution method to
verify guess.
6.006 Quiz 1 Solutions Name 2
(c) [5 points] Find an asymptotic solution of the following recurrence. Express your
answer using Θ-notation, and give a brief justification.
√
T (n) = log n + T n
(a) T F [2 points] Binary insertion sorting (insertion sort that uses binary search to find
each insertion point) requires O(n log n) total operations.
Solution: False. While binary insertion sorting improves the time it takes to
find the right position for the next element being inserted, it may still take O(n)
time to perform the swaps necessary to shift it into place. This results in an O(n2 )
running time, the same as that of insertion sort.
(b) T F [2 points] In the merge-sort execution tree, roughly the same amount of work is
done at each level of the tree.
Solution: True. At the top level, roughly n work is done to merge all n ele-
ments. At the next level, there are two branches, each doing roughly n/2 work to
merge n/2 elements. In total, roughly n work is done on that level. This pattern
continues on through to the leaves, where a constant amount of work is done on
n leaves, resulting in roughly n work being done on the leaf level, as well.
(c) T F [2 points] In a BST, we can find the next smallest element to a given element in
O(1) time.
Solution: False. Finding the next smallest element, the predecessor, may re-
quire traveling down the height of the tree, making the running time O(h).
(d) T F [2 points] In an AVL tree, during the insert operation there are at most two
rotations needed.
(f) T F [2 points] In a min-heap, the next largest element of any element can be found
in O(log n) time.
6.006 Quiz 1 Solutions Name 4
Solution: False. A min-heap cannot provide the next largest element in O(log n)
time. To find the next largest element, we need to do a linear, O(n), search
through the heap’s array.
(g) T F [2 points] The multiplication method satisfies the simple uniform hashing as-
sumption.
Solution: False. We don’t really know of hash functions that satisfy the simple
uniform hashing assumption.
Solution: False. The notes state that double hashing ‘comes close.’ Double
hashing only provides n2 permutations, not n!.
(i) T F [2 points] Python generators can be used to iterate over potentially infinite count-
able sets with O(1) memory.
Solution: True. Python generators do not require the whole set to reside in
memory to iterate over it, making this assertion true.
6.006 Quiz 1 Solutions Name 5
1. Examine all of the values in the first, middle, and last columns of the matrix to find the
maximum location .
2. If is a peak within the current subproblem, return it. Otherwise, it must have a neighbor p
that is strictly greater.
3. If p lies to the left of the central column, restrict the problem matrix to the left half of the ma-
trix, including the first and middle columns. If p lies to the right of the central column, restrict
the problem matrix to the right half of the matrix, including the middle and last columns.
4. Repeat steps 1 through 3 looking at the first, middle, and last rows.
5. Repeat steps 1 through 4 until a peak is found.
Consider the 5 × 5 example depicted below. On this example, the algorithm initially examines the
first, third, and fifth columns, and finds the maximum in all three. In this case, the maximum is the
number 4. The number 4 is not a peak, due to its neighbor 5.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 5 0 0 0 4 5 0 0 0 4 5 0 0 0
0 0 1 2 0 0 0 1 2 0 0 0 1 2 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 6 0 7 0 0 6 0 7 0 0 6 0 7 0
The number 5 is to the left of the middle column, so we restrict our view to just the left half of the
matrix. (Note that we include both the first and middle columns.) Because we examined columns
in the previous step, we now examine the first, middle, and last rows of the submatrix. The largest
value still visible in those rows is 6, which is a peak within the subproblem. Hence, the algorithm
will find the peak 6.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 5 0 0 0 4 5 0 0 0 4 5 0 0 0
0 0 1 2 0 0 0 1 2 0 0 0 1 2 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 6 0 7 0 0 6 0 7 0 0 6 0 7 0
6.006 Quiz 1 Solutions Name 6
Solution: Let S(m, n) be the runtime of the algorithm when run on an m × n matrix
starting with columns. Let T (m, n) be the runtime of the algorithm when run on
an m × n matrix starting with rows. Then S(m, n) ≤ T (m, n/2 + 1) + Θ(m) and
T (m, n) ≤ S(m/2+1, n)+Θ(n). Hence, S(m, n) ≤ Θ(m+n)+S(m/2+1, n/2+1).
When we resolve this recurrence relation, we get S(m, n) = O(m + n). In the case
of a square n × n matrix, we get an asymptotic runtime of Θ(n).
(b) [15 points] Does Alyssa’s algorithm return a peak in all cases? If so, give a short
proof of correctness. Otherwise, provide a counterexample for the algorithm.
Solution: The following is an example of a matrix where the algorithm will return
the wrong value:
0 0 0 0 0
4 5 0 0 0
0 0 1 2 0
0 0 0 0 0
0 0 0 7 0
6.006 Quiz 1 Solutions Name 7
(a) [10 points] You observe that, as in the comparison model, any algorithm can be
viewed as a decision tree where a node corresponds to an experiment with two out-
comes (contaminated or not) and thus two children. Prove a lower bound of Ω(k lg nk )
on the number of experiments that must be done to save the world. Assume that
lg x! ∼ x lg x and that lg(n − k) ∼ lg n (which is reasonable when k < 0.99n).
Solution:
n The number of possible outcomes—whichn cities are contaminated—is
k
. Thus any decision tree must have at least k leaves. Because a decision tree
is binary, it must therefore have height at least
n n!
lg = lg ,
k k!(n − k)!
(b) [10 points] Save the world by designing an algorithm to determine which k of the n
cities have contaminated water supplies using O(k lg n) experiments. Describe and
analyze your algorithm.
Solution: The algorithm is based on divide and conquer: divide the n cities into two
groups of size n/2; test each group for contamination (using two experiments); and
recurse into each contaminated group. The recursion tree has exactly k leaves, and the
height of the tree is at most lg n, so the number of internal nodes leading to the leaves
is at most k lg n. Each internal node costs 2, for a total cost of O(k lg n).
In fact, it is possible to prove an O(k lg nk ) bound on the same algorithm. To minimize
the number of shared nodes among the k paths from root to leaves, the worst case is
when the recursion tree branches for the first lg k levels (to get enough leaves), and
then has k straight paths for the number of levels: lg n − lg k = lg nk . There are O(k)
nodes in the top branching, and O(k lg nk ) nodes in the bottom paths.
6.006 Quiz 1 Solutions Name 9
(a) [5 points] Write pseudo-code for a data structure that supports the following two
operations.
I NIT(N, K, P ) — preprocesses the P [1 . . . N ] array of prices, in O(K ·N K ) expected
time, using O(K · N K ) space, to be able to answer the query below.
BAG(S) — in O(1) expected time, determines whether K of the items have prices
summing to S, and if so, returns K indices b1 , b2 , . . . , bK such that S = K
i=1 P [bi ].
Solution:
I NIT(N, K, p)
1 h ← empty hash table
2 for c ← S UBSETS(K, {1 . . . N })
3 do s ← K i=1 Pci
4 h[s] ← c
BAG(S)
1 if S ∈ h
2 then return h[S]
3 else return N IL
6.006 Quiz 1 Solutions Name 10
(b) [10 points] Write pseudo-code for a function P WN -C ONTEST(N, S, K, P ) that deter-
mines whether K of the items have prices summing to S, and if so, returns K indices
b1 , b2 , . . . , bK such that S = K
i=1 P [bi ]. Unlike part (a), P WN -C ONTEST should run
in O(K · N ) and use O(K · N K/2 ) space.
K/2
Solution:
P WN -C ONTEST(N, S, K, p)
1 h ← empty hash table
2 for c ← S UBSETS(K/2, {1 . . . N })
K/2
3 do s ← i=1 P ci
4 h[s] ← c
5 for c ← S UBSETS(K/2, {1 . . . N })
6 do s ← S − K/2 i=1 Pci
7 if s ∈ h
8 then return c + h[s]
9 return N IL
6.006 Quiz 1 Solutions Name 11
(c) [5 points] Analyze the running time of your pseudo-code for the previous part.
Your algorithm should return a span of 4 since A[2] = 6 and A[6] = 7. The next biggest span is
A[4] = 1 to A[7] = 5.
(a) [5 points] Give an O(n)-time algorithm to compute the minimums of the prefix
A[1 . . . k] for each k, and store in MA[k]: MA[k] = minki=1 A[i].
(b) [15 points] Using the MA[i] computed above, give an O(n log n)-time algorithm to
maximize j − i subject to A[i] < A[j].
Hint: The M A is a sorted array.
1
The joke could be along these lines: “You thought time j was bad with A[j]? Back in time i, we only had A[i]!”
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.