DP problems — edit distance (Levenshtein)
3.7.12· Coding › Algorithm Paradigms
WHAT is it?
Hum ek 2D table define karte hain:
Toh humara final answer hai, aur (do empty strings ki cost kuch nahi).
WHY does a recurrence even exist?
Aakhri move in mein se ek hoga:
- Characters match karte hain (
A[i-1] == B[j-1]): is position ke liye koi edit nahi chahiye → cost se pass through hoti hai. - Substitute
A[i-1]→B[j-1]: cost chhote prefixes ko align karne ki cost. - Delete
A[i-1](yehAmein extra char hai): cost . - Insert
B[j-1](yehBmein chahiye): cost .
Hum minimum lete hain kyunki hum sabse sasta raasta chahte hain.
HOW to derive the recurrence from scratch
Yeh kyun sahi hai: har conversion exactly upar diye chaar cases mein se ek ke saath khatam hoti hai, har ek strictly chhote subproblem par reduce hoti hai, aur hum minimize karte hain — toh hum kabhi optimum miss nahi karte (yeh optimal substructure + overlapping subproblems hai).

Complexity (80/20 takeaway)
- Time: — hum har cell ko ek baar mein fill karte hain.
- Space: full table ke liye, ya agar aap sirf previous row rakhte ho (aap sirf row aur column hi padhte ho).
Worked Example 1 — "horse" → "ros" (answer = 3)
Hum table ko row by row fill karte hain. Maano horse (rows), ros (cols).
| "" | r | o | s | ||
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | ||
| h | 1 | 1 | 1 | 2 | 3 |
| o | 2 | 2 | 2 | 1 | 2 |
| r | 3 | 3 | 2 | 2 | 2 |
| s | 4 | 4 | 3 | 3 | 2 |
| e | 5 | 5 | 4 | 4 | 3 |
- Kyun ?
A[1]='o',B[1]='o'match karte hain, toh hum diagonal copy karte hain. Yeh step kyun? Match karne ki cost kuch nahi, toh cost chhote prefixes"h"→"r"fix karne ke barabar hai. - Kyun ?
'e' ≠ 's', toh . Yeh step kyun? Sabse sasta predecessor delete path hai (edelete karo), jo final 3 deta hai. - Edits wapas padhna:
h→rsubstitute karo? Nahi — actual optimal path hai delete h, delete r, substitute e→s (3 edits), jo se match karta hai.
Worked Example 2 — "sunday" → "saturday" (answer = 3)
Key cells:
- Shared
s...n...daystructure ka matlab hai ki bahut saare matches seedha diagonal ke neeche jaate hain. - Hume
aaurtinsert karne padte hain, aurn→rsubstitute karna padta hai. Yeh hai.
Yeh 3 kyun hai aur zyada kyun nahi? Kyunki lambi common subsequence
s..dayfree mein align ho jaati hai; sirf 3 positions par actually kaam karna padta hai. Yeh step kyun important hai: edit distance secretly lambi shared subsequences ko reward karta hai.
Worked Example 3 — Forecast-then-Verify
Common Mistakes
Recall Feynman: 12-saal ke bachche ko explain karo
Socho tumhare paas do words Lego strips par likhe hain aur tum pehli word ko bilkul doosri jaisi dikhana chahte ho. Tumhe teen moves ki permission hai: ek nayi letter brick chipkaao, ek letter brick khicho, ya ek brick ko doosre se badlo — har move mein ek coin lagti hai. Edit distance woh sabse kam coins hai jo tum kharch kar sakte ho. Clever trick: poori word ek saath solve karne ki jagah, tum ek chhota grid banate ho aur pehle chhoti shuruat ke liye sabse sasta fix pata karte ho (sirf ek letter, phir do...), har answer ek box mein likhte ho. Har naya box sirf teen boxes pe nazar daalta hai jo tumne pehle fill kiye hain (upar, baayi taraf, aur upar-baayi taraf diagonal) aur sabse sasta choose karta hai. Jab tak tum bottom-right box tak pahuncho, answer wahan already ready milta hai!
Active Recall
Edit distance mein kya represent karta hai?
Teen allowed operations aur unki costs kya hain?
kya hai aur kyun?
B[0..j) banane ke liye aapko characters insert karne padte hain.kya hai aur kyun?
A[0..i) ko empty karne ke liye aapko characters delete karne padte hain.Jab A[i-1] == B[j-1] ho, toh kya hai?
Jab characters alag hon, transition likhो.
A se deletion kaunsa neighbour represent karta hai?
A mein insertion kaunsa neighbour represent karta hai?
Time aur space complexity?
Recurrence sirf aakhri character kyun dekhta hai?
"horse" → "ros" ki edit distance?
Kya edit distance length difference ke barabar hoti hai?
Connections
- Dynamic Programming — edit distance ek canonical 2D-table DP hai.
- Longest Common Subsequence — closely related; LCS matches align karta hai, edit distance mismatches count karta hai.
- Optimal Substructure — woh property jo recurrence ko valid banati hai.
- Overlapping Subproblems — isliye memoisation/tabulation naive recursion se better hai.
- Recursion and Memoization — bottom-up table ka top-down alternative.
- Sequence Alignment — bioinformatics generalisation (Needleman–Wunsch) weighted costs ke saath.
- Space Optimization in DP — rolling-row trick jo space tak pahunchata hai.