Pages

Tuesday 31 July 2012

Matrix Chain Multiplication

Matrix-Chain(array p[1..n]) {
array s[1..n-1,2..n]
for i = 1 to n do m[i,i] = 0; // initialize
for L = 2 to n do { // L = length of subchain
for i = 1 to n-L+1 do {
j = i + L - 1;
m[i,j] = INFINITY;
for k = i to j-1 do { // check all splits
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;
}
}
}
}
return m[1,n] (final cost) and s (splitting markers);
}

References :
1) http://www.cs.sunysb.edu/~jgao/CSE548-fall07/David-mount-DP.pdf
2) http://www.geeksforgeeks.org/archives/15553

No comments:

Post a Comment