17. Space Complexity
Time is not the only resource that is of interest in algorithms. Another important one is the amount of memory that algorithms require. The minimum amount of memory required to solve various computational problems can be studied through space complexity.
Space Complexity Classes
The space cost of the Turing machine
Definition. The (worst-case) space cost of the Turing machine
is the function where is the maximum space cost of on any input of length .
For every function
We can also extend the notion of space cost and space complexity classes to verifiers and to probabilistic Turing machines, by again counting the number of distinct squares of the input tape visited during execution. Doing so leads to analogues of the time complexity classes we have seen so far.
Space Class | Definition |
---|---|
All languages decidable by polynomial-space verifiers. | |
All languages |
|
The class
Proposition.
.
Proof. Consider the simple exhaustive search algorithm for solving
. Given a formula , we start by checking to see if the assignment satisfies it. If it does, we accept. Otherwise, we erase our work on the tape (leaving the encoding of and the assignment to that we just checked), increment the assignment, and repeat. If we have checked all the possible assignments of and none have satisfied , then we halt and reject. This simple algorithm decides
because it accepts whenever has a satisfying assignment and rejects otherwise. And because it reuses the same memory for each assignment test, the space cost of this algorithm is the space required to check a single assignment, which is polynomial in the length of the encoding of .
It even contains some languages that may not be in the polynomial hierarchy
Theorem. The Totally Quantified Boolean Formula language
is in
.
Proof sketch. The proof strategy is the same as the one for
: we iterate over all possible assignments of to see if the condition on is satisfied.
Time and Space
We can establish formal connections between time and space complexity classes in the following way.
Theorem. (Time-Space Hierarchy) For every function
,
Proof. We prove both inclusions separately with extensions of the universal Turing machine.
: Fix any
. Let be a verifier that decides and has time cost . We can simulate all paths of length with a deterministic Turing machine that iterates over all possible certificates to of length . By erasing the contents of the simulation part of the tape between each simulation, we end up with overall space cost since that is the space cost of and also an upper bound on the additional space we require for the simulation. And the resulting simulator accepts if and only if there is a certificate that causes to accept, so it decides .
: Fix any
. Let be any Turing machine with states and tape alphabet that decides and has space cost . On any input of length , can have at most distinct configurations. Since always terminates, it can never be in the same configuration twice in a computational path. Thus, it must have time cost .
This theorem immediately lets us place
Corollary.
.
Since
Savitch’s Theorem
The space complexity analogue of the
Remarkably, we do have an answer to this question. Every nondeterministic Turing machine can be simulated by a deterministic Turing machine with at most a quadratic increase in the amount of space required. This result is known as Savitch’s Theorem.
To prove Savitch’s Theorem, we first consider a seemingly unrelated problem on graphs.
Define the
The exhaustive search algorithm for
We can do much better using a divide-and-conquer algorithm.
Theorem. There is a Turing machine that decides
and uses only squares of the input tape in addition to the ones where the input is written.
Proof. Here is a divide-and-conquer algorithm for solving
:
- STCON
- if
,
- if
or , Accept; otherwise Reject - else
- for each
- if STCON
and STCON , Accept - Reject
The correctness of this algorithm follows from the observation that there is a path of length
from to in if and only if there exists some vertex for which there are paths of length at most from to and from to . And the bound on the space cost comes from the fact that a Turing machine implementing the STCON function with the algorithm above only needs to store the values of
, , and for each instance of the algorithm on the current stack: since gets divided by in each call, the stack has depth at most , so in total the Turing machine can be implemented to use at most squares outside of the original input.
The connection between the
Theorem. (Savitch’s Theorem) For every
,
Proof. Fix any language
. Let be a verifier that decides and has space cost . A string
is in if and only if there exists a sequence of consecutive configurations starting from the initial configuration of on input with some certificate and ending with an accepting configuration . Without loss of generality, we can also assume that is unique (say, by having erase the content of the tape before accepting). As we have seen in the proof of the Time-Space Hierarchy Theorem, a Turing machine with space cost
can be in at most distinct configurations. So the configuration graph for on input has vertices. And to determine if accepts , we want to determine if there is a path of length at most from to in this graph. Using the STCON algorithm, we can solve this problem with
additional squares of the tape if the configuration graph is provided on the tape as part of the input. Of course, we don’t want to write explicitly on the tape, as that would blow up the space cost of the algorithm. But we don’t need to: it suffices to examine the transition function of to determine if any two vertices , in its configuration graph are connected by an edge in the graph. Doing so give us the desired algorithmm that decides with space cost .
Savitch’s Theorem immediately provides a solution to the
Corollary.
Savitch’s Theorem does leave one more fundamental open problem: is the quadratic blowup in space complexity required to go from nondeterministic to deterministic Turing machines?