Introduction To Computational Complexity: A B A B B
Introduction To Computational Complexity: A B A B B
Introduction
In the lecture on Turing reducibility, we witnessed how an instance of problem A can be solved by
successively solving instances of another problem B. This represents a “positive” use of reducibility,
in that an algorithm for one problem leads to an algorithm for another. In this lecture we look at a
“negative” use of reducibility, in the sense that a reduction from a hard problem A (i.e. a problem for
which no known efficient algorithm exists) to a problem B lends evidence to the belief that B is also
hard. Despite obtaining a “negative” result, this approach seems quite useful, since many problems
encountered in practice are inherently (but often not provably) difficult, and reducibility helps lend
supporting evidence.
The logic of negative reducibility proceeds as follows. Suppose it is believed, if not proved, that
there is no efficient (e.g. polynomial-time) algorithm for solving problem A. Now suppose A can
be efficiently reduced to problem B. Then if there is an efficient algorithm for solving B, then it
implies an efficient algorithm for solving A. Therefore, it must be believed, if not proved, that no
such algorithm for solving B exists.
Based on the kind of reducibility that will be employed in this lecture, it will seem more convenient to
assume that each problem is a decision problem; i.e. a problem whose solution involves returning
a true or false value. For example, SAT, CNF-SAT, k-SAT, 3SAT, 2SAT, and Perfect Matching
are all examples of decision problems. Moreover, even a problem whose solution involves determining
a structure or numerical value can be defined as a decision problem. For example, the problem of
finding a minimum spanning tree for a weighted connected graph G can be defined as the problem of
deciding if G has a spanning tree of cost not exceeding k, for some integer k. Thus, by introducing
an additional parameter k, the minimum-spanning tree optimization problem is transformed into a
decision problem.
1
Example 1. Recall that the Minimum Spanning Tree (MST) problem takes as input a connected
weighted graph G = (V, E, w) and returns a spanning tree for G whose edge weights sum to a
minimum value. Re-define this as a decision problem.
2
It should be emphasized that there is little if anything lost by treating a problem as a decision
problem. For example, if the original problem involves finding a structure that optimizes some
quantity, and its corresponding decision problem can be efficiently solved, then almost invariably the
original problem can also be efficiently solved.
Example 2. Suppose the MST decision problem can also be solved in polynomial time. Show that
its corresponding optimization problem can be solved in polynomial time, and that an mst can be
constructed in polynomial time.
3
When working with decision problem L, we often replace the phrase “L can be solved” with that of
“L can be decided”. Similarly, solvable decision problems are often referred to as being decidable.
Many-to-one Reducibility
Theorem 1. The Clique problem is the problem of deciding if a simple graph G = (V, E) has a
clique of size k, for some integer k ≥ 0; i.e. a subset C ⊆ V of k vertices that are pairwise adjacent.
Show that 3SAT is polynomial-time many-to-one reducible to Clique.
Proof of Theorem 1. Let C be a collection of m clauses, where clause ci , 1 ≤ i ≤ m, has the form
ci = li1 ∨ li2 ∨ li3 . We proceed to construct a simple graph f (C) = G = (V, E) that has an m-clique
if and only if C is satisfiable. G is defined as follows. V consists of 3m vertices, one for each literal
lij , 1 ≤ i ≤ m and 1 ≤ j ≤ 3. Then (lij , lrs ) ∈ E iff i 6= r and lij is not the negation of lrs (i.e. they
are logically consistent).
Conversely, assume G has an m-clique. Then the clique must have the form R = {l1j1 , l2j2 , . . . , lmjm },
since no two literals from the same clause are adjacent. Moreover, by the definition of G, R is a
consistent set of literals and the i th literal of R is a member of ci . Hence the assignment aR induced
by R satisifes every clause of C, and thus C is satisfiable.
4
Example 3. Show the reduction given in the previous example using
5
The Complexity Class P
A complexity class represents a set of decision problems, each of which can be solved within a
certain limit placed on time and space (i.e. memory). For example, the complexity class P denotes
all decision problems that can be solved in a polynomial amount of time. More specifically, decision
problem L is a member of P iff there exists a predicate function P (x) for which
1 if x is a positive instance of L
P (x) =
0 otherwise
and P (x) can be decided in an amount of time bounded by p(|x|), for some polynomial p.
Example 4. From previous lectures we may conclude that the decision-problem versions of the the
following problems belong to class P: Minimum Spanning Tree, Single Source Distances, Unit
Task Scheduling, Huffman Coding, Fuel Reloading, Fractional Knapsack, Find Statistic,
Find Median, Minimum Distance Pair, Addition, Multiplication, Matrix Multiplication, Minimum
Subsequence Sum, Edit Distance, Optimal Binary Search Tree, Matrix Chain Multiplication,
DAG Longest Path, Optimal Change, All-Pairs Distances, 2SAT, Maximum Matching, Maximum
Flow.
Decision problem L is a member of complexity class NP iff there exists a polynomial-time decidable
predicate P (x, y), such that x is a positive instance of L iff the predicate ∃yP (x, y) evaluates to true,
where the domain elements of y that are tested in P satisfy |y| ≤ p(|x|), for some polynomial p.
Although, for any given value of y, P (x, y) can be be decided in polynomial time, we see that the
addition of quantifier ∃y adds a potential order of complexity to the deciding of x, since it may be
necessary to compute P (x, y) an exponential number of times in order to find an appropriate witness
y0 , for which P (x, y0 ) evaluates to true. This is true since there can potentially exist an exponential
number of domain values of y whose size does not exceed p(|x|), for some polynomial p, and hence it
may be necessary to evaluate P (x, y) an exponential number of times before identifying a witness.
6
Example 5. We show that SAT ∈ NP. Given Boolean formula instance F (x1 , . . . , xn ) of SAT, let y
be a variable whose domain consists of all possible assignments α over V = {x1 , . . . , xn }. Then F is
satisfiable iff
∃y eval(F, y)
evaluates to true. Moreover, from the previous lecture, we know that eval(F, y) can be decided in
time proportional to |F |. Moreover, each assignment α ∈ dom(y) satisfies |α| = n ≤ |F |. Therefore,
SAT ∈ NP.
7
Example 6. An instance of Clique is i) a simple graph G = (V, E), and ii) a nonnegative integer
k ≤ |V | = n. The problem is to decide if G has a clique of size at least k. Show that Clique ∈ NP.
8
Theorem 2. P ⊆ NP.
Do there exist decision problems that are in NP but not in P? This would imply that the use of
the ∃ operation is necessary for solving some problems with a polynomial-time predicate function.
The question remains unanswered since there are decision problems in NP for which no efficient
polynomial-time algorithms are currently known, while at the same time no one has yet to prove that
these problems do not admit efficient algorithms.
9
The Complexity Class co-NP
Given a decision problem L, the complement of L, denoted L, is that decision problem for which
a positive (respectively negative) instance x of L is a negative (respectively positive) instance of L.
Example 7. If L is the problem of deciding if a positive integer is prime, then define its complement
L.
Example 7 Solution.
10
Example 8. Define the complement of the SAT decision problem.
Example 8 Solution.
11
A Logical definition of Co-NP
It is left as an exercise to show that if L ∈ P then L ∈ P. On the other hand, the complexity class
co-NP is defined as follows. Problem L is the the class co-NP if and only if L is a member of NP. It is
generally believed that NP and co-NP represent two unequal complexity classes. One reason for this is
that co-NP has its own independent logical definition. Indeed, it is an exercise to show that decision
problem L is a member of co-NP iff there exists a polynomial-time decidable predicate P (x, y), such
that x is a positive instance of L iff the predicate ∀yP (x, y) evaluates to true, where the domain
elements of y that are tested in P satisfy |y| ≤ p(|x|), for some polynomial p.
12
The Classes PH and PSPACE
that x is a positive instance of L iff the predicate ∃yP (x, y) evaluates to true, where the domain
Qpin P satisfy |y| ≤ P
elements of y that are tested q(|x|), for some polynomial q. Similarly, decision
problem L is a member of k iff there exists a pk−1 -computable predicate function P (x, y), such
that x is a positive instance of L iff the predicate ∀yP (x, y) evaluates to true, where the domain
elements of y that are tested in P satisfy |y| ≤ q(|x|), for some polynomial q.
Pp Qp
From the above definitions, we see that 1 = NP, while 1 = co-NP. Moreover, class PH is defined
∞ P
as PH = ∪ ( pk ∪ pk ).
Q
k=0
Example 9. Consider the deciison problem Clique Partition, where an instance consists of i)
a simple graph G = (V, E), where each vertex of G is labeled with a nonnegative integer, and
ii) a nonnegative intger k. The problem is to decide if every k-clique of G can be partitioned
into two sets, where the
Qp labels of the first set sum to the labels of the second set. Prove that
Clique Partition ∈ 2 .
We note in passing that PSPACE denotes all decision problems that can be decided using a polynomial
amount of space. It is left as an exercise to prove that PH ⊆ PSPACE. Moreover,
P it can Q be shown
that any problem in PSPACE can be logically defined in a manner similar to pk and pk , but now
the number of alternating quantifiers is allowed to grow polynomially (as opposed to being fixed at
k) in the size of input x.
13
NP-Complete Decision Problems
Now that we have an idea of what kind of decision problems belong in NP, we would like a method
for demonstrating that a decision problem is in some sense hard and possibly may not be in P.
Furthermore, if we think about what might constitute a difficult decision problem in a class of
decision problems, the most difficult decision problem would seem to be one for which every other
decision problem in that class can be reduced to. Indeed, if A ≤pm B, then A is no harder than B,
since an efficient algorithm for B leads to an efficient algorithm for A. We say that such a problem
is complete for that class of decision problems. This leads to the following definition. A decision
problem L ∈ NP is said to be NP-complete iff
1. L ∈ NP
Note that, in case L 6∈ NP, then we refer to L as being NP-hard. In particular, the optimization
version of an NP-complete decision problem is usually NP-hard.
Lemma 1. Let P (x, y) be a polynomial-time computable predicate function, and let p be a polyno-
mial. Then for each input x, there exists a Boolean formula Fx for which the following is true:
2. the variables of Fx encode precisely those inputs y for which |y| ≤ p(|x|), and
3. for every input y0 for which |y0 | ≤ p(|x|), P (x, y0 ) evaluates to true iff eval(Fx , α) evaluates to
true, where α is the assignment over the variables of Fx that encodes y0 .
The proof of Lemma 1 is beyond the scope of this course, in that it requires a rigorous mathematical
definition of “polynomial-time algorithm”. Such a definition is typically a subject for a course on the
theory of computation.
Proof of Theorem 2. Let L be an instance of NP. Then there exists a polynomial-time decidable
predicate P (x, y), such that x is a positive instance of L iff the predicate ∃yP (x, y) evaluates to true,
where the domain elements of y that are tested in P satisfy |y| ≤ p(|x|), for some polynomial p. Now
given this p and an instance x of L, by Lemma 1 there exists a polynomial-time computable function
f (x) that maps x to a Boolean formula f (x) = Fx , and that has all three properties described in
Lemma 1. Thus f reduces x in polynomial time to an instance Fx of SAT. Moreover, Fx is satisfiable iff
there exists an assignment α over the variable of Fx for which eval(Fx , α) evaluates to true. But, by
Lemma 1, this will be true iff the predicate ∃yP (x, y) evaluates to true, where the domain elements
14
of y that are tested in P are exactly those that can be encoded by an assignment α. Therefore, x is
a positive instance of L iff f (x) = Fx is a positive instance of SAT, and the proof is complete.
We now build on Theorem 2 to show that a host of other problems are NP-complete (to this date
there are several thousand known NP-complete problems from several areas of computer science and
mathematics). We do this with the help of the following lemma. Its proof is left as an exercise.
Lemma 2. Suppose decision problems L1 and L2 are in NP, and L1 is both NP-complete and
polynomial-time reducible to L2 . Then L2 is NP-complete.
y1 ∧ (y1 ↔ F1 ) ∧ · · · ∧ (ym ↔ Fm )
is satisfiable, and we leave it as an exercise to show that each subformula yi ↔ Fi can be translated
to CNF in linear time with respect to the number of children of ni . Finally, letting CF denote the
union of all clauses obtained from translating each yi ↔ Fi subformula, it follows that F can be
translated to CF in polynomial time, and F is satisfiable iff CF is satisfiable.
15
Example 10. Demonstrate the reduction described in the proof of Theorem 3 for the formula
F (x1 , x2 , x3 ) = (x1 ∧ x2 ) ∨ (x1 ∨ x3 ).
16
Theorem 4. 3SAT is NP-complete.
• Case 5. m ≥ 5. Then
Notice that f (c) can be effectively computed from c in time polynomial in the size of c.
Now suppose C has a satisfying assignment a. Then a satisfies some literal l of c, for every c ∈ C.
In Cases 1-4, f (c) will also be satisfied. In Case 5, the clause containing l will be satisified, and the
clauses not containing l can be satisfied by assigning the z’s appropriate values (verify this!).
Similarly, if C is unsatisiable, and a is some assignment that leaves c ∈ C unsatisfied, then one
can verify, by examining all five cases, that no assignment of the z-variables can satisfy all the
clauses in f (c).
17
Example 12. Given
c = (x1 ∨ x2 ∨ x3 ∨ x4 ∨ x5 ∨ x6 ∨ x7 ),
compute f (c) and verify that an assignment a satisifies c iff there is some assignment of the z variables
that, along with a, satisfies f (c).
18
Theorem 5. The Vertex Cover decision problem is the problem of deciding if a simple graph
G = (V, E) has a vertex cover of size k ≥ 0, for some integer k, i.e. a subset of k vertices C ⊆ V for
which every edge e ∈ E is incident with at least one vertex of C. Then Vertex Cover is NP-complete.
Proof of Theorem 5. The fact that Vertex Cover is in NP is left as an exercise. We show a
polynomial-time reduction from 3SAT. Let C be a collection of m clauses over n variables, where
clause cj , 1 ≤ j ≤ m, has the form cj = lj1 ∨ lj2 ∨ lj3 . We proceed to construct a simple graph
f (C) = G = (V, E) that has a vertex cover of size 2m + n iff C is satisfiable. G = (V, E) is defined as
follows. V consists of the following 3m + 2n vertices.
Clause Edges The three vertices corresponding to a clause are pairwise connected to form a triangle.
In other words, for every 1 ≤ j ≤ m,
Communication Edges A variable vertex l is adjacent to a clause vertex ljk iff ljk and l have the
same lable, i.e. represent the same literal. For example, if l13 = x5 , then clause vertex l13 is
adjacent to variable vertex x5 .
Notice that G can be constructed from C in time polynomial in m and n, since |V | = 2n + 3m and
|E| = 6m + n.
Now assume, that C is satisfiable. Let a = (a1 , . . . , an ) be a satisfying assignment for C. Without loss
of generality, assume that the first literal lj1 of clause cj is satisfied by a, j = 1, . . . , m. Then vertex
cover C ⊆ V has the following 2m + n vertices: i) variable vertex li , where li = xi if ai = 1, and
li = xi otherwise, and ii) clause vertices lj2 , lj3 , j = 1, . . . , m. To see that C is a cover, notice that,
by i), C covers all variable edges, and, by ii) C covers each triangle edge of each clause. Furthermore,
for each clause cj , by ii), the communication edges that are incident with lj2 and lj3 are covered.
Finally, the communication edge incident with lj1 is covered since lj1 is satisfied by a, and hence
lj1 = li , for some i ∈ {1, . . . , n}.
Conversely, assume C is a vertex cover of size 2m + n for G. Then by the way G is constructed,
C must consist of i) n variable vertices li , i = 1, . . . , n, where li = xi or li = xi , and ii) two clause
vertices from each clause (WLOG, assume they are lj2 and lj3 , j = 1, . . . , m). Indeed, C must have
at least one variable vertex li , i = 1, . . . , n, since otherwise some variable edge would not be covered.
Similarly, C must have at least two clause vertices from each clause, since otherwise some triangle
19
edge would not be covered. Moreover, since |C| = 2m + n, we see that it must have exactly one
variable vertex from each of the n pairs, and exactly two clause vertices from each of the m clauses.
Now, since R = {l1 , . . . , ln } is a consistent set of literals, consider the variable assignment aR induced
by R. For each j = 1, . . . , m, notice that lj1 6∈ C. Hence, its communication edge must be incident
with some l ∈ R. In other words, lj1 = li , for some i ∈ {1, . . . , n}, and thus aR satisifes cj , for all
j = 1, . . . , m. Therefore, C is satisfiable.
20
Theorem 6. The Directed Hamilton Path Problem (DHP) is the problem of deciding if directed
graph G = (V, E) possesses a directed simple path having length n − 1. Such a path is called a
Hamilton path. Then DHP is NP complete.
Proof of Theorem 6. The fact that DHP is in NP is left as an exercise. We show a polynomial-time
reduction from 3SAT. Let C be a collection of m ternary clauses over n variables. We proceed to
construct a directed simple graph f (C) = G = (V, E) that has a Hamilton path iff C is satisfiable. G
is defined as follows. G has m clause vertices c1 , . . . , cm and n diamond subgraphs, one corresponding
to each variable xi , 1 ≤ i ≤ n. Diamond subgraph Di consists of a top vertex ti , bottom vertex bi ,
left vertex li , and right vertex ri , along with directed edges
si1 , lci1 , rci1 , si2 , lci2 , rci2 , . . . , sim , lcim , rcim , si(m+1) .
The s vertices are called separators, while the lc and rc pairs correspond with each of the m clauses.
This string of vertices is bidirectionally pairwise connected; i.e.
(li , si1 ), (si1 , li ), (si1 , lci1 ), (lci1 , si1 ), . . . , (si(m+1) , ri ), (ri , si(m+1) ) ∈ E.
Finally, if xi is a literal of clause cj , then edges (rcij , cj ), (cj , lcij ) are added. On the other hand, if
xi is a literal clause cj , then edges (lcij , cj ), (cj , rcij ) are added.
Finally, for 1 ≤ i ≤ n − 1 the edges (bi , ti+1 ) are added to connect the n diamond graphs to-
gether. Note that f (C) = G = (V, E) can be constructed in time that is polynomial in m and n.
Moreover, our goal is prove that C is satisfiable iff f (C) = G = (V, E) has a Hamilton Path starting
at t1 and ending at bn .
For each diamond Di , i = 1, . . . , n, consider a path that begins at the top vertex ti and ends at the
bottom vertex bi , and visits every vertex in the diamond. One such path is in the form of a left
lightning bolt that moves from the top to the left vertex li , cuts across the chain of 3m+1 horizontal
vertices from left to right vertex ri , and moves down to bi . The other path is of the form of a right
lightning bolt that moves from ti down to ri , cuts across the chain of 3m + 1 horizontal vertices
from right to left vertex li , and moves down to bi . Moreover, a left lightning bolt path with
excursions (LBPWE) is a lightning bolt path that, when reaching vertex lcj , takes an excursion
and next visits vertex cj , then returns to the diamond via rcj , provided i) cj has yet to be visited by
a LBPWE within an earlier diamond, and ii) xi ∈ cj . Thus, left LBPWE’s correspond with assigning
xi = 0. Finally, a right lightning bolt path with excursions (LBPWE) is a lightning bolt path
that, when reaching vertex rcj , next makes an excursion to vertex cj , and returns to the diamond
via lcj , provided i) cj has yet to be visited by a LBPWE within an earlier diamond, and ii) xi ∈ cj .
Thus, right LBPWE’s correspond with assigning xi = 1.
21
once, it remains to show that every clause vertex c will be visited exactly once. Let i be the least
index for which either xi ∈ c and ai = 1, or xi ∈ c and ai = 0. Since a satisfies c, at least one of the
two must be true. WLOG, assume xi ∈ c and ai = 1. Then Pi is a right LBPWE and will make an
excursion visit to c, since i) xi ∈ c and hence (rc, c) and (c, lc) are edges connecting diamond Di to
c, and ii) no other previous LBPWE has yet to visit c. Therefore P is directed Hamilton path that
begins at t1 and ends at bn .
Conversely, suppose G has a directed Hamilton path P . Then this path must begin at t1 and end at
bn .
Claim: when P enters a diamond D, it will not subsequently reach a vertex of any other diamond
D0 until first visiting every vertex of D. Suppose, by way of contradiction, that after entering D,
P reaches a vertex in another diamond D0 before reaching the bottom of D. The only way this is
possible is if P makes an excursion visit to some clause c, and, instead of returning back to D, hops
to some other diamond, say D0 . WLOG, assume that P made the excursion to c via edge (lc, c) (i.e.
P is moving across D from left to right). Then how will P be able to return to D and visit vertex
rc? Notice that the only vertices adjacent to rc are lc, c, and s, where s is a separator vertex to the
immediate right of rc. Moreover, since lc and c have already been visited, the only way to reach rc
would be via s. But once rc is visited, there is no other place to go, since all adjacent vertices have
been visited, and the path would have to terminate at rc, which contradicts the assumption that P
is a Hamilton path from t1 to bn .
From the claim it follows that P must have the form P = P1 · · · Pn , i.e. a concatenation of n
paths P1 , . . . , Pn , where each Pi is an LBPWE, i = 1, . . . , n. Now let a = (a1 , . . . , an ) be the truth
assignment where ai = 1 iff Pi is a right LBPWE. It remains to show that a satisfies each c of C.
Since P is a Hamilton path, there must be some subpath Pi for which Pi makes an excursion to c.
WLOG, assume Pi is a right LBPWE. Then, since (rc, c), and (c, lc) are edges that connect c to Di ,
it follows that xi ∈ c and, hence, a satisfies c since ai = 1. A similar argument holds for when Pi is
a left LBPWE. Therefore, C is satisfiable.
22
Example 14. Show the reduction given in Theorem 6 using
23
Subset Sum Problem. Given a finite subset S ⊆ of integers, and an integer t, does there exist a
subset A ⊆ S such that the elements in A sum to t?
Proof of Theorem 7. It is an exercise to show that Subset Sum is in NP. We now show a polynomial-
time reduction to Subset Sum from 3-SAT. Let C be a collection of m ternary clauses over n variables.
The following table provides the reduction from C to f (C) = (S, t). The table rows correspond to the
numbers comprising set S, while t is the number at the bottom.
1 2 3 4 ··· n c1 c2 ··· cm
y1 1 0 0 0 ··· 0 1 0 ··· 0
z1 1 0 0 0 ··· 0 0 0 ··· 0
y2 1 0 0 ··· 0 1 0 ··· 0
z2 1 0 0 ··· 1 0 0 ··· 0
y3 1 0 ··· 0 1 1 ··· 0
z3 1 0 ··· 1 0 0 ··· 1
.. .. .. .. ..
. . . . .
yn 1 0 0 ··· 0
zn 1 0 0 ··· 0
g1 1 0 ··· 0
h1 1 0 ··· 0
g2 1 ··· 0
h2 1 ··· 0
.. ..
. .
gm ··· 1
hm ··· 1
t 1 1 1 1 ··· 1 3 3 ··· 3
Number yi correpsonds to variable xi , while zi corresponds to literal xi . Moreover, the fact that t’s
whole part consists of all 1’s satisfies the requirement that, for a truth assignment A, either ai is set
to 1 or ai is set to 0, but not both. Thus we see that either yi or zi must be added to the subset, but
not both. Also, the fractional part of yi (respectively zi ) indicates to which clauses xi (respectively
xi ) belongs.
Now suppose C is satisfiable via some assignment A. Then the subset A needed to sum to t includes
the following numbers. For 1 ≤ i ≤ n, if ai = 1, then add yi to A; otherwise add zi to A. To see
which gj ’s and hj ’s to add to A, if any, consider clause cj . If cj is satisfied by A only once, then add
both gj and hj to A; otherwise add only gj to A. The reader can check that the elements in A indeed
add to t.
Now suppose that C is unsatisfiable. For the elements of a subset A to sum to t, it is necessary that
either yi or zi must be added to A, but not both. Thus, assuming the elements of A sum to t, let A
be the truth assignment defined by ai = 1 if yi ∈ A, and ai = 0 if zi ∈ A, for 1 ≤ i ≤ n. Then, since
C is unsatisfiable, it must be the case that some clause cj is not satisfied by A, in which case, when
24
summing y and z elements of A in the j th column to the right of the decimal, we see that this sum
equals 0, since none of the y’s or z’s added to A satisfy cj . Thus, even if we choose both gj and hj to
add to A, the j th column to the right of the decimal will add to at most 2, which is one less than
the required value of 3. Therefore, the elements in A cannot sum to t.
25
Exercises
1. Redefine the Minimum Spanning Tree optimization problem as a decision problem.
2. Let A be an algorithm for solving the Minimum Spanning Tree decision problem of the previous
exercise. Given a positive instance of this decision problem, show how A can used to find an
actual minimum spanning tree with the desired cost for the given instance.
4. Let A be an algorithm for solving the 0-1 Knapsack decision problem of the previous exercise.
Given a positive instance of this decision problem, show how A can used to find an actual
subset of goods with the desired profit, and the desired total weight that does not exceed the
knapsack capacity.
6. Suppose A is an algorithm that decides the Vertex Cover decision problem. For a positive
instance of A, describe a procedure that uses A to determine an actual vertex cover of the
desired size for the positive instance.
7. Prove that the 0-1 Knapsack decision problem is in NP. For this exercise (and all subsequent
ones involving showing membership in a complexity class) i) define all variables and, for each
existential (respectively, universal) variable, define its domain and the polynomial p that bounds
the size of each domain element being tested, and ii) define the polynomial-time decidable
predicate function P and justify that it can be computed in polynomial time.
8. A Hamilton Cycle in a simple graph is a cycle that visits every vertex in the graph at most
once. Define the associated decision problem and show that this problem is in NP.
9. Two simple (undirected) graphs G = (V1 , E1 ) and H = (V2 , E2 ) are called isomorphic iff there
exists a one-to-one correspondence f : V1 → V2 between V1 and V2 such that (u, v) ∈ E1 iff
(f (u), f (v)) ∈ E2 , for every pair of vertices u, v ∈ V1 . Are the following graphs isomorphic?
G = ({1, 2, 3, 4}, {(1, 2), (2, 3), (3, 4), (1, 4)}),
and
H = ({a, b, c, d}, {(a, c), (a, d), (b, c), (b, d)}).
If yes, provide the one-to-one correspondence. If no, explain why not.
G = ({1, 2, 3, 4, 5}, {(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 5)}),
and
H = ({a, b, c, d, e}, {(a, d), (a, e), (e, d), (b, c), (a, b), (b, d), (c, e)}).
11. Let ISO be the problem of deciding if two simple graphs G and H are isomorphic. Prove that
ISO ∈ NP.
26
12. Subset Sum is the decision problem that takes as input a set S of nonnegative integers, a
nonnegative integer t, and decides if there is a subset A ⊂ S such that
X
a = t.
a∈A
13. Let G = (V, E) be a simple graph. Subset I ⊂ V is called an independent set of G iff, for
every u, v ∈ I, (u, v) 6∈ E. In other words, I is an independent set iff every pair of vertices in
I are non-adjacent. The Independent Set problem is stated as follows. Given G = (V, E) and
non-negative integer k, does G have an independent set I of size k? Show that this problem is
in NP.
14. Let Triangle be the problem of deciding if a simple graph G has a 3-clique. Prove that Triangle
is in P.
15. Define the complement of the Triangle problem. Give an example of a positive instance of this
problem.
16. A permutation on the set {1, . . . , k} is a one-to-one and onto function on this set. When p is a
permutation, pt means the composition of p with itself t times. Let Perm Power is the problem
of, given input (q, p, t), deciding if q = pt , where p and q are permuations (over {1, . . . , k}), and
t is a nonnegative integer. Prove that Perm Power is in P, Hint: the obvious algorithm does
not run in polynomial time, since the problem size is logarithmic (and not linear) with respect
to the value of t.
17. Let Unary Primes be the set {1n |n is a prime number}. Prove that Unary Primes is in P.
18. Let Unary Subset Sum be the subset sum problem in which all numbers are written in unary.
Show that Unary Subset Sum is in P. Explain why the NP-completeness reduction for Subset
Sum does not work for Unary Subset Sum.
19. Provide a definition for the complement of the Subset Sum problem.
20. Set Partition is the decision problem that takes as input a set S of nonnegative integers and
decides if there is a subset A ⊂ S such that
X X
a=( s)/2.
a∈A s∈S
In other words, can the set be partitioned into two subsets, each of whose elements sum to the
same value? Is the following a positive instance of SUBSET SUM:
? If yes, provide the partition. If not, give a sound argument as to why not. Show that Set
Partition is polynomial-time reducible to Subset Sum. Show that Subset Sum is polynomial-
time reducible to Set Partition.
27
21. Prove that Primes is in co-NP.
22. Prove that polynomial-time reducibility is both a reflexive and transitive relation on the set of
all decision problems.
23. An instance of LPath is a simple graph G = (V, E), two vertices a, b ∈ V , and a nonnegative
integer k ≥ 0. The problem is to decide if there is a simple path of length equal to k from a to
b. An instance of Hamilton Path is a simple graph G = (V, E) along with two vertices a, b ∈ V .
The problem is to decide if there is a Hamilton Path path from a to b, i.e. a simple path from
a to b that visits every vertex. Provide a polynomial-time reduction from Hamilton Path to
LPath, and defend your answer.
24. Let Double-SAT be the problem of deciding if a Boolean formula has at least two satisfying
assignments. Provide a polynomial-time reduction from SAT to Double-SAT.
26. Prove that if some problem L ∈ P is NP-complete, then P = NP. Hint: when proving that set A
equals set B it is necessary and sufficient to prove that A ⊂ B and B ⊂ A.
27. Let Half Clique be the problem of deciding if a simple graph G = (V, E) has a clique of size
bn/2c, where n = |V |. Provide a polynomial-time reduction from Clique to Half Clique. Defend
your answer.
28. Let C be a 3-CNF formula. A 6=-assignment to C is a truth assignment that satisfies C, but in
such a way that every clause of C has at least one literal set to true, but also has one literal
set to false. Prove that, if a is a 6=-assignment, then so is its negation a, where the negation of
an assignment is the assignment that is obtained by negating each assignment value of a. Let
6=SAT be the problem of deciding if a 3-CNF Formula has a 6=-assignment. Prove that 3-SAT
is polynomial-time reducible to 6=SAT, by mapping each clause i of the form (l1 ∨ l2 ∨ l3 ) to
the two clauses
(l1 ∨ l2 ∨ zi ) and (z i ∨ l3 ∨ b),
where zi is a newly introduced variable (one for each clause of C), and b is a single additional
new variable.
29. Given a collection of m-bit binary strings s1 , s2 , . . . , sn , and a nonnegative integer K, the String
Sum decision problem is to decide if there are K strings si1 , si2 , . . . , siK from the collection such
that
si1 + si2 + · · · + siK = 1,
where the addition is bitwise Boolean-algebra addition (e.g. 1001 + 1010 = 1011), and 1 is the
m-bit binary string consisting of all ones. Prove that String Sum is an NP-complete decision
problem. Hint: reduce from Vertex Cover.
30. An instance of Set Splitting is a finite set S and a collection of subsets C = {C1 , . . . , Cm } of
S. The problem is to decide whether or not S can be partitioned into two sets A and B such
that
a. S = A ∪ B
b. A ∩ B = ∅
28
c. Ci ∩ A 6= ∅, for all i = 1, 2, . . . , m
d. Ci ∩ B 6= ∅, for all i = 1, 2, . . . , m.
29
Exercise Hints and Answers.
1. Inputs: weighted graph G = (V, E, w) and integer k. Does G have an mst that has a cost not
exceeding k?
2. Let A be an algorithm that takes as input weighted graph G = (V, E, w) and integer k, and
returns true iff G has an mst with cost not exceeding k. Suppose A returns true on some
input (G, k). We now use A to determine an mst of G whose cost does not exceed k. For each
edge e ∈ E of G, remove e from E: E ← E − e. Run A on input (G, k). If A returns true,
then e is not needed to form an mst of cost not exceeding k. Thus, permanently remove e from
E. Otherwise, add e back to E: E ← E + e. Proceed to the next edge. After iterating over
each edge, E will consist entirely of those edges that form an mst of G with cost not exceeding
k.
4. Let A be an algorithm that takes as input i) Goods G =}g1 , . . . , gn }, where good gi has weight
wi and profit pi , i = 1, . . . , n; ii) capacity M ; iii) integer P . Suppose A returns true on some
input (G, M, P ). We now use A to determine a subset of G whose weights sum to a value not
exceeding M , and whose profits sum to a value that equals or exceeds P . For each good g ∈ G
of G, remove g from G: G ← G − g. Run A on input (G, M, P ). If A returns true, then g is
not needed to add to the knapsack in order to obtain a profit of at least P . Thus, permanently
remove g from G. Otherwise, add g back to G: G ← G + g. Proceed to the next good. After
iterating over each good, G will consist entirely of a set of goods whose weights sum to a value
not exceeding M , and whose profits sum to a value that equals or exceeds P .
5. Define F0 = F . Suppose Fi has been defined. Let Fi+1 be the result of substiuting xi+1 with the
value true in formula Fi . If Fi+1 is satisiable, then the satisfying assignment will assign xi+1 to
true. Otherwise, define Fi+1 be the result of substiuting xi+1 with the value false in formula
Fi . Then Fi+1 must be satisfiable (since Fi is satisfiable, and hence some satisfying assignment
must assign xi+1 either true or false). Repeating this for variables xi , i = 1, . . . , n, will lead
to a satisfying assignment for F .
7. Let G = {g1 , . . . , gn } denote the set of goods, W = {w1 , . . . , wn } the weights, P = {p1 , . . . , pn }
the profits, M the capacity, and k ≥ 0 the profit threshold. Let s be a variable whose do-
main consists of all subsets of G. Then |s| = O(n) is polynomial in the input size. Fi-
nally, P ((G, W, P, M, k), s) is the predicate function that evaluates to true iff s is a subset of
goods, the sum of whose weight does not exceed M , and the sum of whose profit is at least
k. P ((G, W, P, M, k), s) is polynomial-time decidable since the summing of the weights and
30
profits of elements of s can be performed in time linear in n, log M , and log k (we assume that
all weights have O(log M ) bits, and all profits have O(log k) bits). Finally, (G, W, P, M, k) is a
positive instance iff ∃sP ((G, W, P, M, k), s) evaluates to true. Therefore, 0-1 Knapsack is in
NP.
11. Let (G1 = (V1 , E1 ), G2 = (V2 , E2 )) be an instance of Graph Isomorphism, where both graphs
have n vertices and m edges. WLOG, assume V1 = V2 = {1, . . . , n}. Let variable f have domain
equal to the set of one-to-one correspondences from {1, . . . , n} to itself. Then |f | = O(n) is
polynomial in the size of the input. Finally, let P ((G1 , G2 ), f ) denote the predicate function that
evaluates to true iff, for all i = 1, . . . , n, (i, j) ∈ E1 iff (f (i), f (j)) ∈ E2 . Then P ((G1 , G2 ), f ) is
polynomial-time decidable since checking the above can be performed in time linear in both n
and m by constructing hash tables for both E1 and E2 . Finally, (G1 , G2 ) is a positive instance
iff ∃f P ((G1 , G2 ), f ) evaluates to true. Therefore, Graph Isomorphism is in NP.
12. Let (S, t) be an instance of Subset Sum, where |S| = n. Let s be a variable whose domain
consists of all subsets of S. Then |s| = O(n) is polynomial in the input size. Finally, let
P ((S, t), s) denote the predicate function that evaluates to true iff the elements of s sum to
t. Then P ((S, t), s) is polynomial-time decidable since summing n numbers of S can be done
in time linear in n and log t (assuming each element of S has O(log t) bits. Finally, (S, t) is a
positive instance iff ∃sP ((S, t), s) evaluates to true. Therefore, Subset Sum is in NP.
tripleof vertices (x, y, z), check if (x, y), (x, z), (y, z) ∈ E. If yes, return true. Running
14. For each
n
time: O( ) = O(n3 )
3
15. Given a simple graph G = (V, E), is it the case that G does not have a triangle? Positive
instance: G = ({v1 }, ∅).
j
16. Compute p2 , j = 1, . . . , blog tc, and use these powers to compute pt . Thus, pt can be computed
using a O(k log t) steps (since each product requires O(k) steps) and compared with q in k
additional steps.
17. √
Problem input size is now n. Return true√iff it’s not the case that some integer between 2 and
n divides n. This can be checked in O( n) steps.
31
18. It would take exponential time to compute the elements of set S in unary. Thus the reduction
is no longer polynomial time.
19. Given a set S of integers and integer t, is it the case that there is no subset of S whose elements
add to t?
2(t + J) = 2t + 2J = M + J ⇔ J = M − 2t.
21. Let n be √ an instance of Primes. Let d be a variable whose domain consists of all elements
{2, . . . , n}. Then |d| = O(log n) is polynomial in the size of n. Finally, let P (n, d) denote
the predicate function that evaluates to true iff d does not divide evenly into n. Then P (n, d)
is polynomial-time decidable since division of two numbers having log n bits can be performed
in O(log n) steps. Finally, n is a positive instance iff ∀dsP n, d) evaluates to true. Therefore,
Primes is in co-NP.
22. Reflexive: f (x) = x. Transitive. Assume L1 ≤pm L2 via f , and L2 ≤pm L3 via g. Let h :
L1 → L3 be defined by h(x) = g(f (x)). Then x is positive for L1 iff f (x) is positive for L2 iff
h(x) = g(f (x)) is positive for L3 . Moreover, h(x) is computable in time polynomial in |x|, since
computing f (x) can be done in time O(p(|x|) for some polynomial p. Moreover, this means that
|f (x)| ≤ p(|x|). Hence, since g(y) can be computed in time O(q(|y|), and |y| = |f (x)| ≤ p(|x|),
we see that g(y) can be computed in time O(q(p(|x|)), which is bounded by a polynomial of
degree k + l, where k is the degree of p, and l is the degree of q.
23. Given graph G = (V, E) and vertices a, b ∈ V , suppose |V | = n. Then a Hamilton path from
a to b is a simple path of length n − 1. Thus, by definition, (G, a, b) is a positive instance of
HP iff f (G, a, b) = (G, a, b, n − 1) is a positive instance of LPath. Moreover, f is computable
in polynomial time since we only need to add the fourth component of n − 1.
24. f (F ) = F̂ , where F̂ = F ∧ (z ∨ z), where z is a variable that does not appear in F . Then F is
satisfiable iff F̂ has two satisfying assignments (one that sets z = 1, the other that sets z = 0).
In other words, F is a positive instance of SAT iff f (F ) is a positive instance for Double-SAT.
Note also that f (F ) can be computed in polynomial time, since we simply add two nodes to
F ’s tree.
32
25. Use Exercise 17.
26. Given L̂ ∈ NP, let x be an instance of L̂. Compute f (x), where f is the reduction from L̂
to L. This takes polynomial time. Moreover, deciding if f (x) is positive can also be done in
polynomial time, since L ∈ P. Hence, we can decide if x is positive for L̂ in polynomial time.
Therefore, L̂ ∈ P.
27. Given Clique instance (G = (V, E), k), there are three cases for f ((G, k)). Case 1: k = |V |/2.
In this case f ((G, k) = G, since G has a k-clique iff it has a half-clique.
Case 2: k < |V |/2. Then f ((G, k)) = Ĝ, where Ĝ is the result of adding |V | − 2k more vertices
to G and connecting each of them all other vertices in Ĝ. Then, in an analysis similar to the
reduction of Subset Sum to Set Partition (See Exercise 20), G has a k-clique iff Ĝ has a half
clique, since
k + |V | − 2k = 1/2(|V | + |V | − 2k) = |V | − k,
means that Ĝ will have a Half Clique iff G has a k-clique.
Case 3: k > |V |/2. Then f ((G, k)) = Ĝ, where Ĝ is the result of adding 2k − |V | additional
isolated vertices to G. Then G has a k-clique iff Ĝ has a half-clique, since k = 1/2(|V | + (2k −
|V |).
Finally, notice that f is computable in polynomial time with respect to m = |E| and n = |V |,
since at most n vertices and m2 edges must be added to form Ĝ.
28. Let C be a satisifiable instance of 3SAT having variables x1 , . . . , xn . Let a be a satisfy-
ing assignment for C. We can get a satisfying 6= assignment for f (C) over the variables
x1 , . . . , xn , z1 , . . . , zm , b, by extending a to â in the following way. First, assign b = 0. Next,
consider clause ci = (l1 , l2 , l3 ) of C. Case 1: a is not consistent with both l1 and l2 . Then a is
consistent with l3 . Then assign zi = 1. In this case, â is a 6= assignment for
(l1 ∨ l2 ∨ zi ) and (z i ∨ l3 ∨ b).
Case 2: a is consistent with either l1 or l2 . In this case assign zi = 0. Then again, â is a 6=
assignment for
(l1 ∨ l2 ∨ zi ) and (z i ∨ l3 ∨ b).
Thus, assigning b = 0 and the z’s in the above manner show that a positive instance of 3SAT
maps to a positive instance of 6= SAT.
Now assume that C is unsatisfiable. Suppose by way of contradiction that f (C) has a 6=
assignment â. Let a be the restriction of â to variables x1 , . . . , xn of C. Then, since a does
not satisfy C (remember that C is unsatisfiable), there must be a clause ci for which a is not
consistent with ci = (l1 , l2 , l3 ). But â is a 6= assignment for
(l1 ∨ l2 ∨ zi ) and (z i ∨ l3 ∨ b).
The only way this can happen is if â assigns zi = 1 and b = 1.
Now let b̂ be the complement of â. We know from the first part of this exercise that b̂ is also
a 6= assignment for f (C). Once again, let b be the restriction of b̂ to variables x1 , . . . , xn of C.
Then, since b does not satisfy C, there must be a clause cj for which b is not consistent with
cj = (l1‘ , l2‘ , l3‘ ). But b̂ is a 6= assignment for
(l1‘ ∨ l2‘ ∨ zj ) and (z j ∨ l3‘ ∨ b).
33
Again, the only way this can happen is if b̂ assigns zj = 1 and b = 1. But b̂ must assign b = 0,
since it is the complement of â, and â assigns b = 1. Thus, f (C) must not have a 6= assignment,
proving that a negative instance of 3SAT maps to a negative instance of 6= SAT.
Finally, notice that the reduction is clearly polynomial time, since f (C) has n + m + 1 variables
and 2m clauses; both quantities being linear in m + n.
29. Given graph G = ({v1 , . . . , vn }, {e1 , . . . , em }), and integer k, define f ((G, k)) = (S, k), where
S = {s1 , . . . , sn } and si is a binary string of length m, where sij = 1 iff edge ej is incident with
vi . Then with a few moments thought, we can see that G will have a vertex cover of size k iff S
has k strings that sum to 1m , since the latter simply encodes the fact that each edge is covered
by a k-subset of the vertices.
30. Given C, an instance of 6= SAT, let x1 , . . . , xn be its variables, and c1 , . . . , cm its constraints.
Then f (C) consists of set S = {x1 , x1 , . . . , xn , xn }, while C consists of sets c1 , . . . , cm , along
with {x1 , x1 }, . . . , {xn , xn }. Then if a is a 6= assignment for C, let A be the subset of literals
that are consistent with a. For example, if a = (x1 = 1, x2 = 0, x3 = 1), then A = {x1 , x2 , x3 }.
Furthermore, let B = A be the complement of all the literals of A. For example, if A =
{x1 , x2 , x3 , then B = {x1 , x2 , x3 }. Clearly, A ∪ B = S, and both A and B intersect each of the
sets of C. Indeed, since a is a 6= assignment, each clause c will contain an element of A and an
element of B; while each set {xi , xi } will have either xi ∈ A and xi ∈ B, or xi ∈ B and xi ∈ A.
Conversely, if there exist A and B for which S = A ∪ B, and A and B intersect all of the clauses
and literal sets {xi , xi }, then A and B must both be consistent sets of literals. Let a be the
assignment over {x1 , . . . , xn } induced by A, and b the assignment induced by B. Then, b is the
complement of a. Moreover, since both a and b satisfy each clause (since A and B intersect
each clause), we see that a is a 6= assignment for C.
Finally, notice that the reduction can be performed in polynomial time, since the number of
sets C is m + n, which is linear in m + n.
34