3.3.6 · HinglishHashing

Load factor — when to resize, rehashing cost

1,997 words9 min readRead in English

3.3.6 · Coding › Hashing


Load factor KYA hota hai?

Load factor woh ek number hai jo control karta hai ki hash table kitna fast hai. Chhota = fast hai lekin memory waste hoti hai. Bada = memory bachti hai lekin slow ho jaata hai.


speed ko KYU control karta hai?

Chaining: har operation mein expected work

Maano simple uniform hashing (har key equally likely hai kisi bhi bucket mein jaane ke liye, independently). Jo chain tum scan karte ho uski expected length average chain length hai, jo exactly hai.

Yeh step kyun? keys mein se har ek kisi bucket mein hai. Kisi given key ke tumhare bucket mein hone ki probability hai. Expected number of sharing = . Toh jo chain tum walk karte ho uski expected length hai.

➡️ Agar hum rakhen (ek chhota constant, jaise ), toh . Yahi poora reason hai ki hum resize karte hain.

Open addressing: expected probes

Uniform hashing aur ke saath, unsuccessful search ke liye expected probes ki number ek geometric-style argument se nikali jaati hai:


ko scratch se derive karna (open addressing)


Resizing KAISE kaam karta hai (aur pehle expensive kyun hai phir sasta kyun)

elements ka ek rehash cost karta hai. Yeh scary lagta hai — ek insert kabhi-kabhi linear time leta hai! Toh phir table ko per operation kyun maana jaata hai?

Figure — Load factor — when to resize, rehashing cost

Amortized cost — doubling trick, derived


Sab kuch milake: resize rule



Flashcards

Hash table ka load factor kya hota hai?
, stored keys aur buckets ka ratio.
Chained hash table mein expected search cost (uniform hashing)?
.
Open addressing mein unsuccessful search ke liye expected probes?
.
Open addressing chaining (~1.0) se pehle (~0.7) resize kyun karta hai?
Kyunki sharply blow up hota hai jaise , jabki chaining cost sirf linearly badhti hai ki tarah.
elements ke single rehash ki cost?
— har key ko naye size ke modulo re-insert karna padta hai.
Resize ke dauran hum sirf buckets copy kyun nahi kar sakte?
Bucket index hai; change karne se index change ho jaata hai, isliye keys ko re-place karna padta hai.
Table doubling ke saath per insert amortized cost?
; inserts pe total move work hai.
Additive growth (jaise +100) kharaab performance kyun deta hai?
Har constant interval pe rehash → total work → amortized per insert.
Doubling ke amortized bound ke peeche geometric series?
.
Alag grow/shrink thresholds (hysteresis) kyun use karte hain?
Grow↔shrink thrashing rokne ke liye jab ops ek single boundary ke paas alternate karti hain.
Agar 1500 keys expect karo pe, minimum ?
.

Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho ek coat-check room jisme hooks hain. "Load factor" yeh hai ki kitne coats har hook pe hain. Kam coats ho toh instant hai — ek hook, ek coat. Jaise-jaise bharta hai, ek hi hook pe kai coats latke rehte hain aur tumhe flip karna padta hai, slower hota jaata hai. Toh jab room teen-chauthaayi bhara hota hai, tum sab kuch double hooks wale room mein shift kar dete ho. Sabko move karna ek badi mehnat hai — lekin tum yeh kabhi-kabhi hi karte ho, aur kyunki har naya room pichhle se double hai, yeh kaam almost kabhi nahi karna padta. Saare coats jo tumne kabhi bhi latkaaye, unke upar spread karo toh yeh mehnat almost kuch nahi karti per coat. Yeh "average mein almost kuch nahi" hi hai jo hum amortized kehte hain.


Connections

  • Hashing — parent concept; hashing fast tab hi hoti hai jab bounded rahe.
  • Separate Chaining tolerate karta hai; cost .
  • Open Addressing chahiye; cost .
  • Amortized Analysis — aggregate/accounting method jo inserts prove karta hai.
  • Dynamic Arrays — same doubling trick; ArrayList/vector identically grow karte hain.
  • Hash Functions — uniform hashing assumption saare formulas ko underpin karta hai.

Concept Map

measures fullness

measures fullness

higher a increases

avg chain length a gives

geometric sum gives

degrades

degrades

keep a small triggers

explodes near 0.7 triggers

requires

cost spread over inserts

Load factor a = n/m

Chaining

Open addressing

Collisions

Search cost O(1+a)

Expected probes 1/(1-a)

Resize / grow table

Rehashing: move all keys

Amortized O(1)