3.1.5 · Coding › Complexity Analysis
Intuition Badi picture (WHY yeh exist karta hai)
Kuch operations kabhi kabhi expensive hote hain par zyaadatar cheap . Agar tum unhe unke worst single case se bound karo, toh tum sach se zyada bol rahe ho. Example: ek dynamic array ka push O ( 1 ) almost hamesha hota hai, par kabhi kabhi woh double hota hai aur sab kuch copy karta hai (O ( n ) ). Amortized analysis poochhta hai: poore operations ke sequence par, ek operation ki average cost kya hai — guaranteed, probabilistic nahi?
Key word: guaranteed . Amortized = average-case. Koi randomness nahi hai. Hum promise karte hain ki koi bhi m operations ka sequence zyada se zyada m ⋅ ( amortized cost ) cost karega, absolute worst case mein.
Definition Amortized cost
Maano m operations ka ek sequence hai jiska total actual cost T ( m ) hai, toh amortized cost per operation koi bhi value c ^ hai jaise ki
∑ i = 1 m c ^ i ≥ ∑ i = 1 m c i (actual cost)
Phir hum c ^ = m 1 ∑ c ^ i report karte hain. Amortized bound honest hai kyunki left side real total work ko upper-bound karti hai.
c ^ dhundhne ke teen methods hain. Sab same total bound dete hain; farq sirf accounting ke tarike mein hai.
Total cost T ( m ) ko seedha kisi bhi m operations ke sequence ke liye compute karo, phir divide karo: amortized cost = T ( m ) / m . Har operation ko same amortized cost milti hai. Sochne mein sabse simple, jab operations alag hon toh weak.
Worked example Dynamic array (table doubling),
m pushes
KAISE: Capacity 1 se start karo. Jab full ho jaye, double allocate karo aur copy karo.
Sasta part: har push 1 element likhta hai ⇒ total m writes.
Expensive part (copies): hum tab copy karte hain jab size 1 , 2 , 4 , 8 , … hit kare. Copy costs hain 1 + 2 + 4 + ⋯ + 2 k jahan 2 k ≤ m .
Yeh step kyon? Copy sizes ek geometric series banate hain. Crucial fact yeh hai:
1 + 2 + 4 + ⋯ + 2 k = 2 k + 1 − 1 < 2 m .
Total: T ( m ) ≤ m + 2 m = 3 m .
Amortized: c ^ = T ( m ) / m ≤ 3 === O ( 1 ) == per push.
Common mistake Steel-man: "doubling
n elements copy karta hai, toh push O ( n ) amortized hai"
Yeh sahi kyon lagta hai: woh single expensive push sach mein n elements copy karta hai — tum usse dekh sakte ho.
Fix: woh O ( n ) copy sirf har n pushes mein ek baar hota hai. Expensive events rare aur geometrically spaced hain, isliye copies total mein < 2 m sum karte hain, na ki m ⋅ n . Doublings ke beech ke gap par average cost constant hai.
Har operation ko ek amortized cost c ^ charge karo jo uski real cost se zyada ho sakti hai. Surplus ko data-structure elements par credit ke roop mein store karo. Baad mein, expensive operations user ko charge karne ki jagah stored credit kharcha karte hain. Rule jo ise valid banata hai:
total credit = ∑ ( c ^ i − c i ) ≥ 0 hamesha.
Agar bank kabhi negative nahi jata, toh ∑ c ^ ≥ ∑ c , isliye bound honest hai.
Worked example Dynamic array, accounting style
KAISE: c ^ = 3 per push charge karo. Non-resizing push ki real cost 1 hai.
$1 spend karo nayi element place karne ke liye.
$1 nayi element par store karo (baad mein khud ko move karne ke liye pay karne ke liye).
$1 ek purani element par store karo jiske paas abhi credit nahi hai.
Yeh step kyon? Jab array n se 2 n double hoti hai, exactly woh n elements present hain jo copy hone chahiye. Last n pushes ne har ek ne $2 bachaye, jo nayi half aur purani half dono cover karti hai. Toh har copy prepaid hai — credit kabhi negative nahi jata.
Amortized: c ^ = 3 = O ( 1 ) . ✓ aggregate se match karta hai.
Worked example Binary counter increment (k-bit)
increment ki cost = bits flip hue ki sankhya.
Accounting: har increment par $2 charge karo. Ek bit 0 → 1 set karna $1 cost karta hai aur us bit par $1 store karta hai. Jab ek bit baad mein 1 → 0 flip hoti hai (carry ke dauran), apne stored $1 se pay karo.
Valid kyon: har 1 bit par hamesha exactly $1 credit hota hai, isliye saare cascading carries prepaid hain. Increment ki amortized cost === O ( 1 ) == hai, chahe ek increment k bits flip kare.
Intuition KYA (sabse powerful, sabse reusable)
Ek potential function Φ ( D ) define karo jo data-structure state D ko ek real number mein map kare ("stored energy"). Ek operation ki amortized cost uski actual cost plus potential mein change hai:
c ^ i = c i + Φ ( D i ) − Φ ( D i − 1 )
YEH KAISE KAAM KARTA HAI (telescoping derive karo):
∑ i = 1 m c ^ i = ∑ i = 1 m c i + ( Φ ( D m ) − Φ ( D 0 ) ) .
Potential differences telescope karte hain. Agar hum ensure karein ki Φ ( D m ) ≥ Φ ( D 0 ) (usually Φ ≥ 0 aur Φ ( D 0 ) = 0 ), toh
∑ c ^ i ≥ ∑ c i .
Toh amortized total real total ko upper-bound karta hai — honest.
Worked example Dynamic array potential ke zariye —
Φ scratch se derive karo
Maano n = elements ki sankhya, s = capacity. Hum Φ chahte hain jo table fill hote samay badhta rahe taaki copy ke liye pay kar sake.
Choose Φ = 2 n − s jab n ≥ s /2 (aur ≥ 0 , jo hold karta hai kyunki n ≥ s /2 ⇒ 2 n ≥ s ).
Non-resizing push (n → n + 1 , s unchanged):
c ^ = c i 1 + ( 2 ( n + 1 ) − s ) − ( 2 n − s ) = 1 + 2 = 3.
Kyon: push 2 units potential add karta hai (future ke liye bachata hai) 1 real write ki cost par.
Resizing push (table full: n = s , copy n , phir s → 2 s , n → n + 1 ):
Actual cost c i = n + 1 (copy n + insert 1).
Pehle: Φ = 2 n − s = 2 n − n = n .
Baad: Φ = 2 ( n + 1 ) − 2 s = 2 ( n + 1 ) − 2 n = 2 .
c ^ = ( n + 1 ) + ( 2 − n ) = 3.
Yeh punchline kyon hai: badi real cost n + 1 potential ke n − 2 drop hone se cancel hoti hai. Stored energy copy ke liye pay karta hai. c ^ = O ( 1 ) — teesra method, same answer. ✓
Recall Feynman: ek 12-saal ke bacche ko samjhao
Ek piggy bank imagine karo. Har baar jab tum ek chhota kaam karte ho, tum 3 coins daalte ho par kaam sirf 1 coin ka "effort" leta hai. Extra 2 coins pile up hote hain. Kabhi kabhi ek BAHUT BADA kaam aata hai (poora kamra saaf karna). Rone ki jagah, tum piggy bank tod dete ho — aur surprise, tumne exactly itne coins bachaye hain jo usse pay karne ke liye kafi hain! Toh chahe ek kaam bahut bada lagta tha, average par har kaam ne tumhe sirf 3 coins cost kiye. Yahi averaged-but-guaranteed price amortized cost hai, aur piggy bank potential function hai.
Mnemonic Teen methods yaad karo
"A-A-P" → A ggregate (sab Add karo, divide karo), A ccounting (bank Account / credit), P otential (Physics energy Φ ).
Aur master equation: "Actual plus Delta-Phi" — c ^ = c + ΔΦ .
Amortized cost kis tarah ka average hai — inputs par ya sequence par guaranteed? Kisi bhi sequence of operations par guaranteed worst-case; koi probability/randomness NAHI hai.
Aggregate method ek line mein m ops ka total cost T ( m ) seedha compute karo, phir amortized cost = T ( m ) / m (har op ke liye same).
Dynamic array push O ( 1 ) amortized kyon hai jabki O ( n ) copies hain? Copies geometrically rarely hoti hain; total copy work = 1 + 2 + ⋯ + 2 k < 2 m , toh total T ( m ) < 3 m .
Accounting method validity condition Total stored credit ∑ ( c ^ i − c i ) hamesha ≥ 0 rehna chahiye (bank kabhi negative nahi jata).
Potential method ki master equation c ^ i = c i + Φ ( D i ) − Φ ( D i − 1 ) (actual cost plus potential mein change).
Potential method valid upper bound kyon deta hai? Telescoping: ∑ c ^ i = ∑ c i + Φ ( D m ) − Φ ( D 0 ) ; agar Φ ( D m ) ≥ Φ ( D 0 ) toh ∑ c ^ i ≥ ∑ c i .
Ek achhe potential function ki do requirements Φ ( D 0 ) = 0 aur Φ ( D i ) ≥ 0 har i ke liye.
Table doubling ke liye potential function Φ = 2 n − s (n = #elements, s = capacity), c ^ = 3 per push deta hai.
Binary counter increment ki amortized cost O ( 1 ) — $2 charge karo: $1 bit set karne ke liye, $1 us par store karo uske future 1 → 0 flip ke liye pay karne ke liye.
Amortized vs average-case complexity ka farq Amortized = ek sequence par deterministic guarantee; average-case = inputs ke random distribution par expectation.
Dynamic Arrays / Table Doubling — canonical amortized example.
Big-O, Big-Omega, Big-Theta — amortized bounds abhi bhi asymptotic notation mein express hote hain.
Disjoint Set Union (Union-Find) — path compression + union by rank potential ke zariye analyze kiya gaya.
Splay Trees — O ( log n ) amortized ek potential function ke zariye.
Fibonacci Heaps — decrease-key O ( 1 ) amortized marked nodes ke potential se.
Geometric Series — aggregate method ke peeche ka summation engine.
Average-case with randomness
Sum c-hat >= Sum actual cost