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 Table
What is the final cost table (m) after running the matrix chain multiplication algorithm on matrices with dimensions [10, 20, 30, 40]?
DSA Python
def matrix_chain_order(p): n = len(p) - 1 m = [[0]*n for _ in range(n)] for l in range(2, n+1): for i in range(n-l+1): j = i + l - 1 m[i][j] = float('inf') for k in range(i, j): q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1] if q < m[i][j]: m[i][j] = q return m p = [10, 20, 30, 40] m = matrix_chain_order(p) print(m)
Attempts:
2 left
💡 Hint
Trace the cost calculation for multiplying matrices in different orders.
✗ Incorrect
The minimum cost to multiply matrices A1(10x20), A2(20x30), and A3(30x40) is computed by checking all possible splits. The cost table m stores these minimum costs. The correct table shows 6000 for multiplying first two matrices and 18000 for the full chain.
🧠 Conceptual
intermediateUnderstanding the Role of the 'k' Variable
In the matrix chain multiplication algorithm, what does the variable 'k' represent in the nested loops?
Attempts:
2 left
💡 Hint
Think about how the algorithm tries different places to split the chain.
✗ Incorrect
The variable 'k' is used to try all possible places to split the matrix chain between i and j to find the minimum multiplication cost.
🔧 Debug
advancedIdentify the Error in Matrix Chain Multiplication Code
What error will this code produce when run, and why?
Code:
def matrix_chain_order(p):
n = len(p) - 1
m = [[0]*n for _ in range(n)]
for l in range(2, n+1):
for i in range(n-l+1):
j = i + l - 1
m[i][j] = float('inf')
for k in range(i, j):
q = m[i][k] + m[k+1][j] + p[i]*p[k]*p[j]
if q < m[i][j]:
m[i][j] = q
return m
p = [10, 20, 30, 40]
m = matrix_chain_order(p)
print(m)
Attempts:
2 left
💡 Hint
Check how matrix dimensions are accessed in the cost calculation.
✗ Incorrect
The code uses p[k] instead of p[k+1] when calculating multiplication cost, which causes incorrect dimension indexing and leads to an IndexError or wrong results.
🚀 Application
advancedMinimum Multiplication Cost for Given Dimensions
Given matrices with dimensions [5, 10, 3, 12, 5, 50, 6], what is the minimum number of scalar multiplications needed to multiply the entire chain?
DSA Python
def matrix_chain_order(p): n = len(p) - 1 m = [[0]*n for _ in range(n)] for l in range(2, n+1): for i in range(n-l+1): j = i + l - 1 m[i][j] = float('inf') for k in range(i, j): q = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1] if q < m[i][j]: m[i][j] = q return m[0][n-1] p = [5, 10, 3, 12, 5, 50, 6] result = matrix_chain_order(p) print(result)
Attempts:
2 left
💡 Hint
Use dynamic programming to find the minimum cost by checking all splits.
✗ Incorrect
The minimum cost calculated by the algorithm for these dimensions is 2010 scalar multiplications.
🧠 Conceptual
expertWhy is Matrix Chain Multiplication a Dynamic Programming Problem?
Which reason best explains why matrix chain multiplication is solved using dynamic programming?
Attempts:
2 left
💡 Hint
Think about how the problem breaks down into smaller problems that repeat.
✗ Incorrect
Matrix chain multiplication has overlapping subproblems (same subchains computed multiple times) and optimal substructure (optimal solution contains optimal solutions to subproblems), which are key for dynamic programming.
