πDay 21. Matrix Chain Multiplication π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given an array arr[]
where the i
th matrix has the dimensions (arr[i-1] Γ arr[i]) for i β₯ 1
, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of scalar multiplications.
You need to find the minimum number of multiplications required to multiply the matrices.
π Example Walkthrough:
Example 1:
Input:
arr[] = [2, 1, 3, 4]
Output:
20
Explanation:
We have three matrices:
M1 (2Γ1)
,M2 (1Γ3)
, andM3 (3Γ4)
.There are two ways to multiply them:
((M1 Γ M2) Γ M3)
Cost =
(2 Γ 1 Γ 3) + (2 Γ 3 Γ 4) = 30
(M1 Γ (M2 Γ M3))
Cost =
(1 Γ 3 Γ 4) + (2 Γ 1 Γ 4) = 20
The minimum cost is 20.
Example 2:
Input:
arr[] = [1, 2, 3, 4, 3]
Output:
30
Explanation:
We have four matrices:
M1 (1Γ2)
,M2 (2Γ3)
,M3 (3Γ4)
, andM4 (4Γ3)
.The minimum multiplication cost is 30.
Example 3:
Input:
arr[] = [3, 4]
Output:
0
Explanation:
There is only one matrix, so no multiplication is required.
Constraints:
$(2 \leq \text{arr.size()} \leq 100)$
$(1 \leq \text{arr}[i] \leq 200)$
π― My Approach:
Bottom-Up Dynamic Programming
Key Idea:
We define dp[i][j]
as the minimum number of scalar multiplications required to multiply matrices from index i
to j
.
Algorithm Steps:
Create a DP table
dp[i][j]
, initialized to 0.Iterate over chain lengths (
len = 2
ton-1
).Iterate over starting indices (
i = 1
ton-len
), settingj = i + len - 1
.Compute the minimum cost for multiplying matrices from
i
toj
by iterating over possible partition pointsk
.Return
dp[1][n-1]
, which contains the minimum multiplication cost.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(NΒ³), since we iterate over
O(NΒ²)
subproblems, and each subproblem requiresO(N)
operations.Expected Auxiliary Space Complexity: O(NΒ²), for storing the DP table.
π Solution Code
Code (C++)
class Solution {
public:
int matrixMultiplication(vector<int> &arr) {
int n = arr.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int len = 2; len < n; len++) {
for (int i = 1, j = len; j < n; i++, j++) {
dp[i][j] = INT_MAX;
for (int k = i; k < j; k++)
dp[i][j] = min(dp[i][j], arr[i - 1] * arr[k] * arr[j] + dp[i][k] + dp[k + 1][j]);
}
}
return dp[1][n - 1];
}
};
Code (Java)
class Solution {
static int matrixMultiplication(int[] arr) {
int n = arr.length;
int[][] dp = new int[n][n];
for (int len = 2; len < n; len++)
for (int i = 1, j = len; j < n; i++, j++) {
dp[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++)
dp[i][j] = Math.min(dp[i][j], arr[i - 1] * arr[k] * arr[j] + dp[i][k] + dp[k + 1][j]);
}
return dp[1][n - 1];
}
}
Code (Python)
class Solution:
def matrixMultiplication(self, arr):
n, dp = len(arr), [[0] * len(arr) for _ in range(len(arr))]
for l in range(2, n):
for i in range(1, n - l + 1):
j, dp[i][i + l - 1] = i + l - 1, float('inf')
for k in range(i, j):
dp[i][j] = min(dp[i][j], arr[i - 1] * arr[k] * arr[j] + dp[i][k] + dp[k + 1][j])
return dp[1][n - 1]
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated