8.dp Lcs Soln PDF
8.dp Lcs Soln PDF
6. Complete the following table to nd the length of the LCS of tycoon and country using your mem-
oized solution. (The row and column headed with an , denoting the empty string, are for the trivial
cases!)
SOLUTION:
7. Go back to the table and extract the actual LCS from it. Circle each entry of the table you have to
inspect in constructing the LCS. Then, use the space below to write an algorithm that extracts the
actual LCS from an LLCS table.
SOLUTION: We've italicized (rather than circled) the entries used above. (In fact, we could equiva-
lently have gone some slightly dierent routes around the "ou" in "country" and the "oo" in "tycoon".)
Critically, the table tells us the value of the recurrence at each cell, and the recurrence tells us which
cell we need next to reconstruct more of the solution. Our understanding of the recurrence tells us
that when it adds one to the length, that's because we've found one letter of the LCS itself. Otherwise,
the LCS at the next cell is the same as the LCS at the current one.
3
8. Give a dynamic programming solution that produces the same table as the memoized solution.
SOLUTION: We need to nd an order to traverse the table such that by the time we require the
value of any table cell, it has already been calculated. There are many working orders, but we'll ll
in the table column by column, from top to bottom. We don't need to initialize the table entries in
this algorithm, we ll them in directly as we go.
for j from 1 to m do
Soln[0][j ] ← 0
9. Analyze the eciency of your memoized (part 5) and dynamic programming (part 8) algorithms in
terms of runtime and memory use (not including the space used by the parameters). You may assume
the strings are of length n and m, where n≤m (without loss of generality).
SOLUTION:
Both the memoized and dynamic programming solutions ll out each cell of the table exactly once.
How long does it take to ll out that cell? Not counting recursive calls (for the memoized solution),
lling out a table cell takes constant time: the max over three simple expressions. There are n∗m
table cells.
Assuming each table entry takes constant space, both versions also take O(nm) space. (The memoized
version uses additional space for the call stack, but the deepest the call stack gets is O(n + m), which
is dominated by the table size.)
The Explain-LCS algorithm stops as soon as it reaches a base case, which takes O(n + m) steps. It
never takes a "wrong" turn, and so this is also its runtime. (It uses only constant space beyond the
already-stored table, but it does critically rely on that table, as we've currently designed it.)
10. If we only want the length of the LCS of A and B with lengths n and m, where n ≤ m, explain how
we can "get away" with using only O(n) memory in the dynamic programming solution.
SOLUTION: As in our previous dynamic programming problem, we can store only a constant
number of entries along one dimension of the table. In this case, each cell requires the entries above,
to the left of, and diagonally above and to the left of itself. Given the traversal order we chose for our
DP algorithm above, we can store just one old "column" of the table (one space left of the current
column). The recurrence never requires looking further back than that.
At any time, then, we'll have two columns in memory: the current and previous ones. Each column
has n+1 entries, for O(n) memory.
4
Challenge
1. Give a LCS algorithm that runs in the same asymptotic runtime as the one above, uses only O(m + n)
space (note that this is potentially more than the "space-ecient" version mentioned above), and
returns not only the length of the LCS but the LCS itself. (Note: try this for yourself for a while,
and then walk through the description of the awesome algorithm in section 6.7 if you need help.)
SOLUTION:
We don't usually give solutions to challenge problems, but in this case we will make an exception and
point you to
https://imada.sdu.dk/∼rolf/Edu/DM823/E16/Hirschberg.pdf