Complexity Theory: Selected Exercises With Solutions On
Complexity Theory: Selected Exercises With Solutions On
Complexity theory
Theory of computation
Amir Semmo
Extracted from former homeworks in the course "Theory of computation II", Summer
term 2008, University of Potsdam
October 6, 2008
Contents
Exercise Sheet 1 3
Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Exercise Sheet 2 8
Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Exercise Sheet 3 14
Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2
Exercise Sheet 1
Exercise 1
Specify if the following statements are correct. Justify your answers!
(a) n2 = O(n)
We just have to show that: n2 ≤ c · n, for all n ≥ n0 (n0 > 0) and a c > 0.
We know:
n2 ≤ c · n ⇔ n ≤ c
By this we see, that the proposition cannot be true, because for every x c, there
will always exist a number n which is bigger than c.
(b) 3n = 2O(n)
3n ≤ 2c·n | log
⇔ n · log(3) ≤ c · n · log(2) |:n
⇔ log(3) ≤ c · log(2)
3
Theory of computation II
Amir Semmo Exercise Sheet 1
and since we have eleminated n, we can say that this equation is alawys true for
any c > 0 we choose!
n n
(c) 22 = O(22 )
(d) 2n = o(n2 )
We can show that this statement is also true, by having a look at the limit of:
Since the limit is 0, we showed that for any c > 0, the expression 2n is smaller than
c · n2 for all n ≥ n0 (n0 > 0).
(e) n = o(log n)
This statement is not true. We can show this again with the help of the limit:
n l0 Hospital
lim = lim 1 = lim n = ∞
n→∞ log(n) n→∞ 1/n n→∞
(f ) 1 = o(1/n)
The last statement is not true (it is almost the same as the previous proposition):
lim 1 = lim n = ∞
n→∞ 1/n n→∞
4
Theory of computation II
Amir Semmo Exercise Sheet 1
Exercise 2
Show that P is closed under the star operation, that means: If L ∈ P , then
L∗ = {w | w = w1 · · · wn for n ∈ N and w1 , . . . , wn ∈ L} is also in P .
Note: The approach, enumerating all decompositions of a word, doesn't work, be-
cause there are 2n−1 possibilities for the decomposition of a word (with the length n)
into non-empty parts. Instead of that, think about how you can reduce the problem
of the containedness of a word w in L∗ to the containedness of parts of w in L∗
The procedure of our Turing Machine M can be illustrated by the following example:
5
Theory of computation II
Amir Semmo Exercise Sheet 1
• ...
⇒ A = {0, 2, 3, 5}.
By this, the set A contains all indices, which indicate from which position part
words of w are contained in L, in our example: w1 · w2 , w3 and w4 · w5 .
We solve the question if a word w is in L∗ , by building w step by step, and recording
in a set A, if a partition is present on the basis of previous results (subproblems).
This is often adressed as 'dynamic programming ' in literature.
6
Theory of computation II
Amir Semmo Exercise Sheet 1
Exercise 3
Show that the language
is in P .
5. Run through all marked states and check if each of these states is an accepting
state. If at least one state is marked, but is not an accepting state: reject,
else: accept."
What we are actually doing here, is marking every state, which can be reached by a
valid sequence of conguations. This is done by the method of 'Depth-rst search'.
We can implement the algorithm for DFS (in our case step 3-4) in O(n). All other
steps are either constant operations (step 2) or also linear (step 1 and 5).
By this we have found an algorithm, implemented by the Turing Machine M ,
which is clearly polynomial in it's time complexity. M also recognizes ALLDF A ,
so ALLDF A has to be in P .
7
Exercise Sheet 2
Exercise 1
A cycle in a directed graph G is a nite sequence of vertices v1 , . . . , vn of G, where
v1 = vn and G contains an edge from vi to vi+1 for all i ∈ {1, . . . , n − 1}. Specify
an ecient algorithm, which decides if a graph contains a cycle. Explain why your
algorithm is correct and analyze its time complexity.
8
Theory of computation II
Amir Semmo Exercise Sheet 2
4.4 Mark the edge e as visited and check if u is marked as gray. If this is the
case: accept. Else mark u as gray, set it as the new vertex v and continue
with step 5.2.
The main idea is marking all those vertices as gray, which can be reached by a
previosuly chosen start vertex. If there is a cycle, there will be the point where we
will encounter an already marked vertex. The stack helps to return to a previous
vertex, in order to check if there are still outgoing edges (the main principle of
'Depth-rst search').
9
Theory of computation II
Amir Semmo Exercise Sheet 2
Exercise 2
Consider the following problem:
The certicate is the subset N of G, which mark the proof for the membership of
hG, ki in IN DEP EN DEN T − SET . N contains all those vertices in G, which
form a valid independent set. They can be encoded as a sequence of indices, point-
ing to the relevant vertices in VG .
10
Theory of computation II
Amir Semmo Exercise Sheet 2
It is also clear, that W runs in polynomial time. The rst step is linear, where w
just check for a well-formed input. The rst loop runs in O(n2 ), where we check if
N ⊆ V . The second loop in step 3 also runs in O(n2 ), because we run through all
edges of E (O(n)) and check for each edge if it is contained in E (O(n))).
By this W runs in polynomial time and is a valid verier for IN DEP EN DEN T −
SET , wtih the conclusion that IN DEP EN DEN T − SET ∈ N P .
For the second part we need to show that CLIQU E ≤P IN DEP EN DEN T −SET ,
or in other words:
2. Set E 0 = E .
3. Iterate for i = 0 to |V |.
3.1 Iterate for j = 0 to |V |.
3.1.1 If (vi , vj ) ∈ E : Delete (vi , vj ) from E 0 . Else insert (vi , vj ) to E 0 .
4. Return hG0 , ki, where V 0 = V and E 0 as the new set for edges, and halt."
After the rst step of P , all edges are inserted to the structure E 0 which not have
been existed before in E , and all edges are removed, which are already in E . Thus
we gain via the new set E 0 the complement structure of E . Consequently we can
say, that:
11
Theory of computation II
Amir Semmo Exercise Sheet 2
Finally P runs in O(n3 ), because the outer loop in step 3 will be executed exactly
n-times, the inner loop in step 3.1 n-times and step 3.1.1 runs in O(n), which results
in O(n3 ).
Alltogether P calculates p, which represents a valid polynomial time reduction from
CLIQU E to IN DEP EN DEN T − SET .
12
Theory of computation II
Amir Semmo Exercise Sheet 2
Exercise 3
Let us assume that N P 6= coN P . By this we can conclude, that there mus exist a
language in N P , which is not in coN P , or in other words:
/ NP .
∃L ∈ N P : L ∈
But this would mean, that N P is not closed under the operation of complement.
Not it is well-known, that P is closed under the operation of complement. We can
conclude:
∀LP ∈ P : LP ∈ P .
Consequently we showed: P 6= N P .
13
Exercise Sheet 3
Exercise 1
Show that N P and coN P are subsets of P SAT and if N P = P SAT , it would follow
that N P = coN P .
Note: P SAT is the set of languages, which can be decided in polynomial time by a
deterministic Turing Machine with an oracle SAT .
First we will show that N P ⊆ P SAT , which is just the same as we need to show
that:
∀L ∈ N P ⇒ L ∈ P SAT .
2. Ask the oracle SAT with the input p(w). If the oracle answers with 'yes':
accept. Else reject."
14
Theory of computation II
Amir Semmo Exercise Sheet 3
For the second part we need to show that coN P ⊆ P SAT , or in other words:
L ∈ N P ⇒ L ∈ P SAT .
This can be achieved via the construction of M SAT , where we just invert the answer
of the oracle. By this we can be sure that the new M SAT decides L (in polynomial
time).
For the third part we assume that N P = P SAT , where we need to show that
N P = coN P .
At rst we show that coN P ⊆ N P , where we can conclude directly from our as-
sumption N P = P SAT , that P SAT ⊆ N P . We showed in the previous part that
coN P ⊆ P SAT , which results in: coN P ⊆ N P .
L ∈ P SAT ⇒ L ∈ P SAT .
Now we can manage to show our statement by using the precondition that N P =
P SAT , because if this would be the case any language in P SAT would be in N P and
vice versa. For that N P also has to be closed under the operation of complement:
L ∈ NP ⇒ L ∈ NP ,
Finally we showed the two directions: N P ⊆ coN P and coN P ⊆ N P , under the
precondition that N P = P SAT . We can conclude that N P = coN P , which is
exactly what we wanted to show.
15
Theory of computation II
Amir Semmo Exercise Sheet 3
Exercise 2
Proof the following statement: P is closed under nondeleting homomorphisms if
and only if P = N P .
Note: Consider the following language (which is abviously in P ): S = {xy | x ∈
{0, 1}∗ is the encoding of a Boolean formula Φ and y ∈ {t, f }∗ is the encoding of a
variable assignment, which satises the formula Φ }.
We have to show two directions. First we assume that P is closed under nondeleting
homomorphisms, and we need to show that P = N P .
For this we take the SAT problem, which is known to be N P -complete. That
means if we can show with the help of our assumption (P is closed under nondelet-
ing homomorphisms) that SAT ∈ P , we could conclude P = N P .
Let S = {xy | x ∈ {0, 1}∗ is the encoding of a Boolean formula Φ and y ∈ {t, f }∗ is
the encoding of a variable assignment, which fullils the formula Φ } and S is in P .
We construct a homomorphism h with the following rules:
h(t) = #,
h(f ) = # and
h(x) = x, for all x ∈ Σ \ {f, t}.
Obviously we can say: h(S) = SAT 0 , because we just convert the t and f symbols
of S to the number sign (#). By this we can also say that SAT 0 is in P , because
S is in P and by our asumption that P is closed under nondeleting homomorphisms.
In the last step we can "redene" SAT with the help of our new language SAT 0 .
Let M be a Turing Machine which decides SAT :
16
Theory of computation II
Amir Semmo Exercise Sheet 3
1. For each unique literal in Φ, add a # symbol to the end of the input on the
tape.
2. Simulate the decider for SAT 0 (remember: SAT 0 is in P and for this a poly-
nomial time decider exists ) with the tape content as input and accept if the
decider accepts or reject respectively."
The rst step of M runs in polynomial time, as well as the second step, because we
just simulate the polynomial time decider for SAT 0 . Therefore M is a polynomial
time decider for SAT and we can conclude:
P = NP ,
The other direction, where we assume that P = N P , is very easy to show, as soon as
someone knows that N P is closed under nondeleting homomorphisms. From these
two statements it directly follows that P also has to be closed under nondeleting
homomorphisms.
17
Theory of computation II
Amir Semmo Exercise Sheet 3
Exercise 3
Let U N ARY −SU BSET −SU M be a variant of the "Subset-Sum" problem, where
all numbers are encoded unary. So if hx1 , . . . , xn iu = 0x1 1 . . . 10xn is the unary
encoding of the series x1 , . . . , xn , then we have: hS, tiu ∈ U N ARY − SU BSET −
SU M if and only if hS, ti ∈ SU BSET − SU M .
18
Theory of computation II
Amir Semmo Exercise Sheet 3
For the second part we need to show that U SS is in P . This time we won't show
this via detailed reductions to other problems in P , but rather via argumentation.
We could construct a Turing Machine M , which processes the following two steps:
The rst step runs in polynomial time. The input w has the length of n, but because
of the rst step we reduce this length to log(n).
Now we know that M is a deterministic Turing Machine which just simulates an-
other non-deterministic Turing Machine in the second step. A theorem declares
that every t(n) time nondeterministic single-tape Turing Machine has an equiv-
alent 2O(t(n)) time deterministic single-tape Turing Machine. By this we can say
that the secnod step of our Turing Machine M , where we just behave like the non-
deterministic Turing Machine N , runs in 2O(n) .
Now our input has been reduced to the complexity of O(log(n)) after M nished
the rst step. For that we can say that the exponential dimension for the second
step is compensated by the logarithmic dimension of the rst step, which has to
result in a polynomial overall runtime of M .
For that M is polynomial-time decider for U SS and U N ARY − SU BSET − SU M
is in P .
19