sample solution

advertisement
Homework # 24 sample solution:
8-4.
(a)
part 1: LCS
First we use the dynamic programming technique to compute the length of the LCS of two input
strings x (of length m) and y (of length n), which is stored in the array C[0..m][0..n].
int lengthLCS (string x, int m, string y, int n) {
for i from 0 to m C[i][0] = 0;
for j from 0 to n C[0]j] = 0;
}
for i from 1 to m
for j from 1 to n
if x[i] == y[j]
C[i][j] = C[i-1][j-1] + 1;
else
C[i][j] = max(C[i][j-1], C[i-1][j])
return C[m][n];
The space complexity and time complexity of the above algorithm are both O(mn).
Next, we extract the solution by calling extractSolution(x, y, m, n), which is recursively defined as
follows:
string extractSolution(string x, string y, int i, int j) {
if (i == 0 || j == 0)
return “ ”;
else if x[i] == y[j]
return extractSolution (x, y, i – 1, j – 1) + x[i]; // append x[i]
else if (C[i][j-1] > C[i-1][j])
return extractSolution (x, y, i, j – 1);
else
return extractSolution (x, y, i –1, j);
}
The time complexity of the above algorithm is O(m+n).
Part 2: SCS
Using the array C[0..m][0..n] from the previous problem, it’s easy to get SCS by adding all the
symbols not appearing in the LCS of x and y, as long as it won’t break the order of characters in
their original sequences. That is, we call getSCS(x, y, m, n), which is recursively defined as follows:
string getSCS(x[], y[], i, j) {
if (j == 0) return substring(x, 1, i);
if (i == 0) return substring(y, 1, j);
if (x[i] == y[j]) return getSCS (x, y, i – 1, j –1) + x[i];
if (C[i][j-1] > C[i-1][j])
return getSCS (x, y, i, j –1) + y[j];
else
return getSCS (x, y, i –1, j) + x[i];
}
The time complexity of the above algorithm is O(m+n).
(b)
Prove that d(T, P) = |SCS(T, P)| – |LCS(T, P)|
Proof: Suppose m = |T| and n = |P|. We may prove the claim by induction on m+n.
Base case: m+n = 0, i.e., m = n = 0, or both T and P are empty. Then |SCS(T,P)| = |LCS(T, P)| = d(T,P)
= 0. The claim holds.
Inductive case: Assume as induction hypothesis that the claim is true for all m+n < k. Now we
consider m+n = k > 0. Suppose T = T’ + T[m] if m > 0 and P = P’ + P[n] if n > 0. There are five cases
to consider:
Case #1: m >= 0 and n = 0. |LCS(T, P)| = 0 and d(T,P) = |SCS(T, P)| = |T|. The claim holds.
Case #2: m = 0 and n > 0. |LCS(T, P)| = 0 and d(T,P) = |SCS(T, P)| = |P|. The claim holds.
Case #3: m > 0, n > 0, and T[m] = P[n]. Now d(T, P) = d(T’, P’), |LCS(T,P)| = |LCS(T’,P’)| + 1, and
|SCS(T,P)| = |SCS(T’, P’)| + 1. By induction hypothesis, d(T’,P’) = |SCS(T’,P’)| – |LCS(T’,P’)|, so we
have d(T, P) = |SCS(T, P)| – |LCS(T, P)|.
Case #4: m > 0, n > 0, T[m] != P[n], and C[m][n – 1] > C[m – 1][n]. Now d(T, P) = d(T, P’)+1,
|LCS(T,P)| = |LCS(T,P’)|, and |SCS(T,P)| = |SCS(T, P’)| + 1. By induction hypothesis, d(T,P’) =
|SCS(T,P’)| – |LCS(T,P’)|, so we have d(T, P) = |SCS(T, P)| – |LCS(T, P)|.
Case #5: m > 0, n > 0, T[m] != P[n], and C[m][n – 1] <= C[m – 1][n]. Now d(T, P) = d(T’, P)+1,
|LCS(T,P)| = |LCS(T’,P)|, and |SCS(T,P)| = |SCS(T’, P)| + 1. By induction hypothesis, d(T’,P) =
|SCS(T’,P)| – |LCS(T’,P)|, so we have d(T, P) = |SCS(T, P)| – |LCS(T, P)|.
In all the cases, the claim holds, so the claim is true for all m+n.
Download