3.7.13 · D4 · HinglishAlgorithm Paradigms

ExercisesDP problems — matrix chain multiplication

2,115 words10 min read↑ Read in English

3.7.13 · D4 · Coding › Algorithm Paradigms › DP problems — matrix chain multiplication


Level 1 — Recognition

L1.1 — Array se dimensions padhna

Diya gaya hai , har matrix ko uski dimensions ke saath list karo.

Recall Solution

Rule hai " mein aur use hote hain". Array ko overlapping pairs mein walk karo:

Ye kyon kaam karta hai: consecutive matrices ek boundary dimension share karti hain (, , ), aur exactly isi wajah se unhe multiply kiya ja sakta hai — inner dimensions automatically match ho jaate hain. Matrices ki sankhya ki length minus one .

L1.2 — Ek single multiplication ki cost

Tum ek matrix ko ek matrix se multiply karte ho. Kitne scalar multiplications honge?

Recall Solution

Cost . Kyon: result entries ka hoga, aur har entry length ka dot product hai (wo shared middle dimension), toh .


Level 2 — Application

L2.1 — Length-2 chain (sirf ek tarika)

. compute karo.

Recall Solution

Sirf do matrices hain, toh sirf ek split hai (): Min kyon nahi chahiye: do matrices ke saath parentheses rakhne ki exactly ek hi jagah hoti hai.

L2.2 — Length-3 chain, dono splits compare karo

(motivation example). Dono split points try karke compute karo, aur optimal parenthesization batao.

Recall Solution

Chain , indices . Pehle sub-costs:

Ab do outer splits:

  • :
  • :

at → parenthesization .

kyon jeetta hai: ke baad split karne se final combine bahut chhoti ho jaati hai, kyunki badi dimension pehle hi saste step ke andar "kha" li gayi hai.


Level 3 — Analysis

L3.1 — ke liye poori table

(optimal cost) aur optimal parenthesization compute karo. Increasing chain length se fill karo.

Recall Solution

Matrices: , .

Length 2:

Length 3:

  • :
    • :
    • :
    • min ,
  • :
    • :
    • :
    • min ,

Length 4:

  • :
  • :
  • :

at , .

Reconstruct: . Phir . Final: .

Neeche table-filling order dekho — chains diagonal ke saath badhti hain, kabhi row by row nahi.

Figure — DP problems — matrix chain multiplication

L3.2 — kin cells par depend karta hai?

Recompute kiye bina, exactly batao ki compute hone se pehle kaun se table entries honi chahiye, aur diagonal fill order explain karo.

Recall Solution

ke liye splits ko ye pairs chahiye:

  • : aur
  • : aur
  • : aur

Toh ise chahiye — ye sab chhote chains hain (length ). Isi wajah se hum increasing length se fill karte hain: length ki har cell sirf length wali cells mein jaati hai, jo pehle se bhar chuki hoti hain. Neeche figure mein arrows dekho.

Figure — DP problems — matrix chain multiplication

Level 4 — Synthesis

L4.1 — Parenthesizations count karo (Catalan connection)

matrices ke product ke kitne distinct full parenthesizations hote hain? Formula aur number dono batao.

Recall Solution

matrices ke parenthesizations ki sankhya Catalan number hai: ke liye humein chahiye: Catalan kyon: outermost split point choose karna recursively ek left group aur ek right group banata hai, aur — exactly Catalan recurrence. Dekho Catalan Numbers. Ye exponential growth () exactly isi wajah se brute force hopeless hai aur DP () jeetta hai.

L4.2 — Memoized top-down version, aur kyon complexity same hai

Top-down memoized MCM ka pseudocode likho aur argue karo ki ye abhi bhi time, space hai.

Recall Solution
memo = 2D array filled with -1
def solve(i, j):
    if i == j: return 0
    if memo[i][j] != -1: return memo[i][j]
    best = +infinity
    for k in i..j-1:
        cost = solve(i,k) + solve(k+1,j) + p[i-1]*p[k]*p[j]
        best = min(best, cost)
    memo[i][j] = best
    return best
# answer = solve(1, n)

Same complexity kyon: distinct states hain (memo ki wajah se har ek sirf ek baar solve hota hai), aur har ek par loop karta hai → time. Space memo table hai (plus recursion stack). Ye Memoization vs Tabulation equivalence hai: same states, same transitions, alag visiting order. Dekho Time Complexity Analysis.


Level 5 — Mastery

L5.1 — Same paradigm, alag combine: Burst Balloons

Burst Balloons mein tumhare paas values wale balloons hain, jo tak pad kiye gaye hain. Balloon ko burst karna (last, interval ke andar) earn karta hai (adjacent survivors). Recurrence hai: Ek paragraph mein explain karo ki ye MCM se structurally identical kaise hai, aur wo ek conceptual switch batao jo ise kaam karwata hai.

Recall Solution

Dono interval DP hain: ek sub-range ko ek special element fix karke aur do pieces par recurse karke solve karo. MCM mein tum last multiplication fix karte ho ( par split), aur dono sides independent hain kyunki ek completed block sirf apni outer dimensions expose karta hai. Burst Balloons mein tum last balloon burst fix karte ho (pehla nahi!) — kyunki jab ke andar last pop hota hai, uske neighbours exactly survivors aur hote hain, jisse do sub-intervals independent ho jaate hain. Ek conceptual switch: LAST event fix karo, pehla nahi, taaki subproblems decouple ho jaayein. Combine term (MCM) (balloons) ban jaata hai — same "teen boundary values multiplied" shape. Ye same "har split/root try karo" pattern Optimal Binary Search Tree ko bhi drive karta hai.

L5.2 — Ek variant design karo: maximum single multiplication minimize karo

Scalar multiplications ka sum minimize karne ki jagah, maan lo tum poore plan mein use ki gayi sabse badi single combine cost minimize karna chahte ho (ek "peak memory/latency" objective). Modified recurrence likho aur ke liye answer compute karo.

Recall Solution

"Sum then min" ko "har split ke liye teen pieces ka max lo, phir par min karo" se replace karo: Max andar kyon: peak sabse buri single operation hai is raaste mein, toh pieces combine karne par left ka peak, right ka peak, ye combine mein se sabse bada lena padta hai. Min bahar kyon: hum abhi bhi split choose kar sakte hain, toh hum woh chunte hain jiska peak sabse chhota ho.

ke liye compute karo ():

  • :
    • :
    • :
    • min at .

Toh minimum peak hai ke saath. Insight: recurrence ka skeleton (interval DP, par split) untouched hai — sirf aggregation operators bade hain (sum→max). Ye pehchanna ki "paradigm split hai, objective swappable hai" — yahi L5 mastery point hai.


Recall Quick self-test recap

Increasing chain length se fill karo? ::: Haan — , kabhi row by row nahi. Split par ke liye combine cost? ::: . matrices ke parenthesizations ki sankhya? ::: Catalan . MCM ko "peak cost minimize karo" mein kaise switch karein? ::: Inner sum ko max se replace karo, outer min rakho. Interval-DP golden rule? ::: LAST event fix karo taaki subproblems decouple hon.

Connections

  • Dynamic Programming — optimal substructure + overlapping subproblems
  • Catalan Numbers parenthesizations count karta hai (L4.1)
  • Burst Balloons — same interval DP, last burst fix karo (L5.1)
  • Optimal Binary Search Tree — "har root try karo" interval DP
  • Memoization vs Tabulation — L4.2 top-down equivalence
  • Time Complexity Analysis — teen nested loops se