Npcomp 6
Npcomp 6
1.1 Encodings
An encoding is a mapping from abstract objects to character strings over
some finite alphabet.
A concrete problem is a problem whose instances are sets of binary
strings.
An algorithm solves a concrete problem in time O(T (n)) if, given an
input of length n, produces the (encoding of the) solution in at most O(T (n))
time.
1
1.4 Polynomial time verification
Checking (verifying) versus solving
1.7 NP-Completeness
NP-complete problems: If any one of them has a polynomial time solution
then all do, and P = NP. But no one has found such a solution, nor has anyone
proven that it does not exist. Many problems of practical importance are
NP-complete.
These are in a sense the hardest problems in NP.
2
1.8 Examples
• Graph isomorphism is in NP (but maybe not NP complete); a verifica-
tion is an isomorphism
1.9 Reducibility
A language L1 is polynomial-time reducible to L2 (L1 ≤P L2 ) if there is
a polynomial time computable function f from languages to languages such
that for all x, x ∈ L1 iff f (x) ∈ L2 .
If L1 is polynomial time reducible to L2 , then an algorithm to solve L2
can be used to obtain an algorithm to solve L1 . Also, the time required will
be the same to within a polynomial. That is, L1 cannot be much harder than
L2 .
Lemma 36.3 If L1 and L2 are languages such that L1 ≤P L2 , then
L2 ∈ P implies L1 ∈ P .
Proof.
1.10 NP-completeness
A language L is NP-compete if
1. L ∈ NP , and
2. L′ ≤P L for every L′ ∈ NP
3
Theorem 36.4 If any NP complete problem is in P, then P = NP. If any
NP complete problem is not in P, then no NP complete problem is in P, and
P 6= NP.
Thus the whole P = NP question boils down to the complexity of a single
NP-complete problem (any one of them).
4
1.12 Showing Other Problems NP Complete
Once we have one NP-complete problem we can obtain more using the fol-
lowing lemma:
Lemma 36.8 If L′ is NP-complete and L′ ≤P L, then L is NP-hard. If
L is also in NP, then L is NP-complete.
Proof.
Five-step recipe for showing NP-completeness of L:
1. Prove L ∈ NP
2. Select NP-complete L′
5
• The clique problem
• Graph coloring
6
OR
/ \
AND AND
| \/ |
| /\ |
OR OR
| \/ |
| /\ |
AND AND
| \/ |
| /\ |
OR OR
| \/ |
| /\ |
AND AND
| \/ |
| /\ |
...
Z5|
NOT
Z4|
|
OR .
. . .
Z1 . Z2. .Z3
. . .
AND AND AND
/ \ / \ / \
/ \ / \ / \
X1 X2 X3 X4 X5 X6
7
Then we create formulas relating the values of the variables, as follows:
Z1 ≡ AND(X1, X2)
Z2 ≡ AND(X3, X4)
Z3 ≡ AND(X5, X6)
Z4 ≡ OR(Z1, Z2, Z3)
Z5 ≡ NOT (Z4)
We then concatenate them all together, with Z5, as follows:
(Z1 ≡ AND(X1, X2))∧(Z2 ≡ AND(X3, X4))∧(Z3 ≡ AND(X5, X6))∧
(Z4 ≡ OR(Z1, Z2, Z3)) ∧ (Z5 ≡ NOT (Z4)) ∧ Z5
Note that Z5 is added on, too. In this way, a circuit C is mapped on to
a formula F . This is the reduction. We need to show it is polynomial time
(easy), and preserves solvability. (Also not difficult.)
We now show that 3 CNF is NP complete by reducing from formula
satisfiability. An example of a formula in 3 CNF is the following:
We illustrate these steps on the formula obtained above: (Z1 ≡ AND(X1, X2))∧
(Z2 ≡ AND(X3, X4)) ∧ (Z3 ≡ AND(X5, X6)) ∧ (Z4 ≡ OR(Z1, Z2, Z3)) ∧
(Z5 ≡ NOT (Z4)) ∧ Z5
First we make all connectives binary:
(Z1 ≡ AND(X1, X2)) ∧ (Z2 ≡ AND(X3, X4)) ∧ (Z3 ≡ AND(X5, X6))
∧
(Z4 ≡ OR(Z1, OR(Z2, Z3))) ∧ (Z5 ≡ NOT (Z4)) ∧ Z5
Then we add new variables for subformulas so that the number of vari-
ables in each conjunct is 3 or less. For our formula that is already true for
each conjunct except one, so we obtain this formula:
8
(Z1 ≡ AND(X1, X2))∧(Z2 ≡ AND(X3, X4))∧(Z3 ≡ AND(X5, X6))∧
(Z4 ≡ OR(Z1, Y 1)) ∧ (Y 1 ≡ OR(Z2, Z3)) ∧ (Z5 ≡ NOT (Z4)) ∧ Z5
Next we put each subformula in conjunctive normal form, obtaining:
(Z1 ⊃ AND(X1, X2))∧(Z1 ⊂ AND(X1, X2))∧(Z2 ⊃ AND(X3, X4))∧
(Z2 ⊂ AND(X3, X4)) ∧ (Z3 ⊃ AND(X5, X6)) ∧ (Z3 ⊂ AND(X5, X6)) ∧
(Z4 ⊃ OR(Z1, Y 1)) ∧ (Z4 ⊂ OR(Z1, Y 1)) ∧ (Y 1 ⊃ OR(Z2, Z3)) ∧ (Y 1 ⊂
OR(Z2, Z3)) ∧ (Z5 ⊃ NOT (Z4)) ∧ (Z5 ⊂ NOT (Z4)) ∧ Z5
Continuing, we obtain:
(¬Z1 ∨ X1) ∧ (¬Z1 ∨ X2) ∧ (¬X1 ∨ ¬X2 ∨ Z1)∧ (¬Z2 ∨ X3) ∧ (¬Z2 ∨
X4) ∧ (¬X3 ∨ ¬X4 ∨ Z2)∧ . . . ∧Z5
The problem now is that some of the disjunctions have too few variables.
So we add extra variables to them; for example, we replace (¬Z1 ∨ X1) by
(¬Z1 ∨ X1 ∨ W ) ∧ (¬Z1 ∨ X1 ∨ ¬W ) for a new variable W . This has to be
done twice if we start with just one variable.
After all these steps, we obtain a 3 CNF formula that is satisfiable iff the
original formula was satisfiable. Also, the transformation is polynomial time.
Proof that the clique problem is NP complete:
Reduce from 3 CNF; suppose there are k clauses C1 , . . . , Ck . Create a
graph G for the clique problem, as follows:
• Edges between vertices in different clauses if the literals are not com-
plementary
G: a---b
|\ /|\
| | \
|/ \| e
c---d
9
G has a clique of size 4
G complement:
a b
’ .
’ e
. ’.’
c d
e1
v1--v2
| |
e2| |e3
v3--v4
e4
e4 e3 e2 e1 Value
v1: 1 0 0 1 1 4^4 + 4^1 + 4^0
v2: 1 0 1 0 1 4^4 + 4^2 + 4^0
10
v3: 1 1 0 1 0 4^4 + 4^3 + 4^1
v4: 1 1 1 0 0 4^4 + 4^3 + 4^2
e1: 0 0 0 0 1 4^0
e2: 0 0 0 1 0 4^1
e3: 0 0 1 0 0 4^2
e4: 0 1 0 0 0 4^3
---------------------
(2) 2 2 2 2 2*4^4 + 2*4^3
+ 2*4^2 + 2*4^1
+ 2*4^0
11
written. It has a finite set of states, and can read, write, and move to the
left or right, while changing state. It can also be nondeterministic.
The class NP is defined as the class of problems that can be accepted by
nondeterministic Turing machines in polynomial time. That is, there is some
sequence of transitions that leads to an accepting state. One can show that
this is equivalent to the verifier definition given in the text.
12
For this we can obtain a ratio of two to an optimal solution in polynomial
time by a very simple algorithm.
APPROX-VERTEX-COVER(G)
C <- 0
E’ <- E[G]
while E’ .ne. 0
do let (u,v) be an arbitrary edge of E’
C’ <- C U {u,v}
remove from E’ every edge incident
on either u or v
return C
Example.
Proof of approximation.
The Traveling Salesman Problem
We obtain a ratio of two to an optimal solution in polynomial time as-
suming the triangle inequality:
c(u, w) ≤ c(u, v) + c(v, w).
The approximation algorithm makes use of minimum spanning trees.
APPROX-TSP-TOUR(G,c)
select a vertex r in V[G] as a root
grow a MST T for G from r using
Prim’s algorithm
Let L be the list of vertices of T
visited in preorder
return the Hamiltonian cycle that
visits the vertices in the order L
Example and proof of approximation.
Without the triangle inequality, no polynomial time approximation algo-
rithm is possible unless P = NP:
The idea is to take a graph G and produce another graph G′ in which the
costs of the edges in G are 1 but the costs of edges not in G are very large.
Then an approximation algorithm for the TSP G′ can be used to test if G
has a Hamiltonian circuit.
Example.
13
The Set Covering Problem
GREEDY-SET-COVER(X,F)
U <- X
C <- 0
while U .ne. 0
do select an S in F that
maximizes |S /\ U|
U <- U - S
C <- C U {S}
return(C)
EXACT-SUBSET-SUM(S,t)
n <- |S|
L_0 <- <0>
for i <- 1 to n
do L_i <-
MERGE-LISTS(L_(i-1),L_(i-1)+x_i)
14
remove from L_i all elements
larger than t
return the largest element in L_n
APPROX-SUBSET-SUM(S,t,epsilon)
n <- |S|
L_0 <- <0>
for i <- 1 to n
do L_i <-
MERGE-LISTS(L_(i-1),L_(i-1)+x_i)
L_i <- Trim(L_i,epsilon/n)
remove from L_i all elements
larger than t
return the largest element in L_n
TRIM(L, delta)
m <- |L|
L’ <- <y_1>
last <- y_1
for i <- 2 to m
do if last < (1 - delta)y_i
then append y_i onto
the end of L’
last <- y_i
return L’
Example.
Proof of approximation.
15