Title: CSC 3130: Automata theory and formal languages
1Fall 2009
The Chinese University of Hong Kong
CSC 3130 Automata theory and formal languages
NP-complete problems
Andrej Bogdanov http//www.cse.cuhk.edu.hk/andrej
b/csc3130
2Polynomial-time reductions
- Language L polynomial-time reduces to L if
there exists a polynomial-time computable map R
that takes an instance x of L into instance y of
L s.t.
x ? L if and only if y ? L
L
L
(CLIQUE)
(IS)
R
x
y
(G, k)
(G, k)
x ? L
y ? L
(G has clique of size k)
(G has IS of size k)
3The Cook-Levin Theorem
Every L ? NP reduces to SAT
SAT f f is a satisfiable Boolean formula
(x1?x2 ) ? (x2 ?x3 ?x4) ? (x1)
f
(x1?x2 ) ? (x1) ? (x2)
f
is satisfiable
is not satisfiable
x1 T x2 F x3 T x4 T
4NP-hardness
- Language L is NP-hard if every L in NP reduces
to L - Intuitively, NP-hard means harder than all of
NP - The Cook-Levin Theorem says
SAT
NP
SAT is NP-hard
P
5NP-complete
- L is NP-complete if L is NP-hard and L ? NP
- Intuitively, NP-complete means hardest in NP
- Recall that SAT ? NP, so SAT is NP-complete
SAT
NP
If SAT ? P, then P NP
P
6Proof of Cook-Levin Theorem
Every L ? NP reduces to SAT
- To prove it, we have to describe a reduction R
R
w
Boolean formula f
f is satisfiable
w ? L
7Proof of Cook-Levin Theorem
- All we know about L It has a poly-time NTM M
- Lets look at computation tableau of M on input w
S
w
M
q0
w1
S-th configuration symbol at time T
T
Since M is nondeterministic, there may be many
possible tableaus
qacc
8Proof of Cook-Levin Theorem
S
q0
n length of input w
w1
height of tableau is p(n) for some polynomial p
T
u
width is at most p(n)
k possible tableau symbols
qacc
true,
if the (T, S) cell of tableau contains u
1 S p(n)
xT, S, u
1 T p(n)
if not
false,
1 u k
9Proof of Cook-Levin Theorem
R
w
Boolean formula f
f is satisfiable
w ? L
- We will design a formula f such that
variables of f
xT, S, u
assignment to xT, S, u
way to fill up the tableau
accepting computation tableau
satisfying assignment
f is satisfiable
M accepts w
10Proof of Cook-Levin Theorem
true,
if the (T, S) cell of tableau contains u
1 S p(n)
xT, S, u
1 T p(n)
if not
false,
1 u k
- We want to construct (in time poly(n)) a formula
f
true,
if the xs represent a valid accepting tableau
f(x1, 1, 1, ..., xp(n), p(n), k)
if not
false,
Symbols in computation tableau come from some
alphabet a1,...,ak
11Proof of Cook-Levin Theorem
S
f fcell ? f0 ? fmove ? facc
q0
w1
T
fcell
Every cell contains exactly one symbol
u
The first row is q0w1w2...wk?...?
f0
qacc
fmove
The moves between rows follow the transitions
of M
facc
qacc appears somewhere in the last row
12Proof of Cook-Levin Theorem
- Desired meaning
- Implementation
fcell
Every cell contains exactly one symbol
or Exactly one of xS, T, 1 ? ... ? xS, T, k is
true
fcell fcell 1, 1 ? ... ? fcell p(n), p(n)
where
fcell T, S (xT, S, 1 ? ... ? xT, S, k)
? (xT, S, 1 ? xT, S, 2)
? (xT, S, 1 ? xT, S, 3) ?
... ? (xT, S, k-1 ? xT, S,
k)
at least one symbol
no two symbols
13Proof of Cook-Levin Theorem
- Desired meaning
- Implementation
f0
The first row is q0w1w2...wk?...?
facc
qacc appears somewhere in the last row
f0
x1, 1, ? x1, 1, q0 ? x1, 1, w1 ? ... ? x1,
p(n),
facc
xp(n), 1, qacc ? xp(n), 2, qacc ? ... ? xp(n),
p(n), qacc
14Valid and invalid windows
valid windows
invalid windows
6a3b0x0 0a6b0x0 0
6q2a0b0 0a6b0q2 0
6a3q2a0 0q5a6x0 0
6q2q2a0 0q2q2x3 0
q2
a/xL
63b0a0 06b0q5 0
6a3q2a0 0q5a6b0 0
q5
6a3a0?0 0x6a0?0 0
6a3q2a0 0a6q5x0 0
15Proof of Cook-Levin Theorem
- Desired meaning
- Implementation
q0
a
b
q3
b
a
fmove
The moves between rows follow transitions of
M
q7
c
b
fmove 2, 2
qacc
fmove fmove 1, 1 ? ... ? fmove p(n)-3, p(n)-3
?
fmove T, S
(xT, S, a1 ? xT, S1, a2 ? xT, S2, a3 ?
xT1, S, a4 ? xT1, S1, a5 ? xT2, S1, a6)
over all
valid windows
a1
a2
a3
a4
a5
a6
16Other NP-complete problems
CLIQUE (G, k) G is a graph with a clique of k
vertices
IS (G, k) G is a graph with an independent
set of k vertices
VC (G, k) G is a graph with a vertex cover of
k vertices
CLIQUE, IS and VC are NP-complete
CLIQUE
IS
VC
SAT
NP
17Proving NP-hardness
- To show L is NP-hard, it is enough to reduce from
some L we already know is NP-hard - For now we can take L SAT
- To show L is NP-complete, we also need to argue
that L is in NP - This is usually the easy part
roadmap
VC
IS
CLIQUE
3SAT
SAT
183SAT
SAT f f is a satisfiable Boolean formula
3SAT f f is a satisfiable Boolean formula in
conjunctive normal form with
3 literals per clause
(x2?(x1?x2 ))?(x1?(x1?x2 ))
literal xi or xi
gates
CNF AND of ORs of literals
(x1?x2?x2 ) ? (x2?x3?x4)
(conjunctive normal form)
clause
literals
3CNF CNF with 3 literals per clause
(repetitions are allowed)
19NP-hardness of 3SAT
- Theorem
- Proof We describe a reduction R from SAT
3SAT is NP-hard
R
Boolean formula f
3CNF formula f
f is satisfiable
f is satisfiable
20Reducing SAT to 3SAT
(x2?(x1?x2 ))?(x1?(x1?x2 ))
x7 x4 ? x5
x4 x5 x7
T T T TT T F FT F T FT F F
TF T T FF T F TF F T FF
F F T
We give extra variables to every gate (wire)
21Turning gates into 3CNFs
z
z
z
Gj
AND
OR
NOT
x
y
x
y
x
z y ? x
x
y
z y ? x
x
y
z x
x
z
z
z
T T T TT T F FT F T FT F F TF T T
FF T F TF F T FF F F T
T T T TT T F FT F T TT F F FF T T
TF T F FF F T FF F F T
T T FT F T F T TF F F
(x?z)?(x?z)
(x?y?z)?(x?y?z) ?(x?y?z)?(x?y?z)
(x?y?z)?(x?y?z) ?(x?y?z)?(x?y?z)
fj
(x?x?z)?(x?x?z)
22Reducing SAT to 3SAT
R
Boolean formula f
3CNF formula f
On input f, where f is a boolean formula
R
Construct and output the following 3CNF formula
f
Add variable xnj for each gate Gj in f Write
3CNF fj for each gate Gj, j 1, ..., t Let
f fn1 ? fn2 ? ... ? ft ? (xnt ?xnt ?xnt )
23Reducing SAT to 3SAT
R
Boolean formula f
3CNF formula f
f is satisfiable
f is satisfiable
- Every satisfying assignment of f extends uniquely
to a satisfying assignment of f - Conversely, every satisfying assignment of f
must contain a satisfying assignment of f
24Clique
CLIQUE (G, k) G is a graph with a clique of k
vertices
CLIQUE is NP-hard
VC
IS
A clique is a subset of vertices so that all
pairs are connected
2
1
CLIQUE
3SAT
1, 2, 3, 1, 4, 4 are cliques
?
SAT
3
4
25Reducing 3SAT to CLIQUE
- Proof We give a reduction from 3SAT to CLIQUE
3SAT f f is a satisfiable Boolean formula in
3CNF
CLIQUE (G, k) G is a graph with a clique of k
vertices
R
3CNF formula f
(G, k)
G has a cliqueof size k
f is satisfiable
26Reducing 3SAT to CLIQUE
(x1?x1?x2 ) ? (x1?x2?x2) ? (x1?x2?x3)
Put a vertex for every literal
Put an edge for every consistent pair
27Reducing 3SAT to CLIQUE
R
3CNF formula f
(G, k)
On input f, where f is a 3CNF formula with m
clauses
R
Construct the following graph G
G has 3m vertices, divided into m groups,
one for each literal in f
If a and b are in different groups and a ? b,
put an edge (a, b)
Output (G, m)
28Reducing 3SAT to CLIQUE
R
3CNF formula f
(G, m)
G has a cliqueof size m
f is satisfiable
x1
x1
x1
x1
x2
x2
x2
x2
x3
f (x1?x1?x2 ) ? (x1?x2?x2) ? (x1?x2?x3)
T
T
F
F
F
T
F
F
T
29Reducing 3SAT to CLIQUE
R
3CNF formula f
(G, m)
G has a cliqueof size m
f is satisfiable
x1
x1
x1
x1
x2
x2
x2
x2
x3
f (x1?x1?x2 ) ? (x1?x2?x2) ? (x1?x2?x3)
F
T
T
F
F
T
T
T
F
30Reducing 3SAT to CLIQUE
R
3CNF formula f
(G, m)
G has a clique of size m
f is satisfiable
- Every satisfying assignment of f gives a clique
of size m in G - Conversely, every clique of size m in G gives a
consistent satisfying assignment of f.
?
?
31Vertex cover
VC (G, k) G is a graph with a vertex cover of
size k
VC is NP-hard
VC
IS
?
A vertex cover is a set of vertices that touches
(covers) all edges
CLIQUE
?
3SAT
?
2, 4, 3, 4, 1, 2, 3 are vertex covers
SAT
32Reducing CLIQUE to VC
- Proof We describe a reduction from IS to VC
- Example
R
(G, k)
(G, k)
G has a VC of size k
G has an IS of size k
vertex covers
independent sets
2, 4, 3, 4, 1, 2, 3, 1, 2, 4, 1, 3, 4,
2, 3, 4, 1, 2, 3, 4
Ø, 1, 2, 3, 4, 1, 2, 1, 3
33Reducing IS to VC
S is an independent set of G if and only if S is
a vertex cover of G
34Reducing IS to VC
R
(G, k)
(G, k)
?
35The ubiquity of NP-complete problems
- We saw a few examples of NP-complete problems,
but there are many more - A surprising fact of life is that most CS
problems are either in P or NP-complete - A 1979 book by Garey and Johnsonlists 100
NP-complete problems