3.1.4 · D3Complexity Analysis

Worked examples — Space complexity — auxiliary vs total

2,774 words13 min readBack to topic

Before anything, one plain-words reminder so no symbol is unearned:


The scenario matrix

Every space question you will ever meet falls into one of these cells. The Cell column is the label we tag each worked example with.

Cell Case class What makes it distinct Typical auxiliary answer
A Constant scratch, big input fixed # of scalars, in-place aux, total
B Linear scratch you build a new array of size aux
C Recursion — linear depth stack grows to depth aux
D Recursion — logarithmic depth balanced divide-and-conquer aux
E Zero / degenerate input or , empty structures still valid — check the limit
F 2D table grid of size or
G Exponential output must store things
H The exam twist time space; branching recursion depth, not node count
I Real-world word problem translate English → cells depends — you decide

We now walk all nine cells with fully worked examples.


Figure — Space complexity — auxiliary vs total

The figure above is the mental model for the whole page: a table holds two piles of cells — the fixed input pile (blue, you can't shrink it) and the auxiliary pile (yellow, the part your cleverness controls). Recursion adds a stack of trays (red) that all sit on the table at once.


Cell A — Constant scratch, big input


Cell B — Linear scratch you build


Cell C — Recursion with linear depth

Figure — Space complexity — auxiliary vs total
  1. Look at the red tower in the figure. Each call to rec_sum is paused waiting for the deeper call to finish, so its stack frame stays alive. Why this step? A frame can only be freed after the call it is waiting on returns — see Recursion and the call stack.
  2. Count peak simultaneous frames. The deepest call is at ; below it sit frames . That is frames all alive at once. Why this step? Space is peak simultaneous cells, and here the peak is the moment just before the first return.
  3. Multiply by frame size. Each frame holds data, so total . Why this step? Auxiliary space of recursion depth frame size.

Verify: The iterative twin — a for loop with one accumulator — never stacks frames, so its auxiliary is . Same answer, same time, but vs space. The recursion cost was real. ✅


Cell D — Recursion with logarithmic depth

Figure — Space complexity — auxiliary vs total
  1. Track the halving in the figure. Each call throws away half the remaining range. Why this step? If you halve repeatedly, the count of halvings to reach size is .
  2. Read off the depth. Number of nested calls , so at most frames coexist. Why this step? Depth = number of times we can halve before hitting the base case.
  3. Apply the recursion rule. Auxiliary . Why this step? Same "depth × frame size" law as Cell C, but the depth is now logarithmic.

Verify: For , depth . Ten frames, not a thousand. This is exactly why balanced Quick Sort uses average auxiliary — its partition is in-place and its recursion depth is when the pivots split evenly. ✅


Cell E — Zero and degenerate inputs


Cell F — 2D tables


Cell G — Exponential output


Cell H — The exam twist (time ≠ space)

Figure — Space complexity — auxiliary vs total
  1. Look at the recursion tree in the figure. Yes, there are nodes total — but they are not all alive at once. Why this step? Time counts every node ever visited; space counts only nodes alive simultaneously.
  2. Trace one path down the tree. The computer explores depth-first: fib(n) → fib(n-1) → fib(n-2) → .... Only the frames along one root-to-leaf path are on the stack at any instant. Why this step? When a branch returns, its frame is freed before the sibling branch starts.
  3. Measure the longest path. The deepest chain is , length . So peak stack depth . Why this step? Depth of the tree, not size of the tree, sets the space.

Verify: time (number of nodes), space (tree height). They genuinely differ. If a problem says "give the space complexity of naive fib," the answer is , not . ✅


Cell I — Real-world word problem


Every cell, in one glance


Active recall

Recall Which cell? Classify each on sight

A function builds an matrix ::: Cell F → auxiliary. A function recurses by halving a sorted array ::: Cell D → . A branching recursion visiting nodes but only deep ::: Cell H → space (height), not . Reversing a string in place with two pointers ::: Cell A → . Generating every permutation and storing them all ::: Cell G-flavour → (count × length). An algorithm called on an empty array ::: Cell E → still constant, not zero.


Connections

  • Space complexity — auxiliary vs total (index 3.1.4) — the parent; definitions live there.
  • Recursion and the call stack — the source of Cells C, D, H.
  • Merge Sort / Quick Sort vs auxiliary, the D-vs-B contrast.
  • In-place algorithms — Cell A designed on purpose.
  • Time complexity — Big-O notation — the resource Cell H deliberately contrasts against.
  • Asymptotic notation — Big-O, Big-Omega, Big-Theta — why we drop the s and constants.