|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.mhhe.clrs2e.MatrixChainMultiply
Implements the dynamic-programming algorithm to determine the optimal order in which to multiply a chain of matrices, as described in Section 15.2 of Introduction to Algorithms, Second edition.
Field Summary | |
private int[][] |
m
The value of an optimal solution to a subproblem. |
private int |
n
How many matrices are in the chain. |
private int[][] |
s
The position to split an optimal solution to a subproblem. |
Constructor Summary | |
MatrixChainMultiply(int[] p)
Computes an optimal parenthesization of a matrix-chain product, allocating the instance variables and storing the result in them. |
Method Summary | |
private void |
matrixChainOrder(int[] p)
Computes an optimal parenthesization of a matrix-chain product, storing the result in the instance variables. |
private java.lang.String |
printOptimalParens(int i,
int j)
Returns a String describing an optimal
parenthesization of a subproblem. |
java.lang.String |
toString()
Returns a String describing an optimal
parenthesization of the entire matrix chain. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private int[][] m
m[i][j]
is the minimum number of scalar
multiplications needed to compute Ai
Ai+1 ... Aj, for 1 ≤
i ≤ j ≤ n. Entries
m[i][j]
for i = 0, j = 0, or
i > j are unused.
private int[][] s
s[i][j]
is an index k such that
m[i][j]
= m[i][k]
+
m[k+1][j]
+ p[i-1] * p[k] * p[j]
, for
i = 1, 2, ..., n-1 and j = 2, 3, ...,
n. Entries s[i][j]
for i = 0,
i = n, j ≤ 1, or i >
j are unused.
private int n
Constructor Detail |
public MatrixChainMultiply(int[] p)
p
- An array of dimensions, where matrix
Ai is p[i-1]
x
p[i]
, for i = 1, 2, ..., n.Method Detail |
private void matrixChainOrder(int[] p)
p
- An array of dimensions, where matrix
Ai is p[i-1]
x
p[i]
, for i = 1, 2, ..., n.private java.lang.String printOptimalParens(int i, int j)
String
describing an optimal
parenthesization of a subproblem. Implements the
Print-Optimal-Parens procedure on page 338.
i
- Index of one array.j
- Index of another array.
String
describing an optimal
parenthesization of Ai Ai+1
... Aj.public java.lang.String toString()
String
describing an optimal
parenthesization of the entire matrix chain.
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |