3.7.13 · HinglishAlgorithm Paradigms

DP problems — matrix chain multiplication

1,436 words7 min readRead in English

3.7.13 · Coding › Algorithm Paradigms


YEH problem HAI KYA?

Dimension array KYO? Kyunki matrices ke contents se koi fark nahi padta — sirf cost matter karti hai, aur cost sirf dimensions par depend karti hai.


PARENTHESIZATION matter KYO karti hai? (worked motivation)

Lo , yani .

  • :
  • :

Recurrence KAISE derive karein (scratch se)

Key insight: ki kisi bhi full parenthesization mein ek aakhri multiplication hoti hai. Woh aakhri multiply chain ko kisi point par split karti hai:

  • Left block ek matrix produce karta hai.
  • Right block ek matrix produce karta hai.
  • Unhe combine karne ki cost hai.

Yeh decomposition KYO kaam karta hai: har parenthesization mein exactly ek outermost split hota hai. Agar hum best split jaante, toh subproblems "left ka best cost" aur "right ka best cost" independent hain — yahi optimal substructure hai.

Maano = multiply karne ki minimum cost.


Figure — DP problems — matrix chain multiplication

Bottom-up algorithm

MCM(p[0..n]):
    n = len(p) - 1                 # number of matrices
    m = 2D array, m[i][i] = 0
    s = 2D array                   # to reconstruct the splits
    for L in 2..n:                 # chain length
        for i in 1..n-L+1:
            j = i + L - 1
            m[i][j] = +infinity
            for k in i..j-1:
                cost = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
                if cost < m[i][j]:
                    m[i][j] = cost
                    s[i][j] = k
    return m[1][n], s

Pura worked example

Matrices ke saath , toh .

Length 2 chains (pairs ki cost):

  • Kyun? sirf ek split hai.

Length 3 chains:

  • Yeh step kyun? Hum ke baad split karne ko ke baad split karne se compare kar rahe hain; sasta wala par split hai.

Length 4 chain (answer):



Recall Feynman: 12-saal ke bachche ko samjhao

Socho tumhe LEGO sheets ki ek row ko do-do karke jodhna hai. Do sheets jodhne mein mehnat lagti hai jo unke size par depend karti hai. Final badi sheet same rahegi chahe kisi bhi order mein jodo, lekin kuch orders mein kaam bahut kam lagta hai. MCM bas ek smart plan hai: har woh jagah try karo jahan aakhri glue laga sakte ho, har piece banane ka sabse sasta tarika yaad rakho, aur koi bhi piece dobara solve mat karo.


Flashcards

MCM convention mein matrix ki dimension kya hoti hai?
, dimension array use karke.
Ek ko matrix se multiply karne ki cost kya hai?
scalar multiplications.
MCM recurrence batao.
, with .
Saare split points kyun try karte hain?
Hum nahi jaante optimal last multiplication kahan hai; greedy fail hoti hai, isliye sab try karo aur min lo (optimal substructure + overlapping subproblems).
DP table kis order mein fill karte hain?
Increasing chain length ke hisaab se (length 2 se tak), row by row nahi.
MCM DP ki time aur space complexity?
Time (L, i, k ke loops), space .
ki cost vs mein kya hai?
7500 vs 75000 — same result, 10 guna fark.
Actual parenthesization kaise recover karte hain?
Split point store karo, phir aur par recurse karo.
Parenthesization result matrix kyun nahi badalta?
Matrix multiplication associative hoti hai.
Base case kya hai aur kyun?
kyunki ek akeli matrix ko multiply karne ki zarurat nahi hoti.

Connections

  • Dynamic Programming — optimal substructure + overlapping subproblems
  • Catalan Numbers — parenthesizations count karta hai
  • Optimal Binary Search Tree — same "har root/split try karo" interval-DP pattern
  • Burst Balloons — interval DP jahan last element fix karte hain
  • Memoization vs Tabulation
  • Time Complexity Analysis teen nested loops se

Concept Map

hai

same result

goal

Ai is pi-1 x pi

needs

split at k

combine cost

leads to

base case

min over k

solve via

avoids

Matrix chain product

Associative

Cost varies by parenthesization

Minimize scalar multiplications

Dimension array p

Cost of one multiply a*b*c

Optimal substructure

Left block and right block

MCM recurrence m i j

m i i equals 0

Overlapping subproblems

Bottom-up DP by chain length L

Catalan exponential blowup