3.7.6 · D1 · HinglishAlgorithm Paradigms

FoundationsDynamic programming — overlapping subproblems, optimal substructure

2,549 words12 min read↑ Read in English

3.7.6 · D1 · Coding › Algorithm Paradigms › Dynamic programming — overlapping subproblems, optimal subst

Is page par assume kiya gaya hai ki tumne kuch nahi dekha. Parent note Dynamic Programming padhne se pehle, tumhe neeche diya hua chota sa alphabet fluently aana chahiye. Hum har symbol ko pichle symbol ke upar build karte hain.


0. "Subproblem" kya hota hai? (sab kuch ka atom)

Yahan har symbol ek word ke around ghoomta hai: subproblem.

Ise ek nesting doll ki tarah socho: badi doll (poora problem) ke andar choti identical dolls (subproblems) hoti hain. Jab tak tum problems ko is nazar se nahi dekhte, DP samajh nahi aayega.

Figure — Dynamic programming — overlapping subproblems, optimal substructure

Topic ko yeh kyun chahiye: neeche diye gaye baaki saare symbols subproblems ko naam dene, count karne, ya store karne ke tarike hain. Subproblem nahi, toh DP nahi.


1. Function-call symbol

Ek vending machine socho: button dabaao, ek answer nikalta hai.

  • Letter sirf ek label hai — hum ise , , ya "the-answer-getter" bhi keh sakte hain.
  • Brackets ke andar ki cheez, , input hai — problem ki size ya state.

2. Recursion: ek machine jo khud ko call karti hai

Yahan "" kyun? Kyunki Fibonacci mein poora literally do chote answers ka sum hota hai. Alag problems alag tarah combine karti hain (baad mein dekhenge) — combining rule hi har DP ka dil hai.

Recursion ko ek tree jो neeche ki taraf badhta hai socho: upar ek call, choti calls mein split hoti hai, jo aur split hoti hain, jab tak har branch ek base case (leaf) tak nahi pahunch jaati.

Figure — Dynamic programming — overlapping subproblems, optimal substructure

3. Recursion tree ko padhna — jahan "overlap" dikhta hai

Phir se tree figure (s02) dekho. Har node jo label se marked hai use dhundho. ke tree mein, do baar aata hai, teen baar aata hai. Wahi repeats hain jiske liye DP exist karta hai.

Yeh gap — bahut saare nodes, thode distinct — hi woh exploitable waste hai. Detailed tree-counting Recursion Trees mein hai.

Topic ko yeh kyun chahiye: DP ka poora payoff hai "thode distinct wale solve karo, saare repeats skip karo." Jab tak repeats nahi dikhte, payoff nahi dikheга.


4. Big-O: woh symbol jo waste measure karta hai

Do runners socho:

  • ek gentle ramp par chadhta hai.
  • Exponential growth ek near-vertical cliff par shoot karta hai.
Figure — Dynamic programming — overlapping subproblems, optimal substructure

5. "Sticky note": ek table / memo

Do symbols comfortably padhne ke liye:

  • — sticky notes ki ek 1-D row (Fibonacci yahi use karta hai).
  • — ek 2-D grid, har input ke liye ek axis. Knapsack = item index aur = capacity use karta hai.

ko ek spreadsheet socho: rows items hain, columns capacities hain, har cell ek stored answer hai.

Figure — Dynamic programming — overlapping subproblems, optimal substructure

Topic ko yeh kyun chahiye: table woh memory hai jo plain recursion ko DP mein upgrade karti hai. Iske bina, §3 ke repeats recompute hote hain; iske saath, har distinct doll ek baar solve hoti hai.


6. symbol aur "optimal"

Recurrence se pehle, iske symbols se milte hain. Knapsack Problem mein hmare paas items ki ek list hai; item number ke paas hai:

  • = item ki value (yeh kitna worth hai),
  • = item ka weight (yeh kitni jagah leta hai),
  • = backpack mein abhi remaining capacity.

Greedy Algorithms se contrast karo (jo locally best option ek baar le leta hai aur kabhi reconsider nahi karta) aur Divide and Conquer se (jiske subproblems sab distinct hain, toh sticky notes help nahi karte). Doosre classic optimal-substructure problems: Longest Common Subsequence, Bellman-Ford.


Prerequisite map

Subproblem = smaller copy

Function notation F of n

Domain n in naturals with zero

Recurrence F calls smaller F

Base case stops recursion

Recursion tree picture

Overlapping vs distinct subproblems

Big-O measures the waste

Table stores each answer once

Memoization top down

Tabulation bottom up

max and optimal substructure

Dynamic Programming

Har arrow ka matlab hai "right box samajhne se pehle left box chahiye." Topic sabse neeche hai kyunki yeh sab foundations ko fuse karta hai.


Equipment checklist

Right side cover karo aur parent note kholne se pehle answer bolo.

ka ek sentence mein matlab kya hai?
Jab input ki size/value ho tab problem ka answer — ek machine jise tum dete ho aur ek answer milta hai.
Fibonacci ke ka domain kya hai?
Non-negative integers — na fractions, na negatives.
Subproblem kya hota hai?
Wahi problem ki ek choti copy (ek choti nesting doll).
Base case kya hai aur yeh kyun zaroori hai?
Sabse chota input jiska answer directly pata ho; iske bina recursion kabhi nahi rukti (aur yeh ko zero se neeche jaane se rokti hai).
Recursion tree mein "overlapping subproblems" kaisa dikhta hai?
Wahi node label tree mein ek se zyada jagah dikhna.
ke distinct subproblems, aur node count kaise badhta hai?
Sirf distinct, par total node count ki tarah badhta hai — exponential.
formally kya matlab hai?
Running time ek fixed multiple ke neeche rehti hai yardstick function ke, jab kaafi bada ho — ek ceiling hai.
kya hai?
2-D table ka ek cell jo aur se indexed subproblem ka answer store karta hai.
Memoization vs tabulation ek ek line mein?
Memoization = top-down recursion jo pehle table check karta hai (lazy); tabulation = bottom-up loop jo sabse chote pehle fill karta hai (eager).
kya karta hai, aur DP mein kab use hota hai?
aur mein se bada return karta hai; jab problem best option choose karti hai tab use hota hai (jaise knapsack mein take-or-skip).
Knapsack mein , , , aur kya hain?
= item ki value; = uska weight; = bacha hua capacity; = items use karke best value jab room ho.
Knapsack recurrence teen cases mein kyun split hota hai?
(koi item nahi), (item fit nahi hota — skip, negative index se bachata hai), aur (take/skip mein se better choose karo) handle karne ke liye.
Optimal substructure plain words mein batao.
Poore ka best answer uske parts ke best answers se build hota hai.
Combining operator kabhi aur kabhi kyun hota hai?
Yeh match karta hai jo problem poochti hai — Fibonacci do answers add karta hai, knapsack do options mein se best pick karta hai.
Master formula Time = (#subproblems)×(work each) kahan se aata hai?
Har table cell ki fill-cost sum karne se; agar har cell same cost le, toh sum ban jaata hai count × work-per-cell.

Connections

  • Recursion — woh machine jis par DP bana hai.
  • Recursion Trees — jahan overlap visible hota hai.
  • Time Complexity Analysis ka matlab.
  • Memoization vs Tabulation — table fill karne ke do styles.
  • Divide and Conquer — disjoint subproblems, toh caching help nahi karta.
  • Greedy Algorithms — ek baar commit karta hai, kabhi reconsider nahi karta.
  • Knapsack Problem, Longest Common Subsequence, Bellman-Ford — optimal-substructure classics.