P1
Consider the Longest Common Subsequence (LCS) from the notes.
We phrased the solving of this problem as a sequence of choices:
add&chop, chop-X and chop-Y.
I stated that no greedy choice works for this. Find
counterexamples for the following two greedy choice strategies:
-
Strategy 1:
choose add&chop if the last letters of X and Y are
equal, and otherwise chose to chop whichever of X and Y is
longer.
-
Strategy 2:
choose add&chop if the last letters of X and Y are
equal, and otherwise
- set S to the set of letters appearing in both X and Y
- set dX to the number of chop-X's needed to get to
where the last character of X is an element of S
- set dY to the number of chop-Y's needed to get to
where the last character of Y is an element of S
- if dX < dY choose chop-Y else choose chop-X
P2
Consider the LCS memoized and dynamic programming algorithms
from the lecture notes. They create a table E with 1's for
add&chop, 2's for chop-X and 3's for chop-Y. Below is the
table E produced for input
X := fortunefavors
Y := effortrewards
Using the table, recover the longest common subsequence.
| E | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 0 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
| 1 | -1 | 2 | 1 | 1 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
| 2 | -1 | 2 | 2 | 2 | 1 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
| 3 | -1 | 2 | 2 | 2 | 2 | 1 | 3 | 1 | 3 | 3 | 3 | 1 | 3 | 3 |
| 4 | -1 | 2 | 2 | 2 | 2 | 2 | 1 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
| 5 | -1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 6 | -1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 7 | -1 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 1 | 3 | 3 | 3 | 3 | 3 |
| 8 | -1 | 2 | 1 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 9 | -1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 1 | 3 | 3 | 3 |
| 10 | -1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 11 | -1 | 2 | 2 | 2 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 12 | -1 | 2 | 2 | 2 | 2 | 1 | 2 | 1 | 2 | 2 | 2 | 1 | 3 | 3 |
| 13 | -1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 1 |