Sub topic 4
Input Space Versus Auxiliary Space
This chapter assumes you can read Python, know Big O notation for time at an introductory level, and understand what space complexity measures. Here you will learn to separate the memory an algorithm is given from the memory it asks for.
Two kinds of memory
Every function works with some data it receives and some memory it creates. Space complexity analysis keeps these apart.
- Input space is the memory taken by the arguments. For a list of n numbers, it is O(n). The caller already allocated it.
- Auxiliary space is the extra memory the algorithm allocates while it runs. This includes local variables, new lists, and call stack frames.
- Total space is input space plus auxiliary space.
When people say "this algorithm uses O(1) space", they usually mean auxiliary space. Input space is the same for every algorithm that reads the same input, so it does not help you compare them.
A worked comparison
Both functions below read a list of n numbers.
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 creates one integer, s, and the loop variable x. The count of variables does not change with n, so auxiliary space is O(1).
doubled creates result, which ends up holding n values. Its auxiliary space is O(n).
Both functions have total space O(n), because the input alone is O(n). The difference only shows up when you look at auxiliary space. That is why the split is useful.
Does the output count?
The return value is a gray area. doubled must return n numbers, so any correct version needs O(n) memory for its result. Two conventions exist:
- Count the output as auxiliary space. This is simple and what most interview and course material does.
- Exclude the output and count only working memory. This is common in formal analysis, where the question is how much scratch space the algorithm needs.
Pick one and say so. Under the second convention, doubled has O(1) working space beyond its output.
Common errors
- Counting the input as auxiliary. Saying
totaluses O(n) space becausenumshas n items mixes the two measures. - Forgetting that a call's arguments are references. In Python, passing a list does not copy it. The callee gets a reference, so no new input memory is created. The function only allocates if you copy, slice, or build something new.
- Ignoring the stack. A recursive function can have O(n) auxiliary space without building any list, because each pending call keeps a frame alive. Later chapters cover this.
- Modifying the input and calling it free. If a function changes its input to save memory, the caller may lose data it needed. This is a real cost, even though it does not appear in the auxiliary count.
Quick checklist
For any function, ask these questions in order:
- What are the inputs, and how large are they in terms of n?
- What new containers, strings, or copies does the function create?
- How large does each of them get as n grows?
- Does the function call itself or build up pending work?
- Which convention am I using for the output?
The auxiliary space is the largest growth rate among the items you find in steps 2 to 4. Add them up if they exist at the same time, and keep only the dominant term.
Exercise
A function takes a list of n strings and returns the length of the longest string. It uses a loop and one integer variable. What are its input space, auxiliary space, and total space?
Show answer
Input space is O(n), because the list holds n strings. Auxiliary space is O(1), because it keeps only a few variables whose count does not depend on n. Total space is O(n), since the input term dominates.
Exercise
A function receives a list of n numbers and returns a new sorted list, leaving the original unchanged. Using the convention that the output counts as auxiliary space, what is the auxiliary space? What changes if you exclude the output?
Show answer
The auxiliary space is O(n), because the function must build a new list of n items. If you exclude the output, the answer depends on the sorting method used internally. A method that sorts using only a few extra variables would have O(1) working space beyond the output copy.