0
0
DSA Typescriptprogramming~10 mins

Matrix Chain Multiplication in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Matrix Chain Multiplication
Start with matrices dimensions
Initialize DP table with zeros on diagonal
For chain length = 2 to n
For each start index i
For each split point k between i and j
Calculate cost = cost_left + cost_right + multiplication cost
Update DP[i
Repeat until full chain cost computed
Result in DP[0
The flow shows how we fill a table step-by-step to find the cheapest way to multiply a chain of matrices.
Execution Sample
DSA Typescript
const dims = [10, 20, 30, 40];
const n = dims.length - 1;
const dp = Array(n).fill(0).map(() => Array(n).fill(0));
for (let length = 2; length <= n; length++) {
  for (let i = 0; i <= n - length; i++) {
    let j = i + length - 1;
    dp[i][j] = Infinity;
    for (let k = i; k < j; k++) {
      const cost = dp[i][k] + dp[k+1][j] + dims[i]*dims[k+1]*dims[j+1];
      if (cost < dp[i][j]) dp[i][j] = cost;
    }
  }
}
This code calculates the minimum multiplication cost for matrices with given dimensions.
Execution Table
StepOperationIndices (i,j)Split kCost CalculatedDP Table StateMinimum Cost at DP[i][j]
1Initialize diagonal0,0--[[0, -, -, -], [-, 0, -, -], [-, -, 0, -], [-, -, -, 0]]0
2Initialize diagonal1,1--[[0, -, -, -], [-, 0, -, -], [-, -, 0, -], [-, -, -, 0]]0
3Initialize diagonal2,2--[[0, -, -, -], [-, 0, -, -], [-, -, 0, -], [-, -, -, 0]]0
4Calculate cost for length=20,1010*20*30=6000[[0, 6000, -, -], [-, 0, -, -], [-, -, 0, -], [-, -, -, 0]]6000
5Calculate cost for length=21,2120*30*40=24000[[0, 6000, -, -], [-, 0, 24000, -], [-, -, 0, -], [-, -, -, 0]]24000
6Calculate cost for length=30,20dp[0][0]+dp[1][2]+10*20*40=0+24000+8000=32000[[0, 6000, 32000, -], [-, 0, 24000, -], [-, -, 0, -], [-, -, -, 0]]32000
7Calculate cost for length=30,21dp[0][1]+dp[2][2]+10*30*40=6000+0+12000=18000[[0, 6000, 18000, -], [-, 0, 24000, -], [-, -, 0, -], [-, -, -, 0]]18000
8Calculate cost for length=40,30dp[0][0]+dp[1][3]+10*20*40=0+Infinity+8000=Infinity[[0, 6000, 18000, Infinity], [-, 0, 24000, -], [-, -, 0, -], [-, -, -, 0]]Infinity
9Calculate cost for length=40,31dp[0][1]+dp[2][3]+6000+12000=6000+Infinity+12000=Infinity[[0, 6000, 18000, Infinity], [-, 0, 24000, Infinity], [-, -, 0, -], [-, -, -, 0]]Infinity
10Calculate cost for length=40,32dp[0][2]+dp[3][3]+18000+48000=18000+0+48000=66000[[0, 6000, 18000, 66000], [-, 0, 24000, Infinity], [-, -, 0, -], [-, -, -, 0]]66000
11Calculate cost for length=41,31dp[1][1]+dp[2][3]+0+12000=0+Infinity+12000=Infinity[[0, 6000, 18000, 66000], [-, 0, 24000, -], [-, -, 0, -], [-, -, -, 0]]Infinity
12Calculate cost for length=41,32dp[1][2]+dp[3][3]+24000+48000=24000+0+48000=72000[[0, 6000, 18000, 66000], [-, 0, 24000, 72000], [-, -, 0, -], [-, -, -, 0]]72000
13Calculate cost for length=42,32dp[2][2]+dp[3][3]+30*40*?=0+0+?[[0, 6000, 18000, 66000], [-, 0, 24000, 72000], [-, -, 0, 1200], [-, -, -, 0]]1200
14Final result0,3--[[0, 6000, 18000, 66000], [-, 0, 24000, 72000], [-, -, 0, 1200], [-, -, -, 0]]66000
💡 All subproblems computed; minimum multiplication cost is dp[0][n-1] = 66000
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 7After Step 10After Step 12Final
dp[0][0]0000000
dp[1][1]0000000
dp[2][2]0000000
dp[3][3]0000000
dp[0][1]Infinity600060006000600060006000
dp[1][2]InfinityInfinity2400024000240002400024000
dp[0][2]InfinityInfinityInfinity18000180001800018000
dp[0][3]InfinityInfinityInfinityInfinity660006600066000
dp[1][3]InfinityInfinityInfinityInfinityInfinity7200072000
dp[2][3]InfinityInfinityInfinityInfinityInfinityInfinity1200
Key Moments - 3 Insights
Why do we initialize the diagonal of the DP table with zeros?
Because multiplying one matrix alone costs nothing, so dp[i][i] = 0 as shown in steps 1-3 of the execution_table.
Why do we try all split points k between i and j?
Because the chain can be split in different ways, and we want the minimum cost. The execution_table rows 6-7 show different k values and costs compared.
Why does the cost formula include dims[i]*dims[k+1]*dims[j+1]?
This represents the cost of multiplying two matrices after splitting at k. It matches the multiplication dimension rule and is used in cost calculations in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7. What is the minimum cost stored at dp[0][2] after considering split k=1?
A32000
B18000
C6000
D24000
💡 Hint
Check the 'Minimum Cost at DP[i][j]' column in row 7 of the execution_table.
At which step does the DP table first get a value for dp[0][1]?
AStep 4
BStep 6
CStep 2
DStep 10
💡 Hint
Look for when dp[0][1] changes from '-' or Infinity to a number in the execution_table.
If the dimension array dims changed to [10, 20, 10, 40], how would the cost at step 7 change?
AIt would increase
BIt would stay the same
CIt would decrease
DIt would become zero
💡 Hint
Look at the cost formula in step 7 and consider how dims[k+1] affects multiplication cost.
Concept Snapshot
Matrix Chain Multiplication:
- Goal: Minimize scalar multiplications for matrix chain
- Use DP table dp[i][j] for min cost multiplying matrices i to j
- dp[i][i] = 0 (single matrix)
- For length 2 to n, compute dp[i][j] by trying all splits k
- Cost = dp[i][k] + dp[k+1][j] + dims[i]*dims[k+1]*dims[j+1]
- Result in dp[0][n-1]
Full Transcript
Matrix Chain Multiplication finds the cheapest way to multiply a sequence of matrices. We use a table dp where dp[i][j] stores the minimum cost to multiply matrices from i to j. The diagonal dp[i][i] is zero because one matrix alone needs no multiplication. We fill the table by increasing chain length, trying all split points k between i and j. For each split, we calculate cost as sum of left and right parts plus multiplication cost of resulting matrices. We keep the minimum cost in dp[i][j]. After filling the table, dp[0][n-1] gives the minimum multiplication cost for the full chain. The execution table shows step-by-step how costs are computed and updated. Key points include initializing the diagonal, trying all splits, and using the dimension array to calculate multiplication cost.