Lecture 36
Lecture 36
Lecture #36
Sarmad Abbasi
Virtual University
SPACE COMPLEXITY
Deterministic Space Complexity
Non-deterministic space complexity
Examples of Space efficient TMs.
Reachability Problem
Savitch’s Theorem
Let M be a Turing machine that halts on all inputs. We say that M runs
in space s(n) if for all inputs of length n the machine uses at most s(n)
space.
L = {x ∈ {0, 1}∗ : x = x r }
this is the set of all palindromes over {0, 1}. Lets look at two TMs that
accept this language and see how much space they require.
Question
Is it possible to accept L in space o(n)?
The problem is that we are not allowed to write on the input tape.
Since, it is read only. Thus we must copy the contents on the input
tape to the work tape. This would give a machine that takes space n at
least. Can we some how do better? Here is another idea.
A few points to remember that are made clear by this example are:
1 If you are claiming that you have a sublinear space TM then make
sure that you are not overwriting the input or using that space.
2 The machine M1 takes time O(n) and space O(n). On the other
hand M2 takes time O(n2 ) and space O(log2 n). So M1 is better in
terms of time and M2 better in terms of space. This is quite typical.
3 If you are asked to design a machine that takes less space then
no consideration should be given to time.
Let us consider some facts which make space complexity very different
from time complexity.
C1 , . . . , Cm
be the clauses of φ.
This shows:
Theorem
SAT is in SPACE(n).
P 6⊆ SPACE(n).
ENFA
L(A) = L(D)
As a consequence we get:
Theorem
If A is a NFA with q states with L(A) 6= ∅ then A accepts a string of
length at most 2q .
Now, if L(A) = ∅ then all branches of the above NTM will reject. On the
other hand if L(A) 6= ∅ then there will be at least one branch that will
accept. Thus
L(N) = ENFA
The above NTM requires 2q space. Now, we just notice that in order to
simulate an NFA we do not need to know all of x but only the current
symbol of x. Thus we can reuse space. We can be more space
efficient by “guessing the symbols” of x as we go along. Let us
consider a new machine.
This NTM requires a counter that can count upto 2q . Such counter
requires q bits. Thus the whole simulation can be done in linear space.
Thus we have
Theorem
ENFA ∈ SPACE(n).
Lets define
Given a graph G and two vertices s and t decide if there is a path from
s to t.
You learn two algorithms to solve this problem. One is called DFS and
the other is called BFS. Rough outline is the following:
1 Mark s
2 repeat n − 1 times.
3 if there is a edge from a marked vertex a to an unmarked
vertex b then mark b.
4 If t is marked accept. Else reject.
The point to notice is that both these algorithm require us to mark the
vertices (which initially are all unmarked). Therefore, we need to have
one bit for each vertex and we set it when we mark that particular
vertex. Thus the whole algorithm takes linear space.
Question
Can reacability be solved in sublinear space?
(vi , vi+1 ) ∈ E
for all i = 0, . . . , k − 1.
Fact
If b is reachable from a via a path of length k ≥ 2 then there is a vertex
c such that:
1 c is reachable from a via a path of length dk /2e and
2 b is reachable from c via a path of length dk /2e.
Fact
If b is reachable from a then it is reachable via a path of length at most
n. Where n is the number of vertices in the graph.
Fact
b is reachable from a via a path of length 1 if and only if (a, b) ∈ E.
Now, let us write a function reach(a,b,k). This function will return true if
b is reachable from a via a path of length at most k .
reach(a,b,k)
1 If k = 0 then
2 if a = b return true. Else return false.
3 If k = 1 then
4 if a = b or (a, b) ∈ E return true. Else return false.
5 For each vertex c ∈ V
6 if (reach(a,c,dk /2e ) and reach(c,b,dk /2e)
7 return true;
8 return false;
How much space does this program take?
If the vertex set of the graph is V = {1, . . . , n} then each vertex can be
stored in log2 n bits. Similarly, the parameter k is a number between 1
and n and requires log2 n bits to store. Thus each call will require us to
store O(log n) space on the stack. So, the space requirements are
The depth of the recursion is only dlog2 ne. Since, we start with n and
each recursive call reduces the it by a half. Hence the total space
requirements are
O(log2 n)
Theorem
reachability is in SPACE(log2 n).
Theorem
If M runs in space s(n) then there are at most
2O(s(n))
distinct configurations of M on x.
Theorem
If M accepts an input in space s(n) then it accepts it in time
2O(s(n)) .
This theorem is true for both deterministic and non-deterministic TMs.
and [
NPSPACE = NSPACE(nk )
k
Question
Is
PSPACE = NPSPACE?
PSPACE = NPSPACE.
We will prove this theorem. This theorem is a consequence of the
following theorem.
Theorem
Let f (n) ≥ n then
Let us for a moment make the assumption that we can compute f (n)
easily (we will come back to this point). Let us now define a problem
called yeildability. Suppose we are given two configurations c1 and c2
of the NTM N and a number t. The question is
We further assume that N will use space f (n). Consider the following
TM (algorithm)
CANYEILD(c1 , c2 , t)
1 If t = 1 then
2 if c1 = c2 or c2 follows from c1 via rules of N return true. Else
return false.
3 For each configuration c 0 of length at most f (n)
4 if (CANYEILD(c1 , c 0 , dt/2e ) and CANYEILD(c 0 , c2 , dt/2e)
5 return true;
6 return false;
Once again this is a recursive program. So, we have to see how much
space does it require. It stores the configurations on the stack that
takes space
O(f (n)).
1 Input x.
2 compute f (n)
3 Let c0 be the initial configuration of M on x.
4 For all accepting configurations cf of length at most f (n)
5 if CANYEILD(c0 , cf , 2O(f (n)) ) accept.
6 reject.
This completes the proof of our theorem. There is one technicality left.
If you see the algorithm, we have said we will compute f (n). However,
we may not know how to compute f (n). In that case, what can we do?
This is just a technicality and we can add the assumption that f (n) is
easily computable to our theorem. Another way to get around it is as
follows:
Note that we can use the above function to compute if the initial
configuration yields a configuration of length f or not. We can start with
f = n and and increment its value till we find the length of the largest
configuration that we can get to from the initial configuration. We can
use that value of f now to see if M reaches an accepting configuration
of length f .
Note that we can use the above function to compute if the initial
configuration yields a configuration of length f or not. We can start with
f = n and and increment its value till we find the length of the largest
configuration that we can get to from the initial configuration. We can
use that value of f now to see if M reaches an accepting configuration
of length f .
NSPACE(nk ) ⊆ DSPACE(n2k )
and therefore,
PSPACE = NPSPACE.