// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
Matrix-Chain-Order(int p[])
{
// length[p] = n + 1
n = p.length - 1;
// m[i,j] = Minimum number of scalar multiplications (i.e., cost)
// needed to compute the matrix A[i]A[i+1]...A[j] = A[i..j]
// cost is zero when multiplying one matrix
for (i = 1; i <= n; i++)
m[i,i] = 0;
for (L=2; L<=n; L++) { // L is chain length
for (i=1; i<=n-L+1; i++) {
j = i+L-1;
m[i,j] = MAXINT;
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;
// s[i,j] = Second auxiliary table that stores k
// k = Index that achieved optimal cost
s[i,j] = k;
}
}
}
}
}
這是矩陣查寧乘法 的僞碼我不能明白這部分矩陣鏈mutiplication動態編程
for (L=2; L<=n; L++) { // L is chain length
for (i=1; i<=n-L+1; i++) {
j = i+L-1;
m[i,j] = MAXINT;
爲什麼採取i到小於或等於(N-L + 1) 和j = I + L-1
是所有的初學者
感謝名單這是真的useful..i不能給+1我天璣足夠的聲譽 –
你可以「接受」他的回答...... –
這是一個全面的答案。我對這個限制感到非常困惑。現在我完全理解了。謝謝! – mon