Chap 15
Chap 15
BAOJIAN HUA
MAY 23,2004
Matrix-Chain-Multiply(A, s, i, j)
if i ≥ j then
return 0
else k ← s[i, j]
return (Matrix-Chain-Multiply(A, s, i, k)+
Matrix-Chain-Multiply(A, s, k + 1, j) + pi−1 pj pk )
15.4-2 Show how to reconstruct an LCS from the completed c table and the
original sequences X =< x1 , x2 , . . . , xm > and Y =< y1 , y2 , . . . , yn > in O(m + n)
time, without using the b table.
Solution.
1
2 BAOJIAN HUA MAY 23,2004
Print-LCS(X, Y, c, i, j)
if i < 1 or j < 1 then
return
if X[i] = Y [j] then
Print-LCS(X, Y, c, i − 1, j − 1)
print "X[i]"
else if c[i − 1, j] ≥ c[i, j − 1] then
Print-LCS(X, Y, c, i − 1, j)
else Print-LCS(X, Y, c, i, j − 1)
Notice that the initial call is Print-LCS(X, Y, c, m, n)
15.5-2
Solution. Straightforward.