mit6_006s20_q3_sol exam +solution
mit6_006s20_q3_sol exam +solution
Solution: Quiz 3
• 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 60 minutes to earn a maximum of 60 points. Do not spend too much time on any
one problem. Skim them all first, and work on them in an 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) 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: Quiz 3 Name
(a) [1 point] Write your name and email address on the cover page.
Solution: OK!
Please solve problems (3), (4), and (5) using dynamic programming. Be sure to define a set
of subproblems, relate the subproblems recursively, argue the relation is acyclic, provide base
cases, construct a solution from the subproblems, and analyze running time. Correct but inefficient
dynamic programs will be awarded significant partial credit.
1. Subproblems
• x(i): the maximum total value of any gameplay on suffix T [i :] for i ∈ {0, . . . , n}
2. Relate
• Left-most playable word either starts with ti or it does not
⎧ti , word may have any length in {1, . . . , 10} (Guess!)⎫
• If playable word starts with
⎨ j ∈ {0, . . . , 10} and ⎬
• x(i) = max {x(i + 1)} ∪ D[T [i : i + j]] + x(i + j) i + j ≤ n and
T [i : i + j] ∈ D
⎩ ⎭
3. Topo
• x(i) only depends on subproblems with strictly larger i, so acyclic
4. Base
• x(n) = 0 (empty gameplay admits no value)
5. Original
• Solve subproblems via recursive top down or iterative bottom up
• x(0) is the maximum value of any gameplay on T
• Store parent pointers to reconstruct an optimal gameplay
6. Time
• # subproblems: n + 1 = O(n)
• Work per subproblem: expected O(1)
• Together with hash table construction, yields expected O(n + m) time
• (See scratch S2 for common mistakes)
6.006 Solution: Quiz 3 Name 5
1. Subproblems
• Fix an arbitrary order on A = (a0 , . . . , an−1 ) and B = (b0 , . . . , bn−1 )
• xA (i, k): Boolean whether k is sum of any subset of suffix of A[i :] (without repeats)
• xB (i, k): Boolean whether k is sum of any subset of suffix of B[i :] (allowing repeats)
• for i ∈ {0, . . . , n}, k ∈ {0, . . . m}
2. Relate
• Either use ai once or not (cannot use again)
xA (i + 1, k − ai ) if ai ≤ k
• xA (i, k) = OR
xA (i + 1, k) always
• Either use bj once or not (but may use again)
xB (i, k − bj ) if bj ≤ k
• xB (i, k) = OR
xB (i + 1, k) always
3. Topo
• Subproblems xA (i, k) and xB (i, k) each depend only on subproblems
with strictly smaller k − i, so acyclic
4. Base
• xs (i, 0) = True for s ∈ {A, B}, i ∈ {0, . . . , n} (can always make zero sum)
• xs (n, k) = False for s ∈ {A, B}, k ∈ {1, . . . , m} (cannot make positive sum)
5. Original
• Solve subproblems via recursive top down or iterative bottom up
• Original is whether a subset of A and a possibly-repeating subset of B sum to m
• i.e., OR{AND{xA (0, k), xB (0, m − k)} | k ∈ {0, . . . , m}}
6. Time
• # subproblems: (n + 1)(m + 1) = O(nm)
• Work per subproblem: O(1)
• Original takes O(m) time
• O(nm) running time in total
• (See scratch S2 for common mistakes)
6 6.006 Solution: Quiz 3 Name
1. Subproblems
• Let the students be i ∈ {1, . . . , 3n}
• x(i, j, k): the maximum benefit assigning students {1, . . . , i + j + k} to breakout rooms,
with i students to breakout a, j students to breakout b, and k students to breakout c,
where each student is assigned to a breakout room with strictly positive benefit
(or equals −∞ if no such assignment is possible)
• for i, j, k ∈ {0, . . . , n}
2. Relate
• Must assign student i + j + k to some room (Guess!)
⎧ ⎫
⎨ ai+j+k + x(i − 1, j, k) if i > 0 and ai+j+k > 0, ⎬
• x(i, j, k) = max{−∞} ∪ bi+j+k + x(i, j − 1, k) if j > 0 and bi+j+k > 0,
ci+j+k + x(i, j, k − 1) if k > 0 and ci+j+k > 0
⎩ ⎭
3. Topo
• Subproblem x(i, j, k) depends only on strictly smaller i + j + k, so acyclic
4. Base
• x(0, 0, 0) = 0 (no benefit to assigning zero students)
5. Original
• Solve subproblems via recursive top down or iterative bottom up
• x(n, n, n) is the maximum benefit to assign all students evenly to rooms
6. Time
• # subproblems: (n + 1)3 = O(n3 )
• Work per subproblem: O(1)
• O(n3 ) running time in total
• (See scratch S2 for common mistakes)
6.006 Solution: Quiz 3 Name 7
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.
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.
MIT OpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms