Consider a recursive solution:
Let p = <p0, p1, .., pn> be the sequence of dimensions.
Recursive-Matrix-Chain(p,i,j) if i = j then return 0 m[i,j] = for k = i to j - 1 q = Recursive-Matrix-Chain(p,i,k) + Recursive-Matrix-Chain(p,k+1,j) + P[i-1]P[k]P[j] if q < m[i,j] then m[i,j] = q return m[i,j]
Analysis: