Sub topic 7
Recursion and the call stack
Recursion costs memory even when a function creates no lists. This chapter assumes you know Big O for time, Python lists, and how a recursive function calls itself.
Where the memory goes
Each function call gets a stack frame. A frame holds the arguments, the local variables, and the place to return to. The frame stays alive until that call returns. A recursive call starts before the caller has finished, so the frames pile up.
The space used by the call stack is the maximum number of frames alive at once, multiplied by the size of one frame. Count the depth, not the total number of calls.
def sum_recursive(nums, i=0):
if i == len(nums):
return 0
return nums[i] + sum_recursive(nums, i + 1)For a list of length n, the calls nest n + 1 levels deep before the base case returns. The addition cannot run until the deeper call returns, so every frame waits. The auxiliary space is O(n), even though the function creates no list. Each frame holds a reference to the same nums, so the list is not copied.
Depth, not call count
Two recursive functions can make very different numbers of calls and still use the same stack space. A naive Fibonacci function makes about 2^n calls. It goes down one branch fully before it starts the other, so the deepest chain of unfinished calls is n. Its stack space is O(n), not O(2^n).
For recursion that splits the problem in half each time, the depth is about log2(n). Binary search written recursively uses O(log n) stack space. Merge sort's recursion has the same depth.
| Recursion pattern | Max depth | Stack space |
|---|---|---|
| Reduce size by 1 each call | n | O(n) |
| Halve the size each call | log n | O(log n) |
| Two calls of size n - 1 (naive Fibonacci) | n | O(n) |
Recursion versus a loop
An iterative version of the same task often needs only a few variables. The next function computes the same sum as sum_recursive.
def total(nums):
s = 0
for x in nums:
s += x
return sHere total uses O(1) auxiliary space, while sum_recursive uses O(n). Both run in O(n) time. Rewriting a recursion as a loop is a common way to cut memory.
Common mistakes
- Counting calls instead of depth. Add up the frames that are alive at the same moment, not every frame ever created.
- Ignoring the stack because "no data structure is created". The stack is auxiliary space.
- Forgetting that recursive slicing, such as
sum_recursive(nums[1:]), copies the list at every level. Each frame then holds its own smaller copy, which makes the total O(n²). Passing an index, as above, avoids this.
Exercise
A recursive binary search takes a sorted list, a target, and indexes lo and hi. It halves the range on every call and makes only one recursive call per level. What is its auxiliary space, and what would it be if you rewrote it as a loop?
Show answer
The recursion depth is about log2(n), so the recursive version uses O(log n) stack space. The loop version keeps only lo, hi and mid, so it uses O(1) auxiliary space.
Exercise
A naive recursive Fibonacci function fib(n) makes about 2^n calls. Why is its auxiliary space O(n) and not O(2^n)?
Show answer
The calls run depth first, and a frame is released when its call returns. At any moment only the chain from the first call down to the current call is alive, and that chain is at most n frames long.