Space Complexity

  • Contrast between time vs. space complexity.

Space complexity measures the amount of memory that an algorithm needs to run as a function of its input size. That means how much memory is required in the worst case at any point in the algorithm.

Similar to time complexity, we're concerned with how the space consumption grows, in asymptotic terms, as the size of the input increases.

Often space complexity is taken to mean auxiliary space.

  • Auxiliary space is the extra space or the temporary space used by an algorithm during its execution.

  • We can say Space Complexity $=$ Auxiliary Space $+$ Input space.

  • When we compare two algorithms for solving a computational problem, it is arguably more useful to compare their auxiliary space usage since the input space will be the same for a given problem.

Auxiliary space and space complexity are not the same. In this class, we will specify when to find auxiliary space, but when asked for space complexity, consider that Space Complexity $=$ Auxiliary Space $+$ Input space.

Exercise Complete the following table. Use Big-Oh notation to asymptotically describe time/space complexities.

AlgorithmTime ComplexitySpace ComplexityInput SpaceAuxiliary Space
Linear search
Binary search
Solution

The following is based on the worst-case analysis.

AlgorithmTime ComplexitySpace ComplexityInput SpaceAuxiliary Space
Linear search$\Omicron(n)$$\Omicron(n)$$\Omicron(n)$$\Omicron(1)$
Binary search$\Omicron(\lg n)$$\Omicron(n)$$\Omicron(n)$$\Omicron(\lg n)$ or $\Omicron(1)$

The auxiliary space of binary search depends on its implementation. An iterative implementation takes $\Omicron(1)$, but a recursive implementation could be $\Omicron(\lg n)$ — unless the programming language used has optimization for tail recursion (beyond the scope of this course).

Please refer to this article for "Iterative vs. Recursive Binary Search Algorithm."

Resources