Challenge - 5 Problems
Matrix Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Matrix Chain Multiplication Cost Calculation
What is the minimum number of scalar multiplications needed to multiply the chain of matrices with dimensions [10, 20, 30, 40] using the standard Matrix Chain Multiplication algorithm?
DSA C++
int matrixChainOrder(int p[], int n) { int m[n][n]; for (int i = 1; i < n; i++) m[i][i] = 0; for (int L = 2; L < n; L++) { for (int i = 1; i < n - L + 1; i++) { int j = i + L - 1; m[i][j] = INT_MAX; for (int k = i; k <= j - 1; k++) { int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]; if (q < m[i][j]) m[i][j] = q; } } } return m[1][n - 1]; } int main() { int arr[] = {10, 20, 30, 40}; int size = sizeof(arr) / sizeof(arr[0]); int result = matrixChainOrder(arr, size); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about the order of multiplication that minimizes the total scalar multiplications.
✗ Incorrect
The minimum cost is calculated by trying all possible splits. For matrices with dimensions 10x20, 20x30, and 30x40, the minimum cost is 18000 scalar multiplications.
🧠 Conceptual
intermediateUnderstanding Matrix Chain Multiplication Dimensions
Given matrices A1 (5x10), A2 (10x3), A3 (3x12), and A4 (12x5), how many matrices are involved in the chain multiplication and what is the length of the dimension array used in the algorithm?
Attempts:
2 left
💡 Hint
Remember the dimension array length is one more than the number of matrices.
✗ Incorrect
For n matrices, the dimension array length is n+1 because it stores the row count of the first matrix and the column count of each matrix.
❓ Predict Output
advancedOutput of Matrix Chain Multiplication Parenthesization
What is the optimal parenthesization output of the following code for matrices with dimensions [40, 20, 30, 10, 30]?
DSA C++
#include <iostream> #include <vector> #include <climits> void printOptimalParens(std::vector<std::vector<int>>& s, int i, int j) { if (i == j) { std::cout << "A" << i; return; } std::cout << "("; printOptimalParens(s, i, s[i][j]); printOptimalParens(s, s[i][j] + 1, j); std::cout << ")"; } int matrixChainOrder(int p[], int n) { int m[n][n]; std::vector<std::vector<int>> s(n, std::vector<int>(n, 0)); for (int i = 1; i < n; i++) m[i][i] = 0; for (int L = 2; L < n; L++) { for (int i = 1; i < n - L + 1; i++) { int j = i + L - 1; m[i][j] = INT_MAX; for (int k = i; k <= j - 1; k++) { int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]; if (q < m[i][j]) { m[i][j] = q; s[i][j] = k; } } } } printOptimalParens(s, 1, n - 1); return m[1][n - 1]; } int main() { int arr[] = {40, 20, 30, 10, 30}; int size = sizeof(arr) / sizeof(arr[0]); int cost = matrixChainOrder(arr, size); std::cout << std::endl << cost << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Look at the split points stored in s to find the optimal parenthesization.
✗ Incorrect
The optimal parenthesization for these dimensions is (A1(A2(A3A4))) which minimizes the scalar multiplications.
🔧 Debug
advancedIdentify the Error in Matrix Chain Multiplication Code
What error will the following code produce when run, and why?
DSA C++
int matrixChainOrder(int p[], int n) { int m[n][n]; for (int i = 1; i < n; i++) m[i][i] = 0; for (int L = 2; L < n; L++) { for (int i = 1; i < n - L + 1; i++) { int j = i + L - 1; m[i][j] = INT_MAX; for (int k = i; k <= j - 1; k++) { int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]; if (q < m[i][j]) m[i][j] = q; } } } return m[1][n - 1]; } int main() { int arr[] = {10, 20, 30}; int size = sizeof(arr) / sizeof(arr[0]); int result = matrixChainOrder(arr, size); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the loop boundaries for array indexing.
✗ Incorrect
The loop 'for (int i = 1; i <= n; i++)' accesses m[i][i] where i can be equal to n, which is out of bounds for array of size n x n indexed from 0 to n-1.
🚀 Application
expertNumber of Scalar Multiplications for Large Chain
Given the matrix dimensions array [5, 10, 3, 12, 5, 50, 6], what is the minimum number of scalar multiplications needed to multiply the entire chain?
DSA C++
int matrixChainOrder(int p[], int n) { int m[n][n]; for (int i = 1; i < n; i++) m[i][i] = 0; for (int L = 2; L < n; L++) { for (int i = 1; i < n - L + 1; i++) { int j = i + L - 1; m[i][j] = INT_MAX; for (int k = i; k <= j - 1; k++) { int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]; if (q < m[i][j]) m[i][j] = q; } } } return m[1][n - 1]; } int main() { int arr[] = {5, 10, 3, 12, 5, 50, 6}; int size = sizeof(arr) / sizeof(arr[0]); int result = matrixChainOrder(arr, size); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Use dynamic programming to find the minimum cost by checking all splits.
✗ Incorrect
The minimum number of scalar multiplications for the given dimensions is 1510, calculated by the matrix chain multiplication dynamic programming approach.
