Quiz 1 Solutions: and Analysis of Algorithms
Quiz 1 Solutions: and Analysis of Algorithms
Quiz 1 Solutions
• Do not open this quiz booklet until you are directed to do so. Read all the instructions first.
• The quiz contains 7 problems, with multiple parts. You have 120 minutes to earn 120 points.
• This quiz booklet contains 12 pages, including this one.
""
• This quiz is closed book. You may use one double-sided letter (8 12 × 11"" ) or A4 crib sheet.
No calculators or programmable devices are permitted. Cell phones must be put away.
• Do not waste time deriving facts that we have studied. Just cite results from class.
• When we ask you to “give an algorithm” in this quiz, describe your algorithm in English
or pseudocode, and provide a short argument for correctness and running time. You do not
need to provide a diagram or example unless it helps make your explanation clearer.
• Do not spend too much time on any one problem. Generally, a problem’s point value is an
indication of how many minutes to spend on it.
• Show your work, as partial credit will be given. You will be graded not only on the correct
ness of your answer, but also on the clarity with which you express it. Please be neat.
• Good luck!
1 True or False 40 10
4 Amortized Analysis 15 1
6 Dynamic Programming 15 2
Total 120
Name:
(a) T F [4 points] With all equal-sized intervals, a greedy algorithm based on earliest
start time will always select the maximum number of compatible intervals.
Solution: True. The algorithm is equivalent to the earliest finish time algorithm.
(b) T F [4 points] The problem of weighted interval scheduling can be solved in O(n log n)
time using dynamic programming.
(c) T F [4 points] If we divide an array into groups of 3, find the median of each group,
recursively find the median of those medians, partition, and recurse, then we can
obtain a linear-time median-finding algorithm.
Solution: False. T (n) = T (n/3) + T (2n/3) + O(n) does not solve to T (n) =
O(n). The array has to be broken up into groups of at least 5 to obtain a linear-
time algorithm.
(d) T F [4 points] If we used the obvious Θ(n2 ) merge algorithm in the divide-and
conquer convex-hull algorithm, the overall time complexity would be Θ(n2 log n).
Solution: False. The time complexity would satisfy the recurrence T (n) =
2T (n/2) + Θ(n2 ), which solves to Θ(n2 ) by the Master Theorem.
(e) T F [4 points] Van Emde Boas sort (where we insert all numbers, find the min, and
then repeatedly call S UCCESSOR) can be used to sort n = lg u numbers in O(lg u·
lg lg lg u) time.
Solution: False. Inserting into the tree and then finding all the successors will
take n lg lg(u) time, which in terms of u is lg(u) · lg lg(u).
6.046J/18.410J Quiz 1 Solutions Name 3
(f) T F [4 points] Van Emde Boas on n integers between 0 and u − 1 supports successor
queries in O(lg lg u) worst-case time using O(n) space.
(g) T F [4 points] In the potential method for amortized analysis, the potential energy
should never go negative.
Solution: True.
(h) T F [4 points] The quicksort algorithm that uses linear-time median finding to run in
worst-case O(n log n) time requires Θ(n) auxiliary space.
(i) T F [4 points] Searching in a skip list takes Θ(log n) time with high probability, but
could take Ω(2n ) time with nonzero probability.
Solution: True. A skip list could be of any height with nonzero probability,
depending on its random choices.
Alternative solution: False. We can limit the height of the skip list to O(n) or
O(lg n) to get O(n) worse-case cost.
Common mistake 1: We go through each element at least once. (Wrong because
we also need to “climb up” the skip lists.
Common mistake 2: The worst case is when none of the elements is promoted,
or all of the elements are promoted to the same level, in which cases skip lists
become a linked list.
x A B C D
h1 (x) 1 0 1 1
h2 (x) 0 1 0 1
h3 (x) 2 2 1 0
C = A ∗ B = (1, 1),
c = F −1 (C) = (1, 0).
So c represents 1 + 0 · x, which is clearly wrong. Point out Ben’s mistake in one sentence; no
calculation needed. (Ben swears he has calculated FFT F and inverse FFT F −1 correctly.)
Solution: The resulting polynomial is of degree 2, so Ben need to pad a and b with zeroes. (Or
Ben need at least 3 samples to do FFT).
Here is the correct calculaton (not required in the solution). Let a = b = (0, 1, 0, 0); then,
Give the fastest data structure you can for this problem, measured according to worst-case time.
The faster your data structure, the better.
Hint: Use a data structure you have seen in either 6.006 or 6.046 as a building block.
Solution: Initialization takes O(n lg(lg(n))) time to insert all the yellow elements into a VEB
tree, V .
More importantly, each operation takes O(lg lg(n)) time. When a user asks to M ARK -Y ELLOW(i),
then call V.insert(i) which takes O(lg lg(n)) time. When a user asks to M ARK -G REY(i), then
call V.delete(i) which takes O(lg lg(n)) time. When a user asks to N EXT-Y ELLOW(i), then call
V.successor(i) which takes O(lg lg(n)) time.
Another slower solution used an AVL tree in place of a vEB for an O(lg(n)) runtime for the
operations.
Common mistake 1: Claiming operations took O(lg lg(u)). The universe size is exactly n, and
the term u was undefined.
Common mistake 2: Inserting both yellow and grey elements into the same data structure without
an augmentation to keep track of whether any yellow elements existed within children. Next-
Yellow could take O(n) in the worst case.
Common mistake 3: Use a skip list to keep track of all yellow elements. Some operations would
take O(lg(n)) with high probability, but O(n) worst case.
Common mistake 4: Using a skip list capped at 2 levels, doubly linked list, or hash table. Some
operations would take O(n) worst case.
6.046J/18.410J Quiz 1 Solutions Name 6
Describe your algorithm and give the worse-case time complexity of the two operations. Then
carry out an amortized analysis to make I NSERT (x, S) run in amortized O(1) time, and R EMOVE
BOTTOM - HALF (S) run in amortized 0 time.
Solution:
Use a singly linked list to store those integers. To implement I NSERT (x, S), we append the new
integer to the end of the linked list. This takes Θ(1) time. To implement R EMOVE - BOTTOM
HALF (S), we use the median finding algorithm taught in class to find the median number, and then
go through the list again to delete all the numbers smaller or equal than the median. This takes
Θ(n) time.
Suppose the runtime of R EMOVE - BOTTOM - HALF (S) is bounded by cn for some constant c. For
amortized analysis, use Φ = 2cn as our potential function. Therefore, the amortized cost of an
insertion is 1 + ΔΦ = 1 + 2c = Θ(1). The amortized cost of R EMOVE - BOTTOM - HALF (S) is
cn + ΔΦ = cn + (−2c × n2 ) = 0.
6.046J/18.410J Quiz 1 Solutions Name 7
(a) [5 points] Describe an O(n)-time randomized algorithm for testing whether p(x) ·
Solution: Pick a value a ∈ [1, 4n], and check whether p(a)q(a) = r(a). The algo
rithm outputs YES if the two sides are equal, and NO otherwise. It takes O(n) time to
evaluate the three polynomials of degree O(n). Thus the overall running time of the
algorithm is O(n).
Solution: If p(x) · q(x) = r(x), then both sides will evaluate to the same thing for
any input.
6.046J/18.410J Quiz 1 Solutions Name 8
Solution: s(x) = r(x) − p(x) · q(x) is a degree-2n polynomial, and thus has at most
2n roots. Then
2n 1
Pr{s(a) = 0} ≤ =
4n 2
(d) [5 points] Design a randomized algorithm to check whether p(x) · q(x) = r(x) that is
correct with probability at least 1 − ε. Analyze your algorithm in terms of n and 1/ε.
Solution: We run part a m times, and output YES if and only if all answers output
YES . In other words, we amplify the probability of success via repetition.
m
Our test works with probability ≥ 1 − 12 . Thus we need
� �m
1
≤ε
2
1
⇒ m ≥ log .
ε
6.046J/18.410J Quiz 1 Solutions Name 9
Because Prof. Child needs all four quadrants to be non-empty, she can only stand on cells (i, j)
where 1 < i < n and 1 < j < m.
(a) [10 points] Define TLi,j to be maximum tastiness value in the top-left quadrant of
algorithm to compute TLi,j , for all 1 < i < n and 1 < j < m, in O(nm) time.
Solution: When trying to calculate TLi,j , we see that the maximum can be at cell
(i, j). If not, it must lie either in the rectangle from (1, 1) to (i, j − 1), or the rectangle
from (1, 1) to (i − 1, j), or both. These three overlapping cases cover our required
rectangle. We have then,
For the base cases, we can just set TL0,j = TLi,0 = 0 for all valid values of i and j.
We can compute the DP value for each state in O(1) time. There are nm states, so
our algorithm is O(nm).
6.046J/18.410J Quiz 1 Solutions Name 10
(b) [5 points] Use the idea in part (a) to obtain an O(nm) algorithm to find the tastiest
dish.
Solution: In part (a) we calculated range maximum for the top-left quadrant. We
can similarly define range maximums for the other quadrants. Let BLi,j = max{Ta,b |
To calculate the tastiest dish Prof. Child can cook when she stands at cell (i, j) (1 <
i < n and 1 < j < m), we now just need to compute the product TLi−1,j−1 BLi+1,j−1 TR i−1,j+1 BR i+1,j+1
and pick the maximum product. This can be done in O(nm) time.
Solution: Merge the two sorted arrays (which takes O(m + n) time) and find the
median using linear-time selection.
(c) [7 points] Give an algorithm that runs in O(lg(min{m, n})) time, for any m and n.
Don’t spend too much time on this question!
Solution: Without loss of generality, assume |A| = m > n = |B|. We can safely
remove elements A[0 : m−2
n
] and A[ m+
2
n
: m − 1] because none of these elements can
be the median of A + B. After this process, we get two arrays of size approximately
n. Then we can run part (b). The complexity is Θ(lg(min(m, n)))
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.