3.7.13 · D2 · HinglishAlgorithm Paradigms

Visual walkthroughDP problems — matrix chain multiplication

1,707 words8 min read↑ Read in English

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

Hum poore walkthrough mein ek hi chain use karenge: toh dimension array (wo list of numbers jo saari shapes describe karta hai) hai


Step 1 — Matrix ki "shape" kya hoti hai, aur ek multiplication kitni cost karti hai

KYA. Matrix sirf numbers ka ek grid hota hai. Uski shape rows × columns likhi jaati hai. Matrix hai : 35 rows, 15 columns. Dimension array mein, ki shape hai — wo rows ke liye left wala number () lete hai aur columns ke liye right wala number ().

WHY sirf shapes. Effort count karne ke liye hume andar ke actual numbers ki zarurat nahi — effort sirf is baat par depend karta hai ki grids kitne bade hain. Toh hum contents hata dete hain aur sirf shape list rakhte hain.

PICTURE. Ek grid ko grid se multiply karne ke liye, do inner numbers match karne chahiye (dono ). Tum ek result produce karte ho, aur uske cells mein se har cell products ka sum hota hai.

Figure — DP problems — matrix chain multiplication

Step 2 — Do orders, bilkul alag bills

KYA. Sirf pehli teen matrices lo aur unhe do-do karke jodo. Do possible orders exist karte hain. Final grid identical hoti hai (matrix multiplication associative hoti hai), lekin scalar multiplications ka running total nahi hota.

WHY ye matter karta hai. Agar dono orders ki cost same hoti toh koi problem hi nahi hoti solve karne ki. Hoti nahi hai — kabhi kabhi 10 ya usse zyada ka factor — toh order choose karna real kaam hai.

PICTURE. Dekho ki har order kaunsa intermediate grid build karta hai. Mota intermediate grid hi ek order ko expensive banata hai.

Figure — DP problems — matrix chain multiplication

use karte hain (zyada sharp illustration ke liye):


Step 3 — Ek hi idea: LAST glue dekho

KYA. Bhool jao ki poora product kaise build hota hai. Sirf poocho: kaunsi multiplication sabse last hoti hai? Chahe kuch bhi parenthesization ho, exactly ek outermost multiply hoti hai, aur wo ek left block ko ek right block se jodti hai.

WHY ye question. Ye ek mushkil problem ko do chhote, independent problems mein tod deta hai. Agar last glue matrix ke baad hai, toh:

  • left side ki sab cheez, , ek grid mein collapse ho chuki hai,
  • right side ki sab cheez, , ek grid mein collapse ho chuki hai,
  • aur unki internal costs ek doosre ko affect nahi karti. Yahi optimal substructure hai.

PICTURE. Split point chain mein ek single cut hai. Dono halves apne aap mein chhote MCM problems hain.

Figure — DP problems — matrix chain multiplication

Step 4 — Last glue ki cost kya hoti hai

KYA. Dono halves collapse hone ke baad, hum unki shapes se exactly jaante hain:

  • Left block ek grid produce karta hai.
  • Right block ek grid produce karta hai.

WHY ye shapes. Left block se start hota hai (rows ) aur par khatam hota hai (columns ). Right block se start hota hai (rows ) aur par khatam hota hai (columns ). Do inner numbers dono hain — ye automatically match karte hain, isliye consecutive blocks ko hamesha glue kiya ja sakta hai.

PICTURE. Teen boundary numbers bachte hain: , , . Step 1 ke rule se final glue ki cost unka product hai.

Figure — DP problems — matrix chain multiplication

Step 5 — Recurrence assemble karna

KYA. Jo cheez chahiye usse naam do: ko minimum number of scalar multiplications maano jo ko fully collapse karne ke liye chahiye. Chosen split ke liye total cost hai: left half ki cost + right half ki cost + final glue.

WHY par min. Hume advance mein best cut nahi pata (Step 2 ne prove kiya ki greedy guessing fail hoti hai). Toh hum har legal cut try karte hain aur sabse sasta rakhte hain — yahi ek honest choice hai.

PICTURE. Har candidate ek branch hai. Hum sab evaluate karte hain aur sabse chhota bar chunte hain.

Figure — DP problems — matrix chain multiplication

Step 6 — Degenerate aur edge cases (koi gap mat chhodo)

KYA. Har wo corner jo reader encounter kar sakta hai:

  1. Ek matrix (): min ke liye koi valid nahi hai (range empty hai). Hume empty set par min nahi call karna chahiye → yahi reason hai ki base case ek alag branch ki tarah exist karta hai.
  2. Do matrices (): exactly ek legal . Koi real choice nahi, cost sirf hai — Step 1 ke single-multiply cost se match karta hai, jaise hona chahiye.
  3. Ek dimension equal to 1 (jaise row ya column vector): formula phir bhi kaam karta hai; product mein sirf us glue ko cheap bana deta hai. Kuch special-case nahi.
  4. Empty chain (, array length 1): koi matrices nahi, cost — loops se pehle guard karo.

WHY ye dikhate hain. Ek recurrence jo smallest input par crash kare wo complete nahi hai. Base case decoration nahi hai; ye woh floor hai jis par poori recursion khadi hai.

PICTURE. Recursion tree tab tak bottom out hota hai jab tak har leaf ek single matrix (cost 0) na ho.

Figure — DP problems — matrix chain multiplication

Step 7 — Chain LENGTH se fill kyun karo, rows se nahi

KYA. compute karne ke liye hume aur chahiye — dono chhoti chains hain. Toh kisi bhi lambi chain se pehle hume saari chhoti chains milni chahiye.

WHY row-by-row nahi. Table ko left-to-right, top-to-bottom padhne par (length 4) maangega jabki (length 3) abhi blank hoga. Badh ke length se fill karo: (zeros), phir , tak.

PICTURE. DP table main diagonal se bahar sweep karte diagonals ki tarah fill hoti hai.

Figure — DP problems — matrix chain multiplication

Apni chain par kaam karte hain:

Recall 9375 kahan se aaya?

Poori chain ka best split hai ::: , aur andar deta hai , toh answer hai cost 9375 par.


Ek-picture summary

Upar ki saari cheez ek single frame mein compress ki: chain, par ek last cut, do collapsed halves, teen surviving boundary numbers glue cost ko feed karte hain, aur saare cuts par min DP table ko length se fill karta hai.

Figure — DP problems — matrix chain multiplication
Recall Feynman retelling — poora walkthrough simple words mein

Tumhare paas paper sheets ki ek row hai jise stack-glue karna hai, do at a time. Ek sheet ko sheet se glue karne ki cost effort hai (Step 1). Final sheet gluing order chahe kuch bhi ho same dikhti hai, lekin effort 10× differ kar sakta hai kyunki mote middle sheets expensive hote hain (Step 2). Trick: sabse last glue ke baare mein socho — wo hamesha row ko ek left piece aur ek right piece mein split karta hai jo independently build hue the (Step 3). Ek baar split hone par, dono pieces ki shapes boundary numbers se fix ho jaati hain, toh last glue ki cost hoti hai (Step 4). Hume best jagah cut karna nahi pata, toh hum har cut try karte hain aur sabse sasta rakhte hain — yahi par min hai (Step 5). Ek sheet ki cost kuch nahi, jo puri idea ka floor hai (Step 6). Aur kyunki lambi piece chhoti pieces se banti hai, hum pehle chhoti pieces solve karte hain, table ko length se sweep karte hain (Step 7). Yahi poora algorithm hai — ek honest "try all last cuts" cleverly repeat kiya.


Connections

  • Dynamic Programming — Step 5 ke peeche optimal-substructure engine
  • Catalan Numbers — kitne parenthesizations exist karte hain (), yani brute force kyun haarta hai
  • Optimal Binary Search Tree — same "try every split" interval-DP shape
  • Burst Balloons — interval DP jahan tum last balloon fix karte ho
  • Memoization vs Tabulation — top-down vs yahan ka length-sweep
  • Time Complexity Analysis loops over se