Sub topic 2
What space complexity measures
Space complexity describes how much memory an algorithm needs as its input grows. It is the memory counterpart of time complexity. Time complexity counts steps. Space complexity counts memory cells, such as variables, list slots, and stack frames.
This chapter assumes you can read basic Python, know how lists and recursion work, and have seen Big O notation for time. The next chapters cover Big O for space and the split between input and auxiliary space. Here the goal is to fix what is being measured.
What counts as memory
A running program uses memory in two main places. Local variables and function calls live on the call stack. Objects such as lists and dictionaries live on the heap. Space complexity adds up everything the algorithm holds at the same moment:
- Variables, such as counters and accumulators.
- Data structures the algorithm builds, such as new lists or dictionaries.
- Stack frames from function calls that have started but not yet returned.
Peak usage, not total allocated
Space complexity measures the most memory held at one time, not the sum of everything ever allocated. If an algorithm creates a temporary list, finishes with it, and frees it, that memory can be reused. Only the high-water mark matters.
The measure is also about growth. A function that uses 40 bytes for one input and 80 bytes for an input twice as large grows linearly. The exact byte counts depend on the interpreter and platform. The growth rate does not.
A first comparison
Both functions below read a list of numbers. They differ in how much memory they need beyond the input.
def total(nums):
s = 0
for x in nums:
s += x
return s
def doubled(nums):
result = []
for x in nums:
result.append(2 * x)
return resulttotal keeps one accumulator, s, plus the loop variable x. These two names stay the same however long nums is. doubled builds result, which ends up with one entry per input element. If nums has 1,000 items, result has 1,000 items. If it has 1,000,000, result has 1,000,000.
Recursion also uses memory
Memory use is not limited to the objects you create. Each function call adds a stack frame, and that frame stays until the call returns.
def sum_recursive(nums, i=0):
if i == len(nums):
return 0
return nums[i] + sum_recursive(nums, i + 1)This function creates no list. Still, with n items it has n + 1 calls waiting at its deepest point. Those frames are real memory, so this function's space grows with n.
Common mistakes
- Counting only the data structures you create and forgetting stack frames from recursion.
- Adding up every allocation over the whole run instead of finding the peak.
- Treating exact byte counts as the answer. Space complexity describes growth with input size, not a byte total.
Exercise
A function loops over a list of n numbers and appends the square of each number to a new list, then returns that list. A second function loops over the same list and keeps only a running count of the numbers that are negative. How does the extra memory of each function change as n grows?
Show answer
The first function builds a new list with n entries, so its extra memory grows in proportion to n. The second keeps one counter (plus the loop variable), so its extra memory stays the same whatever n is.
Exercise
Why does sum_recursive use memory that grows with n, even though it never creates a list?
Show answer
Each recursive call adds a stack frame that stays alive until the deeper call returns. At the deepest point there are about n + 1 frames waiting, so the stack memory grows with n.