MatrixChainMultiplication(
intdim[],
inti,
intj) {
13 // base case: one matrix 17 // stores minimum number of scalar multiplications (i.e., cost) 18 // needed to compute the matrix M[i+1]...M[j] = M[i..j] 21 // if dp[i][j] is not calculated (calculate it!!) 23 if(
dp[i][j] == 0) {
24 // take the minimum over each possible position at which the 25 // sequence of matrices can be split 27 for(
intk = i + 1;
k<= j - 1;
k++) {
28 // recur for M[i+1]..M[k] to get a i x k matrix 29 intcost = MatrixChainMultiplication(dim, i, k);
31 // recur for M[k+1]..M[j] to get a k x j matrix 32cost += MatrixChainMultiplication(dim, k, j);
34 // cost to multiply two (i x k) and (k x j) matrix 35cost += dim[i] * dim[
k] * dim[j];
38min = cost;
// store the minimum cost 43 // return min cost to multiply M[j+1]..M[j] 49 // Matrix i has Dimensions dim[i-1] & dim[i] for i=1..n 50 // input is 10 x 30 matrix, 30 x 5 matrix, 5 x 60 matrix 51 intdim[] = {10, 30, 5, 60};
52 intn =
sizeof(dim) /
sizeof(dim[0]);
54 // Function Calling: MatrixChainMultiplications(dimensions_array, starting, 57cout <<
"Minimum cost is "<< MatrixChainMultiplication(dim, 0, n - 1)
double k(double x)
Another test function.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4