> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/dynamic-programming/mcm-dp-or-partition-dp/matrix-chain-multiplication.md).

# Matrix Chain Multiplication

## Problem Statement

\
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.

The dimensions of the matrices are given in an array **arr\[]** of size **N** (such that N = number of matrices + 1) where the **ith** matrix has the dimensions **(arr\[i-1] x arr\[i])**.

**Example 1:**

<pre><code><strong>Input: N = 5
</strong>arr = {40, 20, 30, 10, 30}
<strong>Output: 26000
</strong><strong>Explaination: There are 4 matrices of dimension 
</strong>40x20, 20x30, 30x10, 10x30. Say the matrices are 
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D. 
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.
</code></pre>

\
**Example 2:**

<pre><code><strong>Input: N = 4
</strong>arr = {10, 30, 5, 60}
<strong>Output: 4500
</strong><strong>Explaination: The matrices have dimensions 
</strong>10*30, 30*5, 5*60. Say the matrices are A, B 
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The 
number of multiplications are -
10*30*5 + 10*5*60 = 4500.
</code></pre>

\
**Your Task:**\
You do not need to take input or print anything. Your task is to complete the function **matrixMultiplication()** which takes the value **N** and the array **arr\[]** as input parameters and returns the minimum number of multiplication operations needed to be performed.

\
**Expected Time Complexity:** O(N3)\
**Expected Auxiliary Space:** O(N2)

\
**Constraints:** \
2 ≤ N ≤ 100\
1 ≤ arr\[i] ≤ 500\ <br>

## Intuition

```
Recurse through the array 

Partition at each point and find the answer
Basically

i * k * j + Left partition + right Partition

eg- ABC

A = 10*20
B = 20*30
C = 30*40

ABC Total size = 10 * 40

Multiplicaltion is 10*20*40 + let (AB) + let(C)
or 10*20*40 + let (A) + let(BC)

So on 
```

### Links

<https://practice.geeksforgeeks.org/problems/matrix-chain-multiplication0303/1>

### Video Links

<https://www.youtube.com/watch?v=vRVfmbCFW7Y&ab_channel=takeUforward>\
\
<https://www.youtube.com/watch?v=pDCXsbAw5Cg&ab_channel=takeUforward>

### Approach 1:

```
Memoization

dp i j -> refers to value of multiplication count between index i and j
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution{
public:
    int find_ans(int i, int j, int arr[], vector<vector<int>> &dp){
        if(i == j)
            return 0;
        
        if(dp[i][j] != -1)
            return dp[i][j];

        int mini = 1e9;

        for(int k=i; k<j; k++){
            int ans =  arr[i-1] * arr[k] * arr[j] + find_ans(i, k, arr, dp) + find_ans(k+1, j, arr, dp);

            if(mini > ans)
                mini = ans;
        }

        return dp[i][j] = mini;
    }

    int matrixMultiplication(int N, int arr[]){
        vector<vector<int>> dp(N, vector<int> (N,-1));
        
        return find_ans(1, N-1, arr, dp);
    }
};
```

{% endcode %}

### Approach 2:

```
Tabulation
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution{
public:
    int matrixMultiplication(int n, int arr[]){
        vector<vector<int>> dp(n, vector<int> (n,0));
        

        for(int i=n-1; i>=1; i--) {
            for(int j=i+1; j<n; j++) {
                int mini = 1e9;
                for(int k=i; k<j; k++){
                    int ans =  arr[i-1] * arr[k] * arr[j] + dp[i][k] + dp[k+1][j];

                    if(mini > ans)
                        mini = ans;
                }

                dp[i][j] = mini;
            }
        }

        return dp[1][n-1];
    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://coding-9.gitbook.io/untitled/dynamic-programming/mcm-dp-or-partition-dp/matrix-chain-multiplication.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.
