The space complexity of an algorithm or a data structure is the amount of memory space required to solve an instance of the computational problem as a function of characteristics of the input. It is the memory required by an algorithm until it executes completely. This includes the memory space used by its inputs, called input space, and any other (auxiliary) memory it uses during execution, which is called auxiliary space.
Similar to time complexity, space complexity is often expressed asymptotically in big O notation, such as
O(n),
O(nlogn),
O(n\alpha),
O(2n),
Analogously to time complexity classes DTIME(f(n)) and NTIME(f(n)), the complexity classes DSPACE(f(n)) and NSPACE(f(n)) are the sets of languages that are decidable by deterministic (respectively, non-deterministic) Turing machines that use
O(f(n))
f
The space hierarchy theorem states that, for all space-constructible functions
f(n),
f(n)
f(n)
The following containments between complexity classes hold.
Furthermore, Savitch's theorem gives the reverse containment that if
f\in\Omega(log(n)),
As a direct corollary,
PSPACE=NPSPACE.
The Immerman–Szelepcsényi theorem states that, again for
f\in\Omega(log(n)),
NSPACE(f(n))
See main article: L (complexity).
L or LOGSPACE is the set of problems that can be solved by a deterministic Turing machine using only
O(logn)
n
logn
LOGSPACE and other sub-linear space complexity is useful when processing large data that cannot fit into a computer's RAM. They are related to Streaming algorithms, but only restrict how much memory can be used, while streaming algorithms have further constraints on how the input is fed into the algorithm.This class also sees use in the field of pseudorandomness and derandomization, where researchers consider the open problem of whether L = RL.[1]
The corresponding nondeterministic space complexity class is NL.
The term refers to space other than that consumed by the input.Auxiliary space complexity could be formally defined in terms of a Turing machine with a separate input tape which cannot be written to, only read, and a conventional working tape which can be written to.The auxiliary space complexity is then defined (and analyzed) via the working tape.For example, consider the depth-first search of a balanced binary tree with
n
\Theta(logn).