34.NP Completeness: Click To Add Text Hsu, Lih-Hsing
34.NP Completeness: Click To Add Text Hsu, Lih-Hsing
NP Completeness
Click
Hsu,toLih-Hsing
add Text
Computer Theory Lab.
P and NP
Non-formal description
P: solvable polynomial time
NP:
Chapter 34 P.2
Computer Theory Lab.
NP-complete
NP-Complete: No polynomial-time
algorithm has yet been discovered for an
NP-complete problem, nor has anyone yet
been able to prove that no polynomial-time
algorithm can exist for any one of them.
Chapter 34 P.3
Computer Theory Lab.
Given a description of a program and a finite input, decide whether the program
Finishes running or will run forever.
Chapter 34 P.4
Computer Theory Lab.
Generally, we think of problems that are solvable by
polynomial-time algorithms are being tractable, and problems
that requires superpolynomial time are being intractable.
Polynomial Intractables
NP-Complete
Problem
Chapter 34 P.5
Computer Theory Lab.
Chapter 34 P.6
Computer Theory Lab.
Chapter 34 P.7
Computer Theory Lab.
Chapter 34 P.8
Computer Theory Lab.
Reductions
Suppose that there is a different decision problem,
say B, that we already know how to solve in
polynomial time. Finally, suppose that we have a
procedure that transforms any instance of A into
some instance of B with the following
characteristics:
1.The transformation takes polynomial time.
2.The answer are the same. That is, the answer for is
“yes” if and only if the answer for is also “yes.”
Chapter 34 P.9
Computer Theory Lab.
yes yes
Polynomial-time Polynomial-time
reduction algorithm Algorithm to decide B
no no
Polynomial-time algorithm to decide A
Chapter 34 P.10
Computer Theory Lab.
Chapter 34 P.11
Computer Theory Lab.
34.1 Polynomial time
Polynomial time solvable problem are regarded as tractable.
Even if the current best algorithm for a problem has a
running time of (n100), it is likely that an algorithm with
a much better running time will soon be discovered.
Problems for many reasonable models of computation,
can be solved in one model can be solved in polynomial
in another.
Polynomial-time solvable problems has a nice closure
property.
f , g are polynomial
f ( g) is also polynomial
Chapter 34 P.12
Computer Theory Lab.
Chapter 34 P.14
Computer Theory Lab.
Chapter 34 P.15
Computer Theory Lab.
Chapter 34 P.16
Computer Theory Lab.
Chapter 34 P.17
Computer Theory Lab.
Chapter 34 P.18
Computer Theory Lab.
Chapter 34 P.19
Computer Theory Lab.
A fo rm a l-la n g u a g e fra m e w o rk
A n a lp h a b e t is a fin ite s e t o f s y m b o ls .
A la n g u a g e L o v e r is a n y s e t o f s trin g s m a d e u p o f
s y m b o ls fro m .
e m p ty s tr in g : .
e m p ty la n g u a g e : .
*
L e t L1 , L 2 b e tw o la n g u a g e s . W e c a n d e fin e
Chapter 34 P.20
Computer Theory Lab.
L 1 L 2 (union)
L 1 L 2 (intersect ion)
L (complemen t)
L 1 L 2 { x 1 x 2 | x 1 L 1 and x 2 L 2 }
(concatena tion)
The closure (Kleen star) of L :
L * { } L L L ...
2 3
Chapter 34 P.21
Computer Theory Lab.
Chapter 34 P.22
Computer Theory Lab.
Chapter 34 P.23
Computer Theory Lab.
Chapter 34 P.24
Computer Theory Lab.
Example:
PATH PROBLEM:
PATH={<G,u,v,k> | G=(V,E) is an undirected graph,
u, v V , k 0 is an integer, and there is a path from u to v
whose length is at most k}.
Chapter 34 P.26
Computer Theory Lab.
HALTING PROBLEM:
There exists an accepting algorithm, but no decision
algorithm exists.
Chapter 34 P.27
Computer Theory Lab.
Chapter 34 P.28
Computer Theory Lab.
Chapter 34 P.29
Computer Theory Lab.
verification: polynomial
decision problem: ?
Chapter 34 P.30
Computer Theory Lab.
PATH PROBLEM:
PATH={<G,u,v,k> | G=(V,E) is an undirected graph,
u, v V , k 0 is an integer, and there is a path from u to v
whose length is at most k}.
Chapter 34 P.31
Computer Theory Lab.
naive algorithm:
input size: If we use the reasonable encoding of a graph as
its adjacency matrix, the number m of vertices is ( n ),
where n = |<G>| is the length of the encoding of G. There
are m! possible permutations of the vertices. Therefore the
running time is ( m !) ( n !) ( 2 n ) . This is not
a polynomial algorithm.
Chapter 34 P.32
Computer Theory Lab.
Verification algorithms:
A verification algorithm is a two-argument algorithm A,
where one argument is an ordinary input string x and the
other is a binary string y called a certificate. A
two-argument algorithm A verifies an input x if there exists
a certificate y such that A(x,y)=1. The language verified by
a verification algorithm A is
L { x { 0 ,1 } *| y { 0 ,1 } * s.t . A( x , y ) 1 } .
Chapter 34 P.33
Computer Theory Lab.
Problem:
1. P NP?
2. Complexity class co-NP
co NP { L | L NP } .
NP co NP?
3. Obviously P NP co NP .
P NP co NP ?
Chapter 34 P.35
Computer Theory Lab.
NP=co-NP
P=NP=co-NP
P
P
NP co-NP
P NP co NP
P
NP co-NP
P NP co NP
Chapter 34 P.36
Computer Theory Lab.
Chapter 34 P.37
Computer Theory Lab.
Reducibility:
ax b 0
ax 2 bx c 0
Chapter 34 P.38
Computer Theory Lab.
Chapter 34 P.39
Computer Theory Lab.
f
{ 0 ,1 }* { 0 ,1 }*
L1 L2
Chapter 34 P.40
Computer Theory Lab.
yes, f ( x) L2 yes, f ( x) L1
x f (x)
F A2
no, f ( x) L2
no, f ( x) L1
A1
Chapter 34 P.41
Computer Theory Lab.
NP-Completeness
A language L { 0 ,1 } * is NP-complete if
1. L NP , and
2. L' P L for every L' NP
Chapter 34 P.42
Computer Theory Lab.
Chapter 34 P.43
Computer Theory Lab.
NPC
NP
Chapter 34 P.44
Circuit satisfiability Computer Theory Lab.
x1 1
1
1
1
x2 1
0 1
1 1
1
0
1
1 1 Satisfiable
x3
0 1
1
x1
x2
Not Satisfiable!
x3
Chapter 34 P.45
Computer Theory Lab.
Chapter 34 P.46
Computer Theory Lab.
Chapter 34 P.47
Computer Theory Lab.
Input bits
0/1 output
Chapter 34 P.48
Computer Theory Lab.
Chapter 34 P.49
Computer Theory Lab.
Formula satisfiability:
An instance of SAT is a boolean formula composed of
1. boolean variables: x1 , x2 ,...
2. boolean connectives: any boolean function with one or
two input and one output
3. parentheses
Chapter 34 P.51
Computer Theory Lab.
Chapter 34 P.52
Computer Theory Lab.
x5 x8
x2
x6 x 10
x9
x3 x7
x4
Chapter 34 P.53
Computer Theory Lab.
x10 ( x4 x3 ) ( x5 ( x1 x2 ))
( x6 x4 ) ( x7 ( x1 x2 x4 ))
( x8 ( x5 x6 )) ( x9 ( x6 x7 ))
( x10 ( x7 x8 x9 ))
Chapter 34 P.54
Computer Theory Lab.
3-CNF satisfiability
literal
conjunction normal form (CNF)
3-conjunction normal form (3-CNF)
( x1 x1 x2 ) ( x3 x2 x4 )
(x1 x3 x4 )
Chapter 34 P.55
Computer Theory Lab.
Chapter 34 P.56
(( x1 x2 ) (( x1 x3 ) x4 ) x4 Computer Theory Lab.
y1
y2
x4
y3 y4
y5
x1 x2
y6
x4
y1 ( y1 ( y2 x4) ( y2 ( y3 y4)
( y3 (x1 x2) ( y 4 y5)
( y5 ( y6 x4) ( y6 ( x1 x 3)
Chapter 34 P.58
Computer Theory Lab.
1 y1 ( y2 x2 )
Truth Table
1 ( y1 y2 x2 ) ( y1 y2 x2 )
( y1 y2 x2 ) (y1 y2 x2 )
De Morgan rule
1 (y1 y2 x2 ) (y1 y2 x2 )
(y1 y2 x2 ) ( y1 y2 x2 )
Chapter 34 P.59
Computer Theory Lab.
| Ci| 3 Ci
| Ci | 2
| Ci| 1
Ci l ( l p q ) ( l p q )
( l p q ) ( l p q )
Chapter 34 P.60
Computer Theory Lab.
Chapter 34 P.61
Computer Theory Lab.
CIRCUIT-SAT
SAT
3CNF-SAT
Clique HAM-CYCLE
SUBSET-SUM
Chapter 34 P.62
Computer Theory Lab.
Chapter 34 P.63
Computer Theory Lab.
2 |V |
naïve algorithm: ( k )
k
Chapter 34 P.64
Computer Theory Lab.
Chapter 34 P.65
Computer Theory Lab.
C1 x1 x2 x3
x1 x2 x3
x1 x1
x2 x2
x3 x3
C2 x1 x2 x3 C3 x1 x2 x3
Chapter 34 P.66
Computer Theory Lab.
C1 C2 ... Ck
r s (1) rs
( vi , v j ) E r s
(2) li l j
clique size k
Chapter 34 P.67
Computer Theory Lab.
Chapter 34 P.68
Computer Theory Lab.
Chapter 34 P.69
Computer Theory Lab.
u v u v
z w z w
y x y x
Chapter 34 P.70
Computer Theory Lab.
kinds of wedges
Chapter 34 P.71
( x1 x2 x3 ) ( x1 x2 x3 ) ( x1 x2 x3 ) Computer Theory Lab.
b1 ,1
x 1'
b1 ,2
A e1
e1
B
x 1''
A
b1 ,3
b1 ,4
b2 ,1 A
A
x 2'
b2 ,2
e2
B A e2
b2 ,3
A x 2''
b2 ,4
A
b3 ,1
A
x 3'
b3 ,2 e3
e3
B
b3 ,3
A x 3''
b3 ,4
Chapter 34 P.72
Computer Theory Lab.
b1,3 x 3'
b1 ,3
b1,4
b1 ,4
A x 3'
e3
b3 ,3
A x 3''
b3,3
b3 ,4
b3,4 x 3''
Chapter 34 P.73
Computer Theory Lab.
u v
4
3 2
1
1
x w
Chapter 34
5 P.74
Computer Theory Lab.
Theorem 34.13
The hamiltonian cycle problem is NP-
complete.
Chapter 34 P.75
Computer Theory Lab.
Proof.
First, show that HAM-CYCLE belongs to NP.
We now prove that VERTEX-COVER p HAM-
CYCLE, which shows that HAM-CYCLE is NP-
complete.
Given an undirected graph G=(V,E) and an
integer k, we construct an undirected graph
G’=(V’,E’) that has a hamiltonian cycle iff G
has a vertex cover of size k.
Chapter 34 P.76
Computer Theory Lab.
widget
[u,v,2] [v,u,2]
[u,v,3] [v,u,3]
Wuv Wuv Wuv Wuv
[u,v,4] [v,u,4]
[u,v,5] [v,u,5]
Chapter 34 P.77
The reduction of an instance of Computer Theory Lab.
(a)
z y
(b)
Chapter 34 P.80
Computer Theory Lab.
Chapter 34 P.81
Computer Theory Lab.
Chapter 34 P.82
Computer Theory Lab.
Chapter 34 P.83
Computer Theory Lab.
Chapter 34 P.84
Computer Theory Lab.
Theorem 34.15
The subset-sum problem is NP-
complete.
Chapter 34 P.85
Computer Theory Lab.
Proof.
First, show that SUBSET-SUM is in NP.
We now show that 3-CNF-SAT p SUBSET-
SUM.
Given a 3-CNF formula over variables x1,
x2,…, xn with clauses C1, C2,…, Ck, each
containing exactly three distinct literals.
The reduction algorithm constructs an
instance <S,t> of the subset-sum problem
such that is satisfiable iff there is a subset
of S whose sum is exactly t.
Chapter 34 P.86
Computer Theory Lab.
Example
The formula in 3-CNF is = C1 C2
C3 C4, where C1 = (x1 ¬x2 ¬x3), C2
= (¬x1 ¬x2 ¬x3), C3 = (¬x1 ¬x2
x3), and C4 = (x1 x2 x3).
A satisfying assignment of is <x1 = 0,
x2 = 0, x3 = 1>.
Chapter 34 P.87
Computer Theory Lab.
Chapter 34 P.89
Computer Theory Lab.
Chapter 34 P.90