









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










We have found already several problems, like the traveling salesman tour (TST) problem, for which it seems hard to come up with an “efficient” algorithm. But what means “efficent” ? To formalize this, it has been suggested, and is now in widespread use, that “efficient” means solvable in time that is polynomial (in the size of the input). Around this, the class NP of problems and the concept of NP-completeness have been developed as a way to classify problems and being able to claim that they are computationally hard.
Intuitively, given two algorithmic problems A and B, we say that A can be reduced to B if we can solve A with an algorithm AA that uses an algorithm AB for B as a subroutine or oracle. In measuring the running time of AA, a call to AB is charged constant time. Since the composition of two polynomials is also a polynomial, we have that if A is polynomial time reducible to B and also B is solvable in polynomial time, then A is solvable in polynomial time. We need to be more precise as to what a problem is and what solving it means.
Problems as Languages. We restrict the discussion to decision problems (the output is Yes or No) and then regard them as languages (set of strings) over an alphabet Σ (usually Σ = { 0 , 1 }) encoding their Yes instances. For example, an optimization problem like the TST problem can be made into a decision problem:
TRAVELING SALESMAN TOUR (TST)
Instance: A complete graph G with set of vertices V with an edge weight function w : V × V → Q≥, and a rational W ≥ 0. Question: Is there a traveling salesman tour, that is, a closed path in G that visits every vertex exactly once, whose total edge weight is at most W?
We write
TST = {〈G, w, W 〉 : G is a graph with edge weights w, and there is a TST with weight at most W }
where 〈·〉 is used to denote an encoding, since we are assuming that we are working with strings over the alphabet Σ.
NPC. A language is in the class NPC (NP-complete) if
(i) L ∈ NP
(ii) for all L′^ ∈ NP, L′^ ≤P L.
Thus, an NP-complete problem is not easier than (or it is as hard as any) other problem in NP.
Two simple but important observations:
Observation 2. If L ∈ NPC, L′^ ∈ NP and L ≤P L′^ then L′^ ∈ NPC.
So, once we have a first NP-complete problem L, to prove that another problem L′^ in NP is NP-complete, it suffices to show that L ≤P L′, and we can avoid the more involved condition (ii) in the definition.
Observation 3. If L ∈ NPC and also L ∈ P then any L′^ ∈ NP is also in P.
So, by showing that a problem is in NPC, we know that it is unlikely that there is a polynomial time algorithm for the problem, as this would imply that all the problems in NP also have a polynomial time algorithm.
In the general satisfiability problem, the instance is a boolean formula over n variables and the question is whether there is a truth assignment to the variables that make the formula true. It is always useful to identify simpler versions of a problem. In the case of satisfiability, the problem is already hard for formulas in conjunctive normal form (CNF) with three literals per clause. More precisely, a formula φ over variables x 1 , x 2 ,... , xn is in CNF if
φ = C 1 ∧C 2 ∧· · ·∧Cm with Ci = i, 1 ∨i, 2 ∨· · ·∨i,ji wherei,s = xr or `i,s = ¬xr , for some r.
Each Ci is called a clause and each `i,s is called a literal. The formula is in kCNF if ji = k for each i. Now, we can define the problem:
kCNF
Instance: A kCNF formula φ over variables x 1 ,... , xn. Question: Does there exist a satisfying assignment of φ, that is, a True/False as- signment to the variables x 1 ,... , xn that makes φ True?
While 2SAT is in P (homework problem), 3SAT is already as hard as the general problem. To show that 3SAT is in NPC, we need to reduce any problem in NPto 3SAT. Before attempting to do the general case, let us see how a particular reduction can be done. We reduce VERTEX-COVER to 3SAT:
We show given a graph G = (V, E), how to construct a boolean formula φ such that G has a vertex cover of size k iff φ is satisfiable. As suggested, φ has a variable xv,i, for each vertex v ∈ V and 1 ≤ i ≤ k (with the meaning that xv,i = 1 iff v is the i-th vertex in a cover of size k).
φ is the conjunction of the following:
(i) For each i, 1 ≤ i ≤ k, a formula that enforces that exactly one vertex is the i-th vertex in the cover: ∧k
i=
v∈V
xv,i ∧
u,v∈V
(xu,i ⇒ ¬xv,i)
(ii) For each edge e ∈ E, a formula that says that one of its vertices is in the cover:
∧
{u,v}∈E
( (^) k ∨
i=
xu,i ∨
∨^ k
i=
xv,i
Theorem 4. 3SAT is in NPC.
Proof. Clearly, 3SAT is in NP. The difficult part is to show that L ≤P 3SAT for all L in NP. This is very technical, though not really difficult. It depends on what model of computation is chosen. Most often “Turing Machines” are used (see the book “Computers and Intractability” by Garey and Johnson, or “Computational Complexity” by Papadim- itriou, among many). See CLRS for the outline of a proof via CIRCUIT-SATISFIABILITY. We outline here a proof (borrowed from the book “Combinatorial Optimization” by Pa- padimitriou and Steiglitz) that uses a simple model that is essentially a Turing machine. The machine consists of a program control with a head that scan a semi-infinite tape; the
0
Program
$ 1 0 1 0 1
program consists of instructions of the form
: if σ then (σ′, d,′)
where is the instruction number, and it means that if σ is the symbol written in the cell currently being scanned by the head, then the head writes the symbol σ in that position, then moves left, right, or not at all depending on d ∈ {− 1 , 0 , +1} and goes to instruction′^ in the program. The program starts with the head at position 0, which contains the special symbol $ with the input string following to the right with another $ at the end, and at the first instruction in the algorithm (program control) A. A valid program should not attempt to move the head to the left of the 0-th cell of the tape. If the algorithm accepts a language L in NP, then the certificate is supposed to follow x in the tape, and there is a polynomial p(n) such that the program on input x and appropriate certificate will accept after at most p(|x|) steps; more precisely, the machine writes 1 on the tape cell |x| + 1 (after the input) and stops with the head there (it gets in a loop so that it remains there). (It takes some work to convince oneself that more powerful models of computation can be simulated in this simple machine, with only a polynomial time blow-up factor. Read more about turing machines if you are interested.)
(iv) E(x) enforces that at after p(|x|) steps the machine scans tape cell |x| + 2 with a 1 written on it:
E(x) = W [p(|x|), |x| + 2, 1]
It is a messy but essentially trivial task to show that x ∈ L iff φ(x) is satisfiable (modulo any minor mistakes in the formulas above).
Instance: An undirected graph G = (V, E) and a positive integer k ≤ |V | Question: Does G have a clique of size at least k, that is, a set of k or more vertices such that all pairs are connected by an edge?
INDEPENDENT-SET
Instance: An undirected graph G = (V, E) and a positive integer k ≤ |V | Question: Does G have an independent set of size at least k, that is, a set of k or more vertices such that no pair is connected by an edge?
VERTEX-COVER
Instance: An undirected graph G = (V, E) and a positive integer k ≤ |V | Question: Does G have a vertex cover of size at most k that is, a set of k or less vertices such that any edge is incident to one of these vertices?
These three are essentially the same problem, as the following lemma indicates:
Lemma 5. Let G = (V, E) be an (undirected) graph, and V ′^ ⊆ V , then the following are equivalent:
(a) V ′^ is an independent set in G.
(b) V − V ′^ is a vertex cover in G.
(c) V ′^ is clique in Gc^ = (V, E′) with E′^ = {{u, v} 6 ∈ E : u, v ∈ V } (G’s complement).
Theorem 6. CLIQUE, INDEPENDENT-SET and VERTEX-COVER are NP-complete.
Proof. Clearly, they are all in NP. Because of the previous lemma, there are straightforward reductions between these problems. So it suffices to show that one of them is in NPC to conlcude that each of them is in NPC. We show a reduction from 3SAT to INDEPENDENT- SET. Let φ be a boolean formula in 3CNF as written above. We want to define f (φ) = 〈G, k〉 where the graph G = (V, E) is such that φ is satisfiable iff G has an independent set of size at least k. The basic gadget in the construction is a “triangle” (3 connected
vertices), in which at most one vertex can be in an independent set. We let k = m and define G = (V, E) as follows:
V = {vi,j : 1 ≤ i ≤ m, 1 ≤ j ≤ 3 } E = {{vi,j , vi′,j′ } : if i = i′^ and j 6 = j′, or i 6 = i′^ and i,j = ¬i′,j′ }
In words, there is a triangle for each clause – each vertex corresponding to a literal in the clause, and two vertices in different clauses are connected by an edge exactly when the cor- responding literals are one the negation of the other. It is easy to see that given an encoding of φ, f (φ) can be constructed in polynomial time. Now, let’s check the further requirement on f : Suppose φ is satisfiable, then there is an assignment to the variables x 1 ,... , xn that makes True at least one of the literals in each clause, say the literals 1 ,k 1 , 2 ,k 2 ,... , m,km , but then the corresponding vertices v 1 ,k 1 , v 2 ,k 2 ,... , vm,km form an independent set because no two of them can be connected (neither can be a negation of the other otherwise they both could not be True); so G has an independent set of size k = m. For the other direction, suppose that G has an independent set of size k = m; no two of the vertices in this independent set could be in the same triangle, so exactly one comes from each triangle, say v 1 ,k 1 , v 2 ,k 2 ,... , vm,km ; we use this to define a truth assignment, namely that the corre- sponding literals 1 ,k 1 , 2 ,k 2 ,... ,m,km are True; this is a consistent assignement because by
¬x 2
(x 1 ∨ x 2 ∨ ¬x 4 ) ∧ (x 2 ∨ ¬x 3 ∨ x 4 ) ∧ (x 1 ∨ ¬x 2 ∨ x 5 ) ∧ (x 2 ∨ x 4 ∨ ¬x 5 )
x 5
¬x 5
x 1
x 2
x 2
x 4
¬x 4 x 2
¬x 3
x 4 x 1
construction a variable and its negation do not appear among these literals; furthermore, this is a satisfying assignment for φ because there is at least one true literal in each clause. This completes the verification of the reduction.
The following problem has a similarity with VERTEX-COVER. Its NP-hardness can be shown by a reduction from VERTEX-COVER.
DOMINATING-SET
Instance: Graph G = (V, E) Question: Does G have a dominating set of size at most k that is, a set V ′^ of k or less vertices such that for any vertex u ∈ V − V ′^ there is an edge {u, v} with v ∈ V ′^?
We now consider a “numeric” problem. This will be useful in proving NP-completeness of problems like KNAPSACK.
x 1 x 2 x 3 C 1 C 2 C 3 C 4 v 1 001 000 000 001 000 000 001 v′ 1 001 000 000 000 001 001 000 v 2 000 001 000 000 000 000 001 v′ 2 000 001 000 001 001 001 000 v 3 000 000 001 000 000 001 001 v′ 3 000 000 001 001 001 000 000 s 1 000 000 000 001 000 000 000 s′ 1 000 000 000 010 000 000 000 s 2 000 000 000 000 001 000 000 s′ 2 000 000 000 000 010 000 000 s 3 000 000 000 000 000 001 000 s′ 3 000 000 000 000 000 010 000 s 4 000 000 000 000 000 000 001 s′ 4 000 000 000 000 000 000 010 t 001 001 001 100 100 100 100
Remark. Note that it is essential that the numbers involved are long. In fact, if they are sufficiently small, then a fast algorithm is possible. Let S = {s 1 , s 2 ,... , sn}. Using dynamic programming (or more like simply recursion in this case), let SUM(i, x) be true or false depending on whether the instance {s 1 , s 2 ,... , si}, of the SUBSET-SUM problem has a solution. We have
SUM(i, x) = SUM(i − 1 , x) ∨ SUM(i − 1 , x − si),
with base case SUM(0, x) equal to true iff x = 0. The number of entries needed is O(nt). However, this can’t be regarded a polynomial time algorithm, since if t is specified with N bits, then t can be almost 2N^. Algorithms with such running times are called pseudo- polynomial.
SUBSET-SUM can be used to derive the NP-hardness of the following problems.
PARTITION
Instance: A set S of positive integers. Question: Is there a set S′^ ⊆ S such that
s∈S′^ s^ =^
s∈S−S′^ s.
KNAPSACK
Instance: Set of n items with positive integer weights w 1 , w 2 ,... , wn and positive integer costs p 1 , p 2 ,... , pn, a positive integer M, and a positive integer P. Question: Is there a set of indices I ⊆ { 1 ,... , n} such that
i∈I wi^ ≤^ M^ and^
i∈I pi^ ≥ P?
kCOLORING
Instance: A graph G = (V, E) and an integer k.
Question: Is there a (proper) coloring of the vertices of G with k colors, that is, is there a function ξ : V → { 1 ,... , k} such that u 6 = v implies ξ(u) 6 = ξ(v)?
Theorem 8. 3COLORING is NP-complete.
Proof. A coloring itself is a poly-size certificate that can be verified in poly-time. We show a reduction from 3SAT (it actually works from kSAT). Let φ be a kCNF formula. We show how to construct a graph G. The construction is based on two gadgets: (i) the truth setting gadgets for the variables, and (ii) the clause satisfaction gadgets. The truth setting
¬xn
vG G R vR
B vB
x 1 ¬x 1 x 2 ¬x 2 xn
vR
mi, 1 mi, 2 mi, 3
1 2 ` 3
vG vG vG
bi, 1 bi, 2 bi, 3
vG
Figure IV.1: Truth setting gadget on the left and clause satisfaction gadget on the right.
gadget consists first of three vertices vG, vR, vB all connected so that they must be colored with 3 different colors G, R, B (green, red, blue). G and R correspond to boolean values True and False. The gadget also has two vertices for each variable, the variable xi and its negation ¬xi (we use these to name the vertices), connected between them and to vertex vB. Thus, in a proper coloring, xi and ¬xi must be colored G, R or R, G, which is a proper truth setting. The satisfaction gadget for clause Ci is shown on the right in the figure. It consists of six vertices mi, 1 , mi, 2 , mi, 3 (middle) and bi, 1 , bi, 2 , bi, 3 (bottom) connected as shown to the 3 vertices corresponding to the literals in Ci, and to the vertices vG, vR. It works as follows: (i) if all literal vertices are colored R (False) then all the middle vertices must be colored B, and so the bottom vertices are forced to take color (from left to right) R, G, R, which is not consistent with the vertex vR that appears on the left; (ii) if at least one literal is G (True) then the corresponding middle and bottom vertices can be colored R and B respectively, and then the other vertices can be colored consistently. We omit a more detailed argument.
2COLORING on the other hand is in P(actually, it can be solved in linear time, how ?). 3COLORING remains NP-complete when restricted to planar graphs. The reduction is basically the same from 3SAT with the addition of a gadget that allows to “transfer color” and at the same time avoid the coorssing of edges: This gadget has the property that in any proper 3-coloring the opposite vertices (t and b, l and r) have the same color, and that any equal coloring of opposite vertices can be extended to a proper coloring of the gadget. Using this gadget, in the previous proof, the graph G constructed can be made planar (a crossing gadget is introduced wherever two edges cross). Some work is needed though to determine these crossings.
{w, y}
s 1
s 2
s 1
s 2
x
y
z
w {w, x}^ {w, z}
{x, y}
Figure IV.2: On the left, a vertex cover is shown with double circles, and the corresponding hamiltonian cycle is shown on the right with a (red) dashed path. Note that vertices s 1 and s 2 on the left and right are copies of the same vertex (to facilitate the drawing).
where z is either t when j < il and b or j > il. The figure below illustrates the construction for the example graph on the left. Now to the verification that G has a vertex cover of size k iff G′^ has a hamiltonian cycle. The implication from G to G′^ is clear from the construction: given the vertex cover of size k, we can describe the path (we only need to be careful when the same edge is covered by two vertices; then we need to assign the edge to one of them). For the implication from G′^ to G, we assume that there is a hamiltonian cycle p in G′. We can split p into subpaths between the si, namely:
s 1
p 1 si 2
p 2 si 3
p 3 · · ·
pk− 2 sik− 1
pk− 1 sik
pk s 1 ,
where i 2 , i 3 ,... , ik is a permutation of 2, 3 ,... , k (we start with s 1 but the path may happen to follow the si’s in some arbitrary permutation). Because of the construction each path pi starts with an edge leading to the first gadget in a line and then it must follow it until the vertex si+1. The claim is that the set of lines so selected correspond to a vertex cover in G. Indeed, by construction and since p is hamiltonian, all the vertices in all the copies of the basic gadgets – which correspond to edges in G – can only be covered if the lines selected for a vertex cover of G.
HAM-CYCLE can be used to derive the NP-hardness of problems like LONGEST-PATH, MIN-DEGREE-MST, etc.
Many optimization problems (in fact many of the problems we are considering) can be posed as integer linear programs.
INTEGER-(LINEAR)-PROGRAMMING
Instance: Integer n-tuples (ai, 1 , ai, 2 ,... , ai,n), i = 1,... , m, and (c 1 ,... , cn), integer m-tuple (b 1 , b 2 ,... , bm), and integer B.
Question: Is there an integer n-tuple (x 1 ,... , xn) such that ∑^ n
i=
cj xj ≥ B
∑^ n
i=
ai,j xj ≤ bi for i = 1,... , m
The linear term in the first condition is the objective function, that is, the term that should be maximized (in the optimization problem). The other inequalities are the constraints. Note that the direction of the inequalities is not very important as they can be reversed via multiplication by −1. Subsequently, we drop the adjective linear since we only deal with linear programs. This problem is NP-complete. However, unlike the other problems we have seen so far, it is not trivial to specify a certificate. We can’t simply say “guess n integers”. How big should they be? For the problem to be in NP, these integers should have a representation that is polynomial in the size of the input. It is not clear that this is always possible. So, for the moment, we can only prove the following.
Theorem 10. INTEGER-PROGRAMMING is NP-hard.
Proof. Let φ be a 3CNF formula with the usual notation. For each boolean variable xi, we introduce an integer variable yi in the integer program. For a variable xi, let p(xi) = yi and p(¬xi) = 1 − yi; this establishes a correspondence from the boolean values True/False to the integer values 0/1. We introduce the following constraints – recall that the i-th clause is Ci = (i, 1 ,i, 2 , `i, 3 ):
p(i, 1 ) + p(i, 2 ) + p(`i, 3 ) ≥ 1 for i = 1,... , m yj ≥ 0 for j = 1,... , j yj ≤ 1 for j = 1,... , j
The constraints on yj force it to have only the values 0 and 1. The first constraints force each clause to have at least one satisfied literal. These constraints already capture the satisfiability of φ. To complete the integer program, we may add some trivial optimization function (the constraints already correspond to satisfying assignments, so any so-called feasible solution will do, but formally we need to define an optimization function). Like
∑^ n
j=
yj ≤ n.
It is easy to check that φ is satisfiable iff the constructed integer program f (φ) has a solution.
In contrast to 3-SAT, it turns out that the problem 2-SAT:
2-SAT Instance: A boolean formula Φ in CNF with exactly two literals per clause. Question: Is there is a satisfying assignment for Φ? is in P. We present three different approaches.
We construct a directed graph GΦ = (V, E) as follows. V is the set of variables in Φ and their negations. For each clause (,′) in Φ, E contains the directed edges (¬,′) and (¬′,), that correspond to the logical formulas ¬→′^ and ¬′^ →, and are equivalent to the original clause. In GΦ, if a literal is set to True, then this forces True on all literals in GΦ reachable from. There is a contradiction if both a variable and its negation are reachable from `. So a possible algorithm is the following. As long as the graph GΦ is not empty, do:
and find all vertices (literals) reachable fromThis is actually pretty much the same as the previous algorithm. We basically try both truth value assignments for each variable. Step 1 or step 3 follow the implications of setting a literal to False(then the other literal in the clause must be True). If the propagation leads to a contradiction, then it is not good. If neither option is good then the formula is not satisfiable. Again the running time is O(nm).
s(k) ≤ 1 + p · s(k − 1) + (1 − p) · s(k + 1)
where p = 1/2 or p = 1. Since s(k − 1) ≤ s(k + 1), if we use p = 1/2 and solve the equation as an equality, then we’ll obtain an upper bound for s(k). The boundary
conditions are s(0) = 0 (all in agreement) and s(n) = 1 + s(n − 1) (when all disagree, any change will produce one agreement). So, we solve the recurrence
s(k) = 1 +
· s(k − 1) +
· s(k + 1)
with those boundary conditions. We rewrite this equation
s(k + 1) − 2 s(k) + s(k − 1) = − 2.
The characteristic equation λ^2 − 2 λ + 1 = 0 has a double root +1. So the solution is of the form s(k) = Ak^2 + Bk + C.
To satisy the recurrence equation, we get A = −1, to satisfy the second boundary consition, we get B = 2n, and to satisfy the first boundary equation, we get C = 0. So the solution is s(k) = 2nk − k^2
which is maximum, as expected, for k = n: s(n) = n^2. So the expected number of steps is O(n^2 ).
What happens if we apply this approach to 3SAT? In a clause either 1, 2 or 3 variables are in disagreement. So the probability of decreasing the disagreement can be as small as 1/3. So, we get the recurrence equation
s(k) = 1 +
· s(k − 1) +
· s(k + 1),
or rewritten
s(k + 1) −
· s(k) +
· s(k − 1) = −
with the same boundary conditions as before. The characteristic equation λ^2 − (3/2)λ + 1/2 = 0 has roots +1, +1/2. So the solution is of the form
s(k) = A 2 −k^ + Bk + C.
To satisfy the recurrence equation, we get B = −3, to satisfy the second boundary condition we get A = − 2 n+2, and to satisfy the first boundary condition, we get C = −A = 2n+2. So, the solution is
s(k) = 2n+2^ − 2 n−k+2^ − 3 k
which is Ω(2n). Of course, this does not show for sure that the approach does not work, since, we used the worst case 1/3-2/3 for the probabilities, and we don’t know whether this can happen often enough. We would need to construct an example where the algorithm performs bad ... a candidate is the formula consisting of all the clauses (xi ∨ ¬xj ∨ ¬xk) for all triples of distinct i, j, k. The assignment with all variables true satisfies the formula. In the random walk, when “few” variables have the wrong assignemnt, the probability of hitting the right variable seems to be “close” to 1/3. Can you show that the random walk will indeed take an exponential number of steps in this case? In any case, we know from the fact that 3SAT is NP-complete that the expected time is unlikely to be polynomial.