https://www.8bitavenue.com/dynamic-programming-matrix-chain-multiplication/
http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
Given a sequence of n matrices A1, A2, ... An, and their dimensions p0, p1, p2, ..., pn, where where i = 1, 2, ..., n, matrix Ai has dimension pi − 1 × pi, determine the order of multiplication that minimizes the the number of scalar multiplications.
http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
Given a sequence of n matrices A1, A2, ... An, and their dimensions p0, p1, p2, ..., pn, where where i = 1, 2, ..., n, matrix Ai has dimension pi − 1 × pi, determine the order of multiplication that minimizes the the number of scalar multiplications.
For 1 ≤ i ≤ j ≤ n, let m[i, j] be the minimum number of scalar multiplications needed to compute the Ai..j. The optimum cost can be described by the following recursive formulation.
Basis: Observe that if i = j then the problem is trivial; the sequence contains only one matrix, and so the cost is 0. (In other words, there is nothing to multiply.) Thus,
m[i, i] = 0 for i = 1, 2, ..., n.
Step: If i ≠ j, then we are asking about the product of the subchain Ai..j and we take advantage of the structure of an optimal solution. We assume that the optimal parenthesization splits the product, Ai..j into for each value of k, 1 ≤ k ≤ n − 1 as Ai..k . Ak+1..j.
The optimum time to compute is m[i, k], and the optimum time to compute is m[k + 1, j]. We may assume that these values have been computed previously and stored in our array. Since Ai..k is a matrix, and Ak+1..j is a matrix, the time to multiply them is pi − 1 . pk . pj. This suggests the following recursive rule for computing m[i, j].
To keep track of optimal subsolutions, we store the value of k in a table s[i, j]. Recall, k is the place at which we split the product Ai..j to get an optimal parenthesization.
有A1A2…An共n个矩阵,第i个矩阵的大小为pi-1*pi,计算代价由标量乘法决定,求最小代价及运算顺序。
比如:
对应的答案是:
15125次乘法
思路
设m[i,j]表示从i到j的矩阵链的最小计算代价,s[i,j]=k表示在i和j中间的矩阵k后面加一个括号,则m的递推方法是:
第一项是前半部分,第二项是后半部分,第三项是前后两部分组合的计算代价。最终m[1,n]就是最小代价。由于是ijk三重遍历,所以复杂度是O(n3)。这只是粗略估计,据《算法导论》介绍,准确的复杂度是。
在递推的同时,记录最小的k即可。那如何利用s[i,j]来得到最终的的运算顺序呢?递归往下打括号就行了:
private static int[][] m = new int[100][100];
private static int[][] s = new int[100][100];
private static int[] p = new int[105];
private static final int MAX = Integer.MAX_VALUE;
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
for (int i = 0; i <= n; i++)
{
p[i] = scan.nextInt();
m[i][i] = 0;
}
for (int l = 2; l <= n; l++)
{
for (int i = 1; i <= n - l + 1; i++)
{
int j = i + l - 1;
m[i][j] = MAX;
for (int k = i; k <= j - 1; k++)
{
int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j])
{
m[i][j] = q;
s[i][j] = k;
}
}
}
}
print(1, n);
System.out.printf(" %d\n", m[1][n]);
}
public static void print(int i, int j)
{
if (i == j)
System.out.print("A" + i);
else
{
System.out.print("(");
print(i, s[i][j]);
print(s[i][j] + 1, j);
System.out.print(")");
}
}
From: http://mytestpjt.googlecode.com/svn/trunk/ASAssembly/src/com/mhhe/clrs2e/MatrixChainMultiply.java
private void matrixChainOrder(int[] p) { // Initial the cost for the empty subproblems. for (int i = 1; i <= n; i++) m[i][i] = 0; // Solve for chains of increasing length l. for (int l = 2; l <= n; l++) { for (int i = 1; i <= n-l+1; i++) { int j = i + l - 1; m[i][j] = Integer.MAX_VALUE; // Check each possible split to see if it's better // than all seen so far. for (int k = i; k < j; k++) { int q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]; if (q < m[i][j]) { // q is the best split for this subproblem so far. m[i][j] = q; s[i][j] = k; } } } } }Also refer to http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
int
MatrixChainOrder(
int
p[],
int
n)
{
/* For simplicity of the program, one extra row and one extra column are
allocated in m[][]. 0th row and 0th column of m[][] are not used */
int
m[n][n];
int
i, j, k, L, q;
/* m[i,j] = Minimum number of scalar multiplications needed to compute
the matrix A[i]A[i+1]...A[j] = A[i..j] where dimention of A[i] is
p[i-1] x p[i] */
// cost is zero when multiplying one matrix.
for
(i = 1; i < n; i++)
m[i][i] = 0;
// L is chain length.
for
(L=2; L<n; L++)
{
for
(i=1; i<=n-L+1; i++)
{
j = i+L-1;
m[i][j] = INT_MAX;
for
(k=i; k<=j-1; k++)
{
// q = cost/scalar multiplications
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if
(q < m[i][j])
m[i][j] = q;
}
}
}
return
m[1][n-1];
}
int
MatrixChainOrder(
int
p[],
int
i,
int
j)
{
if
(i == j)
return
0;
int
k;
int
min = INT_MAX;
int
count;
// place parenthesis at different places between first and last matrix,
// recursively calculate count of multiplcations for each parenthesis
// placement and return the minimum count
for
(k = i; k <j; k++)
{
count = MatrixChainOrder(p, i, k) +
MatrixChainOrder(p, k+1, j) +
p[i-1]*p[k]*p[j];
if
(count < min)
min = count;
}
// Return minimum count
return
min;