Proving space cost: auxiliary space and recursion depth
About this lesson
Time is not the only cost. Here you measure memory: the extra space a function needs on top of its input. Watch a recursive Python function stack one frame per waiting call, find the peak, and prove it needs Θ(n) extra space, while a loop doing the same job needs Θ(1). The Software lesson "How fast is fast? Big O" introduces counting steps, and the Software lesson "Recursion" shows how a function calls itself; the Maths lessons "Logarithms and exponents" and "Recurrences: describing a value by smaller cases" cover the functions and recurrences used here.
Space means extra memory
Time counts steps; space counts memory. The list a = [4, 1, 5, 2] is the input, n = 4 numbers. It is in memory before any call, so we count only the extra memory a function makes: its auxiliary space.
a = [4, 1, 5, 2] n = 4
total space = input + auxiliary
auxiliary space = extra memory the function
uses while it runs
the input is the same for every algorithm,
so space cost counts only the auxiliary partRead the lesson step by step
Space means extra memory
Time counts steps; space counts memory. The list a = [4, 1, 5, 2] is the input, n = 4 numbers. It is in memory before any call, so we count only the extra memory a function makes: its auxiliary space.
a = [4, 1, 5, 2] n = 4 total space = input + auxiliary auxiliary space = extra memory the function uses while it runs the input is the same for every algorithm, so space cost counts only the auxiliary partEach call gets a frame
Call rsum(a). Python gives every running call a frame: a small block of memory for its local names, here a and i. Frames stack up on the call stack. One call running means one frame.
rsum([4, 1, 5, 2]) frame for i = 0 a -> the same list (a reference, not a copy) i = 0 call stack: 1 frameA call waits for its call
Line 4 cannot add a[i] until rsum(a, i + 1) answers. So the frame for i = 0 waits, and a new frame goes on top for i = 1, then 2, then 3. Every waiting call keeps its frame.
i = 0 waits for rsum(a, 1) i = 1 waits for rsum(a, 2) i = 2 waits for rsum(a, 3) i = 3 waits for rsum(a, 4) call stack: 4 frames, all still in memoryThe base case: the peak
At i = 4, i == len(a), so this call returns 0 without calling again. Right now 5 frames are in memory at once, one for each i from 0 to 4. That is n + 1, the highest the stack gets.
i = 4: i == len(a) -> return 0 frames at this moment: i = 0, 1, 2, 3, 4 peak = n + 1 = 5 framesUnwinding frees the frames
Now each call finishes and its frame is freed. i = 3 returns 2 + 0 = 2, i = 2 returns 5 + 2 = 7, then 8, then 12. The stack shrinks to nothing, but the peak ring stays at 5.
i = 4 returns 0 i = 3 returns a[3] + 0 = 2 + 0 = 2 i = 2 returns a[2] + 2 = 5 + 2 = 7 i = 1 returns a[1] + 7 = 1 + 7 = 8 i = 0 returns a[0] + 8 = 4 + 8 = 12 rsum([4, 1, 5, 2]) == 12 frames over time: 1 2 3 4 5 4 3 2 1 0Space is the peak
Auxiliary space is the most extra memory in use at once, not the total ever used. rsum peaks at n + 1 frames, each a fixed size, so its auxiliary space is Θ(n). With no depth limit, a million numbers need a million frames.
each frame holds i and a reference to a: the same size for every n space(n) = (n + 1) · one frame = Θ(n) freed memory is reused, so the cost is the peak (n + 1), not the sum of all framesA loop needs one frame
lsum adds the same list with a loop. The counters now track lsum: one call, one frame, holding total and x. They are overwritten each pass, so memory stays the same for any n: auxiliary space Θ(1). Both take Θ(n) time.
lsum([4, 1, 5, 2]) == 12 one frame: a, total, x total = 0, 4, 5, 10, 12 (one box, rewritten) lsum: peak 1 frame -> Θ(1) auxiliary space rsum: peak n + 1 -> Θ(n) auxiliary spacePython caps the depth
Deep stacks are a real limit. CPython allows 1000 nested calls by default (see sys.getrecursionlimit()) and then raises RecursionError. So rsum fails on a list of 2,000 numbers, while lsum adds a million without trouble.
import sys sys.getrecursionlimit() # 1000 in CPython rsum(list(range(2000))) RecursionError: maximum recursion depth exceeded lsum(list(range(1_000_000))) 499999500000
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01How many frames does rsum have on the call stack at its peak for a list of 10 numbers?
One frame for each i from 0 to 10: the calls for i = 0..9 are all waiting when the base case i = 10 runs. That is n + 1 = 11.
02What is auxiliary space?
Auxiliary space is the extra memory the algorithm makes. It is measured at the busiest moment, because freed memory is used again.
03rsum makes n + 1 calls in total and its peak is n + 1 frames. Why do we count the peak and not the total?
Space is memory in use at one moment. For rsum the two numbers happen to match, but a function that calls itself twice and returns between the calls can make many calls while only a few frames are alive at once.
04lsum adds a list with a for loop. What is its auxiliary space?
One call, one frame, holding total and x. Those names are overwritten on each pass, so the memory used does not grow with n.
05In CPython with the default settings, what happens when you call rsum on a list of 2,000 numbers?
CPython's default recursion limit is 1000 (sys.getrecursionlimit()). rsum needs 2,001 nested calls, so Python raises RecursionError. lsum has no such limit.
Glossary
- Auxiliary space
- The extra memory an algorithm uses while it runs, on top of its input. Space cost usually means this.
- Frame
- The block of memory for one running function call. It holds the call's local names and where to continue when it returns.
- Call stack
- The pile of frames for calls that have started but not finished. A new call goes on top; a return removes the top frame.
- Recursion depth
- How many calls of a recursive function are running at the same time: the height of its part of the call stack.
- Base case
- The input for which a recursive function answers without calling itself again. It stops the stack from growing.
- Peak
- The most memory in use at any one moment. A space bound is a bound on the peak.
- Reference
- A memory address that points to an object. Passing a list to a function passes a reference, not a copy.
- RecursionError
- The Python exception raised when calls nest deeper than sys.getrecursionlimit() allows.
Notes and sources
- Cost model: one frame counts as one unit of memory. The byte size of a real frame depends on the Python version, but it is the same for every call of rsum, so counting frames gives the right growth rate.
- Passing a list to a function does not copy it: each frame holds a reference (a memory address, 8 bytes on a 64-bit machine). If rsum sliced the list instead (rsum(a[1:])), every frame would hold its own shorter copy and the peak would be Θ(n²).
- Freed memory is used again by the next call, so space is measured at the busiest moment. For a recursive function that is the depth of the deepest chain of waiting calls.
- The code is real Python 3: rsum([4, 1, 5, 2]) and lsum([4, 1, 5, 2]) both return 12. The default limit of 1000 is a CPython setting and sys.setrecursionlimit can change it, but a too-high limit can crash Python, as its documentation warns.
- Python Language Reference: Execution model (execution frames)
- Python Language Reference: Data model, frame objects
- Python Library: sys.getrecursionlimit and sys.setrecursionlimit
- Python Library: RecursionError
- CPython source: Py_DEFAULT_RECURSION_LIMIT (Include/internal/pycore_ceval.h)
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 3: Sorting (in place = O(1) extra space)
- Sedgewick and Wayne, Algorithms 4th ed., §1.4 Analysis of Algorithms (memory)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 3rd ed. (MIT Press, 2009): §2.1 sorting in place; Problem 7-4 stack depth for quicksort
Quiz Unlocks after the last step.