diff --git a/matrix_chain_mul.txt b/matrix_chain_mul.txt new file mode 100644 index 0000000..650a39b --- /dev/null +++ b/matrix_chain_mul.txt @@ -0,0 +1,53 @@ +#include +#include + +// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n +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 + dimension of A[i] is p[i-1] x p[i] */ + + // cost is zero when multiplying one matrix. + for (i=1; i