3.1.4 · HinglishComplexity Analysis

Space complexity — auxiliary vs total

1,410 words6 min readRead in English

3.1.4 · Coding › Complexity Analysis


HUM KYA MEASURE kar rahe hain?


SPACE COUNT kaise karein — first principles

Hum har "memory cell" ko count karte hain jo program ek saath alive-and-holding rakhta hai (peak simultaneous usage), phir dominant term rakhte hain.

Rules of thumb (derived, memorized nahi):

  1. Ek fixed number of scalar variables ⇒ (constant — ke saath nahi badhta).
  2. Ek array/list jiska length ke proportional ho ⇒ .
  3. Ek 2D table of size .
  4. Recursion stack space leta hai = (max recursion depth) × (space per frame). Yeh space ka sabse zyada miss hone wala source hai!

Figure — Space complexity — auxiliary vs total

Worked Examples



Active Recall — Flashcards

#flashcards/coding

What is auxiliary space?
Woh extra memory jo algorithm input se alag allocate karta hai (temp vars, helper arrays, recursion stack).
What is total space?
Input space + auxiliary space.
Why do we usually report auxiliary space when comparing algorithms?
Kyunki algorithm sirf scratch memory control karta hai; input size caller ne fix ki hoti hai.
What hidden cost does recursion add to space?
Call stack — depth × frame size; max simultaneous frames auxiliary space mein count hote hain.
Auxiliary space of in-place array reversal?
O(1).
Auxiliary space of naive recursive Fibonacci?
O(n) (max stack depth), O(2^n) NAHI jo uska time hai.
Merge sort vs quick sort auxiliary space?
Merge O(n) (temp array); Quick O(log n) average (recursion stack).
Total space formula?
Total = S_input + S_aux.
Why is storing all subsets O(2^n · n) space?
2^n subsets hote hain, har ek n elements tak lamba hota hai.
A function with 5 scalar locals and no arrays — auxiliary space?
O(1), constant count jo n se independent hai.

Recall Feynman: ek 12-saal ke bachche ko samjhao

Socho tumhe LEGOs ka ek bada dabba diya gaya hai (yeh input hai). Kuch banana ke liye, tum kabhi kabhi ek khali tray nikalte ho pieces sort karne ke liye — woh tray tumhari auxiliary space hai, woh extra cheez jo tum khud laaye ho. Total space dabba aur tray dono milake hoti hai. Ek smart builder ek chhoti tray use karta hai (kam auxiliary). Aur dhyan do! Agar tum ek ke andar ek nayi chhoti side-projects shuru karte jaate ho (recursion), toh har ek ko apni chhoti tray chahiye, aur woh sab ek saath table par rakhti hain — yeh trayon ka ek sneaky dher hai jise tum count karna bhool gaye.


Connections

  • Time complexity — Big-O notation — same asymptotic machinery, alag resource.
  • Recursion and the call stack — hidden auxiliary space ka source.
  • Merge Sort / Quick Sort — classic space trade-off.
  • In-place algorithms auxiliary ke liye design kiye gaye algorithms.
  • Asymptotic notation — Big-O, Big-Omega, Big-Theta — constants kaise drop karte hain.

Concept Map

splits into

splits into

equals

includes

includes

fixed by

measures

counted via

dominant source

depth n gives

in-place example

common mistake

Space complexity

Auxiliary space

Total space

Input + Auxiliary

Input space

Caller hands data

Algorithm cleverness

Peak simultaneous cells

Recursion stack depth

O of n

O of 1 aux

Looks O of 1 but n frames alive