Final Solutions
Final Solutions
Final Solutions
Solution: Final
• Do not open this quiz booklet until directed to do so. Read all the instructions on this page.
• When the quiz begins, write your name on the top of every page of this quiz booklet.
• You have 180 minutes to earn a maximum of 180 points. Do not spend too much time on
any one problem. Skim them all first, and attack them in the order that allows you to make
the most progress.
• You are allowed three double-sided letter-sized sheet with your own notes. No calcula-
tors, cell phones, or other programmable or communication devices are permitted.
• Write your solutions in the space provided. Pages will be scanned and separated for grading.
If you need more space, write “Continued on S1” (or S2, S3, S4, S5, S6, S7) and continue
your solution on the referenced scratch page at the end of the exam.
• Do not waste time and paper rederiving facts that we have studied in lecture, recitation, or
problem sets. Simply cite them.
• When writing an algorithm, a clear description in English will suffice. Pseudo-code is not
required. Be sure to argue that your algorithm is correct, and analyze the asymptotic
running time of your algorithm. Even if your algorithm does not meet a requested bound,
you may receive partial credit for inefficient solutions that are correct.
• Pay close attention to the instructions for each problem. Depending on the problem,
partial credit may be awarded for incomplete answers.
Name:
School Email:
2 6.006 Solution: Final Name
(a) [1 point] Write your name and email address on the cover page.
Solution: OK!
(b) T F A Θ(n10 )-time algorithm will always take at least as long to run as a Θ(log n)-
time algorithm on a problem with input size n.
Solution: False. Only true in the limit of large n. Constant factors could make
a Θ(n10 )-time algorithm faster than a Θ(log n)-time algorithm for small n.
(c) T F Given an array of n distinct comparable integers, we can identify and sort the
n
log n
smallest of them in O(n) time using a heap.
Solution: True. We can build a heap on array in O(n) and pop the k largest in
order in O(k log n) time. For k = n/ log n, this running time is O(n).
4 6.006 Solution: Final Name
(d) T F Given k distinct integer keys, there exists a binary search tree containing all k of
them that satisfies the max heap property.
Solution: True. A chain to the left where each subtree is rooted at its max
element.
(e) T F Checking if a string of length k exists in a hash table takes worst-case O(k) time.
Solution: False. Hash table operations are expected time operations.
(f) T F Using an AVL tree as Dijkstra’s priority queue results in the same asymptotic
running time as using a Fibonacci heap, when running Dijkstra’s algorithm to
compute single source shortest paths on a graph with n vertices and O(n) edges.
Solution: True. An AVL tree will yield a running time of O(|V | log |V |) on
sparse graphs, which is asymptotically equivalent to Dijkstra using a Fibonacci
heap.
(h) T F Given a connected weighted directed graph having positive integer edge weights,
where each edge weight is at most k, we can compute single source shortest paths
in O(k|E|) time.
Solution: True. Replace each edge (a, b) with weight w with a directed
unweighted path from a to b, and run BFS from the source.
(j) T F Let A be a problem that cannot be solved in polynomial time, and let ALG be an
algorithm with optimal running time T (n) to solve a size n instance of another
problem B. If there is an algorithm to solve a size m instance of problem A in
time O(mc ) + T (md ) for some constants c and d by using ALG, then problem B
also cannot be solved in polynomial time.
Solution: True. If T (n) were polynomial in n, then O(mc ) + T (md ) would
be polynomial in m, meaning problem A could be solved in polynomial time,
a contradiction.
6 6.006 Solution: Final Name
(a) [8 points] Given an array A containing n integers, where each integer is in the range
{1, . . . , nm }, describe an O(n · min{m, log n})-time algorithm to sort A.
Solution: Branch on the value of m. If m < log n, sort using radix sort in O(nm) ≤
O(n log n) time; otherwise, use merge sort to sort in O(n log n) ≤ O(nm) time. This
yields the desired running time O(n · min {m, log n}).
Common Mistakes:
• Not comparing m and log n before running algorithms
• i.e., saying to just use the fastest, which doesn’t let you branch preemptively
• Comparing m with a value besides log n, i.e., checked whether m was constant
(b) [8 points] Recall that an array of distinct integers is k-proximate if every integer
of the array is at most k places away from its place in the array after being sorted1 .
Describe an efficient2 in-place algorithm to sort a k-proximate array.
Solution: Insertion sort can sort a k-proximate array in-place in O(nk) time, but we
can do better!
Apply heap sort to sort the first 2k elements in place in O(k log k) time. Since the array
is k-proximate, the first k elements are now guaranteed to be smallest k elements in
sorted order. Now repeatedly use heap sort to sort items k ·i to k(i +2) in place, which
maintains the invariant that before iteration i > 0, items 0 to k · i − 1 are sorted and
items k · i to n is k-proximate, until k(i + 2) > n, or when i = dn/k − 2e. Each of
the O(n/k) loops takes O(k log k) time, leading to O(n log k) time in total.
Common Mistakes:
• Correct in-place O(nk) algorithms got lots of partial credit here
• O(n log k) algorithms that were not in-place received less partial credit
1
If the ith integer of the unsorted input array is the jth largest integer contained in the array, then |i − j| ≤ k.
2
By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.
6.006 Solution: Final Name 7
3
Recall, a graph is simple if every edge in the graph connects two distinct vertices, and there is at most one edge
connecting any vertex pair.
6.006 Solution: Final Name 9
4
Steryl will always continue at the same pace in the same direction along a trail until reaching a junction.
10 6.006 Solution: Final Name
1. Subproblems
• Label the boxes as box 1, box 2, and box 3
• x(i, j, k): a Boolean which is True if two disjoint subsets of the book pages from p1 to
pi can sum to j and k respectively (to be placed in boxes 1 and 2), and False otherwise.
2. Relate
• We make a choice of which of boxes 1, 2, or 3 to place book pages pi
x(i − 1, j − pi , k) if j ≥ pi
• x(i, j, k) = OR x(i − 1, j, k − pi ) if k ≥ pi
x(i − 1, j, k) always
• Subproblems x(i, j, k) depends only on smaller i so acyclic
3. Base
• x(0, 0, 0) = True (no more items and no more pages to fill)
• x(0, j, k) = False for j > 0 or k > 0 (no more items, cannot fill j + k pages)
4. Solution
• Solve subproblems for i ∈ {0, . . . , n} and j, k ∈ {0, . . . , m}
P
• Can pack books into boxes if there is a True x(n, j, k) such that p∈P p −j −k ≤ m
• So loop through all subproblems and evaluate this condition
5. Time
• O(nm2 ) subproblems
• O(1) work per subproblem
• O(nm2 ) work total (including O(nm2 ) work to evaluate condition in solution step)
Common Mistakes:
1. Subproblems
• x(i, −1): max sum of any parity proximate subsequence of (a1 , . . . , ai )
• x(i, j): max sum of any parity proximate subsequence of (a1 , . . . , ai ), ending in a num-
ber which is equal to j modulo 2, which may be alone, for j ∈ {0, 1}
2. Relate
• If j = −1, unrestricted in choice; may take or leave element ai
• If j 6= −1, restricted in choice; still may take or leave element ai
• x(i, −1) = max{x(i − 1, −1), ai + x(i − 1, mod(ai , 2))}
x(i − 1, j),
• x(i, j ∈ {0, 1}) = max
ai + max{x(i − 1, −1), x(i − 1, j)} if mod(ai , 2) = j
• Subproblems x(i, j) only depend on smaller i so acyclic
3. Base
• x(0, −1) = 0 (no more items, so best is no more sum)
• x(0, j) = −∞ for j ∈ {0, 1} (no more items, so can’t choose one!)
4. Solution
• x(n, −1)
5. Time
• O(n) subproblems
• O(1) work per subproblem
• O(n) work total
Common Mistakes:
Return p(r, n0 ) where r is the root of the access AVL tree, which can be computed in O(log n)
time as at most one recursive call is made down the tree. Lastly, remove n0 from the access AVL
tree and insert n. This algorithm runs in worst-case O(log n) time if an AVL tree is used for the
variable dictionary, and in expected O(log n) time if a hash table is used.
Common Mistakes: See S1
14 6.006 Solution: Final Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S1” on the problem statement’s page.
Common Mistakes:
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S2” on the problem statement’s page.
16 6.006 Solution: Final Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S3” on the problem statement’s page.
6.006 Solution: Final Name 17
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S4” on the problem statement’s page.
18 6.006 Solution: Final Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S5” on the problem statement’s page.
6.006 Solution: Final Name 19
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S6” on the problem statement’s page.
20 6.006 Solution: Final Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S7” on the problem statement’s page.