Introduction to Algorithms. (2nd edition) Thomas Cormen_ Charles Leiserson_ Ronad Rivest (811417), страница 62
Текст из файла (страница 62)
For the matrix-chain multiplication problem, we pick as our subproblems theproblems of determining the minimum cost of a parenthesization of Ai Ai+1 Aj for 1 ≤ i ≤ j ≤ n.Let m[i, j] be the minimum number of scalar multiplications needed to compute the matrixAi j; for the full problem, the cost of a cheapest way to compute A1 n would thus be m[1, n].We can define m[i, j] recursively as follows. If i = j, the problem is trivial; the chain consistsof just one matrix Ai i = Ai, so that no scalar multiplications are necessary to compute theproduct. Thus, m[i, i] = 0 for i = 1, 2, ..., n.
To compute m[i, j] when i < j, we take advantageof the structure of an optimal solution from step 1. Let us assume that the optimalparenthesization splits the product Ai Ai+1 Aj between Ak and Ak+1, where i ≤ k < j. Then, m[i, j]is equal to the minimum cost for computing the subproducts Ai k and Ak+1 j, plus the cost ofmultiplying these two matrices together. Recalling that each matrix Ai is pi-1 × pi, we see thatcomputing the matrix product AiobtainkAk+1 j takes pi-1 pk pj scalar multiplications.
Thus, wem[i, j ] = m[i, k] + m[k + 1, j ] + pi-1 pk pj.This recursive equation assumes that we know the value of k, which we do not. There are onlyj - i possible values for k, however, namely k = i, i + 1, ..., j - 1. Since the optimalparenthesization must use one of these values for k, we need only check them all to find thebest.
Thus, our recursive definition for the minimum cost of parenthesizing the product Ai Ai+1Aj becomes(15.12)The m[i, j] values give the costs of optimal solutions to subproblems. To help us keep track ofhow to construct an optimal solution, let us define s[i, j] to be a value of k at which we cansplit the product Ai Ai+1 Aj to obtain an optimal parenthesization. That is, s[i, j] equals a valuek such that m[i, j] = m[i, k] + m[k + 1, j] + pi-1 pk pj.Step 3: Computing the optimal costsAt this point, it is a simple matter to write a recursive algorithm based on recurrence (15.12)to compute the minimum cost m[1, n] for multiplying A1 A2 An. As we shall see in Section15.3, however, this algorithm takes exponential time, which is no better than the brute-forcemethod of checking each way of parenthesizing the product.The important observation that we can make at this point is that we have relatively fewsubproblems: one problem for each choice of i and j satisfying 1 ≤ i ≤ j ≤ n, orinall.
A recursive algorithm may encounter each subproblem many times in different branchesof its recursion tree. This property of overlapping subproblems is the second hallmark of theapplicability of dynamic programming (the first hallmark being optimal substructure).Instead of computing the solution to recurrence (15.12) recursively, we perform the third stepof the dynamic-programming paradigm and compute the optimal cost by using a tabular,bottom-up approach.
The following pseudocode assumes that matrix Ai has dimensions pi-1 ×pi for i = 1, 2, ..., n. The input is a sequence p = p0, p1, ..., pn , where length[p] = n + 1. Theprocedure uses an auxiliary table m[1 n, 1 n] for storing the m[i, j] costs and an auxiliarytable s[1 n, 1 n] that records which index of k achieved the optimal cost in computingm[i, j].
We will use the table s to construct an optimal solution.In order to correctly implement the bottom-up approach, we must determine which entries ofthe table are used in computing m[i, j]. Equation (15.12) shows that the cost m[i, j] ofcomputing a matrix-chain product of j - i + 1 matrices depends only on the costs of computingmatrix-chain products of fewer than j - i + 1 matrices. That is, for k = i, i + 1, ..., j - 1, thematrix Ai k is a product of k - i + 1 < j - i + 1 matrices and the matrix Ak+1 j is a product of j k < j - i + 1 matrices. Thus, the algorithm should fill in the table m in a manner thatcorresponds to solving the parenthesization problem on matrix chains of increasing length.MATRIX-CHAIN-ORDER(p)1 n ← length[p] - 12 for i ← 1 to n3do m[i, i] ← 04 for l ← 2 to n▹l is the chain length.5do for i ← 1 to n - l + 16do j ← i + l - 17m[i, j] ← ∞8for k ← i to j - 19do q ← m[i, k] + m[k + 1, j] + pi-1 pkpj10if q < m[i, j]11then m[i, j] ← q12s[i, j] ← k13 return m and sThe algorithm first computes m[i, i] ← 0 for i = 1, 2, ..., n (the minimum costs for chains oflength 1) in lines 2–3.
It then uses recurrence (15.12) to compute m[i, i + 1] for i = 1, 2, ..., n 1 (the minimum costs for chains of length l = 2) during the first execution of the loop in lines4–12. The second time through the loop, it computes m[i, i + 2] for i = 1, 2, ..., n - 2 (theminimum costs for chains of length l = 3), and so forth. At each step, the m[i, j] costcomputed in lines 9–12 depends only on table entries m[i, k] and m[k + 1, j] alreadycomputed.Figure 15.3 illustrates this procedure on a chain of n = 6 matrices. Since we have defined m[i,j] only for i ≤ j, only the portion of the table m strictly above the main diagonal is used.
Thefigure shows the table rotated to make the main diagonal run horizontally. The matrix chain islisted along the bottom. Using this layout, the minimum cost m[i, j] for multiplying a subchainAi Ai+1 Aj of matrices can be found at the intersection of lines running northeast from Ai andnorthwest from Aj. Each horizontal row in the table contains the entries for matrix chains ofthe same length.
MATRIX-CHAIN-ORDER computes the rows from bottom to top and fromleft to right within each row. An entry m[i, j] is computed using the products pi-1 pk pj for k = i,i + 1, ..., j - 1 and all entries southwest and southeast from m[i, j].Figure 15.3: The m and s tables computed by MATRIX-CHAIN-ORDER for n = 6 and thefollowing matrix dimensions:matrix dimensionA1A2A3A4A5A630 × 3535 × 1515 × 55 × 1010 × 2020 × 25The tables are rotated so that the main diagonal runs horizontally.
Only the main diagonal andupper triangle are used in the m table, and only the upper triangle is used in the s table. Theminimum number of scalar multiplications to multiply the 6 matrices is m[1, 6] = 15,125. Ofthe darker entries, the pairs that have the same shading are taken together in line 9 whencomputingA simple inspection of the nested loop structure of MATRIX-CHAIN-ORDER yields arunning time of O(n3) for the algorithm. The loops are nested three deep, and each loop index(l, i, and k) takes on at most n -1 values. Exercise 15.2-4 asks you to show that the runningtime of this algorithm is in fact also Ω(n3).
The algorithm requires Θ(n2) space to store the mand s tables. Thus, MATRIX-CHAIN-ORDER is much more efficient than the exponentialtime method of enumerating all possible parenthesizations and checking each one.Step 4: Constructing an optimal solutionAlthough MATRIX-CHAIN-ORDER determines the optimal number of scalarmultiplications needed to compute a matrix-chain product, it does not directly show how tomultiply the matrices. It is not difficult to construct an optimal solution from the computedinformation stored in the table s[1 n, 1 n]. Each entry s[i, j] records the value of k suchthat the optimal parenthesization of Ai Ai+1 ··· Aj splits the product between Ak and Ak+1. Thus,we know that the final matrix multiplication in computing A1 n optimally is A1 s[1,n] As[1,n]+1 n.The earlier matrix multiplications can be computed recursively, since s[1, s[1, n]] determinesthe last matrix multiplication in computing A1 s[1,n], and s[s[1, n] + 1, n] determines the lastmatrix multiplication in computing As[1,n]+1 n.
The following recursive procedure prints anoptimal parenthesization of Ai, Ai+1, ..., Aj , given the s table computed by MATRIXCHAIN-ORDER and the indices i and j. The initial call PRINT-OPTIMAL-PARENS(s, 1, n)prints an optimal parenthesization of A1, A2, ..., An .PRINT-OPTIMAL-PARENS(s, i, j)1 if i = j2then print "A"i3else print "("4PRINT-OPTIMAL-PARENS(s, i, s[i, j])5PRINT-OPTIMAL-PARENS(s, s[i, j] + 1, j)6print ")"In the example of Figure 15.3, the call PRINT-OPTIMAL-PARENS(s, 1, 6) prints theparenthesization ((A1 (A2 A3)) ((A4 A5)A6)).Exercises 15.2-1Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is5, 10, 3, 12, 5, 50, 6 .Exercises 15.2-2Give a recursive algorithm MATRIX-CHAIN-MULTIPLY(A, s, i, j) that actually performsthe optimal matrix-chain multiplication, given the sequence of matrices A1, A2, ..., An , thes table computed by MATRIX-CHAIN-ORDER, and the indices i and j.