# Matrix Multiplication Parenthesization

Question

Example

Pic - subproblem tree, memo table, topo order

![](/files/-Ltpq_64jnFzaSoRaEXw)

![](/files/-Ltpq_66sB6NP8VbRY_S)

Analysis - substring

Recurrence

going from outermost

```
DP(i, j) = min { DP(i, k) + DP(k, j) + Cost(A[i:k] * A[k:j]) }
           for k from i to j
We want to find the splitting point k where the cost is the minimum
```

Top-down

```
Initialization
for all j >= i, C(i, j) = 0 //half table is 0

// the topological order is increasing substring size s
// we go diagonally
for (s from 1 to n-1)
    for (i from 1 to n-s)
      j = i + s
      C(i, j) = min {C(i, k) + C(k+1, j) + Cost(A[i:k] * A[k+1:j]) for i <= k <= j}
return C(1, n)
```

O(n^2) subproblems and O(n) to compute each subproblem so O(n^3)

Bottom-up

## References

6.006 [Lecture 21 Notes](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec21_orig.pdf)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zedive.gitbook.io/project-l/part-2/dynamic_programming/substring-dp/matrix-multiplication-parenthesization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
