0% found this document useful (0 votes)
528 views2 pages

Chap 15

Matrix-chain multiplication problem exhibits optimal substructure. Solution: Matrix-Chain-Multiply(A, s, i, j) if I >= j then return 0 else k - s[i,j] + pi-1 pj pk. Problem: reconstruct an LCS from the completed c table and the original sequences X = and Y = in O(m + n) time, without using the b table

Uploaded by

Sujeet Bansal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
528 views2 pages

Chap 15

Matrix-chain multiplication problem exhibits optimal substructure. Solution: Matrix-Chain-Multiply(A, s, i, j) if I >= j then return 0 else k - s[i,j] + pi-1 pj pk. Problem: reconstruct an LCS from the completed c table and the original sequences X = and Y = in O(m + n) time, without using the b table

Uploaded by

Sujeet Bansal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

THEORY OF ALGORITHMS

SOLUTIONS TO THE PROBLEMS

BAOJIAN HUA

MAY 23,2004

15.2-1 Find an optimal parenthesization of a matrix-chain product whose se-


quence of dimensions is < 5, 10, 3, 12, 5, 50, 6 >.
Solution. Straightforward.

15.2-2 Give a recursive algorithm Matrix-Chain-Multiply(A, s, i, j) that actually


performs the optimal matrix-chain multiplication, given the sequence of matrices
< A1 , . . . , An >, the s table computed by Matrix-Chain-Order, and the indices i
and j.(The initial call would be Matrix-Chain-Multiply(A, s, 1, n))
Solution.

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.3-3 Consider a variant of the matrix-chain multiplication problem in which


the goal is to parenthesize the sequence of matrices so as to maximize, rather than
minimize, the number of scalar multiplication. Does this problem exhibit optimal
substructure?
Solution. Yes. And the proof is similar as before.

15.4-1 Determine an LCS of (1, 0, 0, 1, 0, 1, 0, 1) and (0, 1, 0, 1, 1, 0, 1, 1, 0).


Solution. Straightforward.

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.


You might also like