next up previous
Next: Up: Previous:

Recursive Solution

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] = $\infty$
$\;\;\;\;\;$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:


\begin{displaymath}T(n) = \left\{ \begin{array}{ll}
\Theta(1) & n = 1 \\
\The...
...^{n-1} (T(k) + T(n-k) + \Theta(1)) & n > 1
\end{array} \right.\end{displaymath}


\begin{eqnarray*}T(n) & = & \Theta(1) + \sum_{k=1}^{n-1} (T(k) + T(n-k) + \Theta...
..._{k=1}^{n-1} T(k) \\
& = & \Theta(n) + 2 \sum_{k=1}^{n-1} T(k)
\end{eqnarray*}



next up previous
Next: Up: Previous: