Definitionintro
Space complexity
Also called memory complexity, working space.
The function giving the amount of memory an algorithm uses as a function of input size.
The space complexity of an algorithm is the function that maps each input size to the number of memory cells that the algorithm uses on inputs of size , in a stated model of computation. As for time complexity, the definition must name the case (worst, average) and say what is counted. The usual choice is the worst case, counted in machine words, with the space of the recursion stack included.
Definition
Fix a model, here the word RAM: memory is an array of cells, and each cell holds one word of bits, with so that an index into the input fits in one word. Many texts write that a word has bits. For an algorithm and an input , let be the number of distinct cells that reads or writes during its run on . Then the worst-case space is
where is the set of inputs of size . A cell that is allocated but never accessed is not counted. This is a convention. In practice an allocated array of words counts as words. The two measures agree for an algorithm that initializes all that it allocates.
Total and auxiliary space
Total space counts every cell that the run accesses, including the cells of the input. Auxiliary space (also called working space) counts only the cells outside the input and the output. The auxiliary space is the useful measure for an algorithm that reads an array of items, because the input already takes cells.
Most texts on algorithms use a second convention: the whole input always counts, so the total space is the size of the input plus the auxiliary space. This page counts accessed cells, as defined above. Under this rule a sublinear-time algorithm such as binary search accesses only cells of its input. An algorithm is in place when its auxiliary space is words. Some authors also call an algorithm in place when it uses auxiliary words, for example for a recursion stack.
The recursion stack counts
Each active call of a recursive function holds a frame: its parameters, its local variables and its return address. A frame of a function with a constant number of variables has words. So the stack space is words, where is the greatest number of active calls at one time. A recursive algorithm that holds no other memory has auxiliary space . Space is counted in words. In bits it is at most times larger. With the usual , an array of indices needs words and bits. A mark array of bits needs only bits.
Time and space
Let each step of the machine access at most cells, where is a constant. A run of steps then accesses at most distinct cells. So : the space is at most a constant times the time. This is why an algorithm that must touch each of cells has . The converse is false. An -bit binary counter that counts from to uses bits and increments. For a Turing machine that halts on each input, with a read-only input tape and cells on the work tapes, the time is at most , because a configuration is described by bits plus the head positions and the number of different configurations bounds the time (Arora and Barak, Chapter 4). On the Turing machine one counts the work tape only, which makes sublinear classes such as logarithmic space possible, and polynomial space is the class with .
Examples and non-examples
Each entry is the auxiliary space in words for a sort of items or for a search.
- Linear search, selection sort, insertion sort. A few indices and one temporary key. The space is .
- Heapsort. The heap is built inside the input array, and a sift-down loop needs a constant number of words (two indices and one temporary key). The auxiliary space is , so heapsort is in place in the strict sense.
- Merge sort. The merge step writes into a second array. A version with an auxiliary array of items has auxiliary space, and a version that copies only the left half before each merge needs items. The recursion has depth , so it holds frames. The total auxiliary space is , and the stack is of lower order.
- Quicksort, stack space. After a partition, the algorithm calls itself on the smaller side and continues with the larger side in a loop. A call is made only on a range of at least 2 items. With this rule the stack never holds more than frames, for . The partition must put the pivot in its final place and leave it out of both ranges. Proof: a call at depth (the first call has depth ) starts with a range of items. At any time its frame works on a range of items. A partition of this range leaves two ranges with items in total, so the child call gets at most items, and . Since , we get . A call has , so and . The number of frames is at most , and it is an integer. A version that recurses on both sides, with the last item as pivot, holds about frames on a sorted array, so its space is in the worst case.
- Binary search. The loop version uses words. The recursive version holds frames, unless the compiler removes tail calls.
- Breadth-first search and depth-first search. A search of a graph with vertices and edges needs a mark for each vertex and a queue or a stack of at most vertices, when each vertex is marked as it is pushed (or when the depth-first search is recursive). Then the auxiliary space is . An iterative depth-first search that pushes every neighbor without a test of the mark can hold entries.
- Naive recursive Fibonacci. The function makes calls to depth , so it holds frames, and its space is . Its time is exponential. This shows that space can be much smaller than time.
- Non-example: a bound that omits the stack. The statement "recursive quicksort or recursive binary search uses extra space" is wrong once the stack is counted. Quicksort that recurses on the smaller side uses words in the worst case. A bound that omits the stack is a bound on the heap memory only, and it should say so.
- Non-example: cumulative allocation. A routine that allocates a new array of words in each of loop rounds and frees it at the end of the round has peak space . The sum of all allocations is words. Space is the peak use, not the sum.
Related definitions
- Time complexity counts steps. Space and time are two separate costs, and many algorithms trade one for the other.
- In-place algorithm: auxiliary space , or in the weaker use.
- Word RAM: the model with words of bits in which space is counted in words.
- Logarithmic space and polynomial space: the complexity classes that bound the space of a Turing machine.
- Input size: the argument of .
References
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Chapter 2 (Section 2.3, merge sort) and Chapter 7 (quicksort, stack depth)
- Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition, Section 5.2.2 (sorting by exchanging, stack size of quicksort) and Section 5.2.3 (heapsort)
- Michael Sipser, Introduction to the Theory of Computation, third edition, Chapter 8 (Space Complexity)
- Sanjeev Arora and Boaz Barak, Computational Complexity: A Modern Approach, Chapter 4 (Space complexity)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag RF1AVH Edit this page History Report an error Markdown JSON