Fall 2011
The Chinese University of Hong Kong
CSCI 3130: Formal languages and automata theory
The Cook-Levin Theorem
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
NP-completeness
IS
VC
CLIQUE
3SAT
SAT
NP
(easy to verify solution)
P
NP-complete
(easy to solve)
Cook-Levin theorem:
Everything in NP
reduces to SAT
Polynomial-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’ (IS)
L (CLIQUE)
x (G, k)
R
y (G’, k’)
x∈L
y ∈ L’
(G has clique of size k)
(G’ has IS of size k’)
SAT and 3SAT
SAT = {〈f〉: f is a satisfiable Boolean formula}
((x1∨x2 ) ∧ (x1∨x2)) ∨ ((x1∨(x2 ∧ x3)) ∧ x3)
3SAT = {〈f〉: f is a satisfiable 3CNF formula}
(x1∨x2∨x2 ) ∧ (x2∨x3∨x4) ∧ (x2∨x3∨x5) ∧ (x1∨x5∨x5)
The Cook-Levin Theorem
Every L ∈ NP reduces to SAT
• To show this, we need a reduction R such that:
z
z∈L
R
Boolean formula f
f is satisfiable
NP-completeness of SAT
• All we know: L has a polynomial-time verifier V
– Look at the computation tableau of V on input 〈z, s〉
V
z|s
S
q0 z1 z2
…
S-th configuration
symbol at time T
T
a qacc b …
NP-completeness of SAT
S
q0 z1 z2
n = length of z
…
height of tableau is O(nc)
for some constant c
T
u
width is at most nc
k possible tableau symbols
xT, S, u =
a qacc b …
true, if the (T, S) cell of
tableau contains u
false, if not
1 ≤ S ≤ nc
1 ≤ T ≤ nc
1≤u≤k
NP-completeness of SAT
z
z∈L
R
Boolean formula f
f is satisfiable
• 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
satisfying assignment
accepting computation tableau
f is satisfiable
V accepts 〈z, s〉 for some s
NP-completeness of SAT
xT, S, u =
true, if the (T, S) cell of
tableau contains u
false, if not
1 ≤ S ≤ nc
1 ≤ T ≤ nc
1≤u≤k
• We want to construct (in time O(n2c)) a formula f :
f(x1, 1, 1, ..., xnc, nc, k) =
true, if the xs represent a legal
and accepting tableau
false, if not
NP-completeness of SAT
f = fcell ∧ f0 ∧ fmove ∧ facc
fcell:
“Every cell contains
exactly one symbol”
f0:
“The first row is
q0z|s☐...☐” for some s
q0 z1 z2
…
a qacc b …
fmove: “The moves between rows
follow the transitions of V”
facc: “qacc appears somewhere in the last row”
NP-completeness of SAT
fcell:
“Every cell contains exactly one symbol”
“Exactly one of xS, T, 1 ∨ ... ∨ xS, T, k is true”
fcell = fcell 1, 1 ∧ ... ∧ fcell nc, nc
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)
where
at least one symbol
no cell contains
two or more symbols
NP-completeness of SAT
f0:
“The first row is q0z1z2...zn|s1s2...sm
for some (possible solution) s1s2...sm”
f0 = x1, 1, q0 ∧ x1, 2, z1 ∧ ... ∧ x1, n+1, zn ∧ x1, n+2, |
NP-completeness of SAT
facc: “qacc appears somewhere in the last row”
facc = xnc, 1, qacc ∨ xnc, 2, qacc ∨ ... ∨ xnc, nc, qacc
Legal and illegal windows
legal windows
illegal windows
… 6a3b0x0 …
… 0a6b0x0 …0
… 6q2a0b0 …
… 0a6b0q2 …0
… 6a3q2a0 …
… 0q5a6x0 …0
q2
… 6q2q2a0 …
… 0q2q2x3 …0
a/xL
… 6#3b0a0 …
… 0#6b0q5 …0
… 6a3a0☐0 …
… 0x6a0☐0
…0
q5
… 6a3q2a0 …
… 0q5a6b0 …0
… 6#3q2a0 …
… 0q5#6x0 …0
NP-completeness of SAT
fmove: “The moves between rows
follow transitions of V”
LW = list of all legal windows
= {(a1, a2, a3, a4, a5, a6):
a1 a2 a3
a4 a5 a6
…
q0 z1 z2
a1 a2 a3
a4 a5 a6
fmove 2, 2
is legal}
a qacc b
…
fmove = fmove 1, 1 ∧ ... ∧ fmove nc-3, nc-3
∨
fmove T, S =
(xT, S, a1 ∧ xT, S+1, a2 ∧ xT, S+2, a3
∧ xT+1, S, a4 ∧ xT+1, S+1, a5 ∧ xT+2, S+1, a6)
(a , a , a , a , a , a ) ∈ LW
1
2
3
4
5
6
NP-completeness of SAT
z
R
Boolean formula f
z∈L
✔
f is satisfiable
Let V be a poly-time verifier TM for L.
R := On input z,
Construct the formulas fcell, f0, fmove, and facc
Output the formula f = fcell ∧ f0 ∧ fmove ∧ facc.
Then R is computable in time O(n2c) and
V accepts z if and only if f is satisfiable.
How to argue NP-completeness
SUBGRAPH = {〈G, H〉: G and H are graphs where
H is a subgraph of G}
6
Every edge of H must be
included in G
7
4
5
1
2
4
3
G
SUBGRAPH is NP-complete
3
1
H
2
How to argue NP-completeness
• Step 1: SUBGRAPH is in NP ✔
What is a solution for SUBGRAPH?
6
7
4
5
1
2
4
3
G
3
1
H
A solution s is a map from vertices of H to vertices of G
(1➝4)(2➝2)(3➝5)(4➝7)
V := On input 〈G, H, s〉
For every edge {u, v} of H:
Running time = O(m)
m = number edges in H
Find u’ and v’ such that u➝u’ and v➝v’ in s.
If {u’, v’} is not an edge in G, reject.
Otherwise, accept.
2
How to argue NP-completeness
• Step 2: Some NP-complete L reduces to
SUBGRAPH
Direction of reduction is important!
You want to show SUBGRAPH
is harder than L
VERTEX COVER
So you assume you can solve
SUBGRAPH, and show how
to solve L
IND SET
This requires converting instances
of L into instances of SUBGRAPH
SAT
CLIQUE
3SAT
How to choose L?
Tips on choosing L
6
4
G
CLIQUE
7
4
5
1
2
4
3
3
1
G
H
SUBGRAPH
2
• Both CLIQUE and SUBGRAPH are about graph
containment, so their complexities may be related
• It usually makes sense to try and reduce from the
problem that looks the most related
How to argue NP-completeness
• Step 3: Reduce CLIQUE to SUBGRAPH ✔
CLIQUE = {〈G, k〉: G has a k-clique}
SUBGRAPH = {〈G, H〉: H is a subgraph of G}
R := On input 〈G, k〉
Construct the k-clique H
Output 〈G, H〉
G has a k-clique
H is a subgraph of G
Cover for triangles
• A graph has a k-cover for triangles if there are
k vertices that touch all the triangles
Does it have a 2-cover for triangles? Yes
Does it have a 1-cover for triangles?
No, because it has two vertex-disjoint triangles
TRICOVER = {〈G, k〉: G has a k-cover for triangles}
TRICOVER is NP-complete
Cover for triangles
6
• Step 1: TRICOVER is in NP ✔
7
5
4
What is a solution for TRICOVER?
1
A solution s is a subset of vertices like {2,5}
2
3
V := On input 〈G, k, s〉, where s is a set of vertices of size k
For every triple {u, v, w} of vertices:
If {u, v}, {u, w}, {v, w}, are all edges in G:
If none of u, v, or w are in s, reject.
Otherwise, accept.
Running time = O(n3)
Cover for triangles
• Step 2: Some NP-complete L reduces to TRICOVER
SUBGRAPH
TRICOVER = {〈G, k〉: G has a
k-cover for triangles}
VERTEX COVER Some vertex in every triangle is covered
IND SET
SAT
3SAT
CLIQUE
VC = {〈G, k〉: G has a k-vertex cover}
Some endpoint of every edge is covered
Cover for triangles
• Step 3: Reduce VC to TRICOVER
VC = {〈G, k〉: G has a k-vertex cover (for edges)}
TRICOVER = {〈G’, k’〉: G’ has k’-cover for triangles}
Idea: Replace edges by triangles
G
vertex-cover in G
G’
cover for triangles in G’
Cover for triangles
• Step 3: Reduce VC to TRICOVER
R := On input 〈G, k〉, where G has n vertices and m edges
Construct the following graph G’:
G’ has n + m vertices v1, ..., vn,
and uij for every edge {vi, vj} of G
For every edge {vi, vj} of G,
include the edges {vi, vj}, {vi, uij}, {vj, uij} in G’
Output 〈G’, k〉.
Running time = O(m)
m = number of edges in G
Cover for triangles
• Step 4: Argue that the reduction is correct
〈G’, k〉 ∈ TRICOVER
〈G, k〉 ∈ VC
G
G has a k-vertex cover C
✔
G’
C is a k-triangle cover of G’
Old triangles from G are covered
New triangles in G’ also covered
Cover for triangles
• Step 4: Argue that the reduction is correct
✔
〈G, k〉 ∈ VC
G
✔
〈G’, k〉 ∈ TRICOVER
G’
G has a k-vertex cover C’
G’ has a k-vertex cover C
C’ is obtained after moving some
vertices of C
Some vertices in C may not
come from G!
Since C’ covers all triangles in G’,
it must cover all edges in G
But we can move them and still
cover the same triangle
Coloring
Suppose you need to schedule
exams on same day in 3 time slots
Y
R
1030-1230 130-330
CSCI 3230
CSCI 3190
CSCI 2110
B
430-630
CSCI 3130
Some people are enrolled in
multiple classes, so there may be conflicts
CSCI 3160
Task: Schedule the exams so there are no conflicts
Coloring
Given a graph, we want to color
the vertices so every edge has
endpoints of different colors
Y
R
G
B
This is called a valid 3-coloring
3COL = {〈G〉: G has a valid 3-coloring}
3COL is NP-complete
Coloring
• Step 1: 3COL is in NP
✔
5
4
3
G
What is a solution for 3COL?
A solution s is a coloring of vertices like
{1B,2R,3Y,4R,5B}
1
2
V := On input 〈G, s〉
Running time = O(m)
For every edge {u, v} of G:
If u and v are assigned same color in s, reject.
Otherwise, accept.
Coloring
• Step 2: Some NP-complete L reduces to 3COL
3COL = {〈G〉: G has a valid 3-coloring}
SUBGRAPH
VERTEX COVER
IND SET CLIQUE
SAT
3SAT
3COL does not look like
any of these problems
In such a case it is a good
idea to start with 3SAT
Coloring
• Step 3: Reduce 3SAT to 3COL
3SAT = {〈f〉: f is a 3CNF that has a satisfying assignment}
3COL = {〈G〉: G has a valid 3-coloring}
Idea: Assignment of f
Coloring of G
X
T
G
F
Part I: 3 special vertices T (true), F (false), and X
Coloring
Either xi has color of T
and xi has color of F
X
xi
xi
Or xi has color of F
and xi has color of T
Part 2: For each variable xi
T
x1
F
x1
F
T
x2
F
x2
x3
T
X
T
Example:
F
(x1∨x1∨x2)∧(x1∨x2∨x3 )
x3
Coloring
• To “encode” the clauses of f, we need a gadget
x
y
z
If x or y is colored T, z can be colored T
If x and y are colored F, z must be colored F
x1
x2
T
Can be colored if and only if
at least one literal is colored T
x3
Part 3: For each clause like (x1∨x2∨x3 )
Coloring reduction: An example
x1
x1
x2
x2
x3
X
T
Example:
F
(x1∨x1∨x2)∧(x1∨x2∨x3 )
T F
F
T
F
T
x3
Coloring reduction: An example
x1
x1
x2
x2
x3
X
T
Example:
F
(x1∨x1∨x2)∧(x1∨x2∨x3 )
T F
F
T
F
T
x3
Coloring
• Step 3: Reduce 3SAT to 3COL
R := On input 〈f〉, where f is a 3CNF:
Running time = O(m + n)
n = number of variables
m = number of clauses
Construct the following graph G:
Add 3 special vertices T, F, X connected by a triangle.
For every variable xi of f, add vertices xi and xi
and include the triangle xi, xi, X.
For every clause u∨v∨w of f, connect u, v, w, and T
using the gadget below.
u
Output 〈G〉.
v
w
T
Coloring
• Step 4: Argue that the reduction is correct
〈f〉 ∈ 3SAT
(x1∨x1∨x2)∧(x1∨x2∨x3 )
〈G〉 ∈ 3COL
✔
x1
x1
x2
x2
x3
x3
X
T
f has a SAT assignment a
F
G has a valid 3-coloring
Each literal gets “color” as in a
Each clause gadget contains a
true literal, so it can be colored
Coloring
• Step 4: Argue that the reduction is correct
〈f〉 ∈ 3SAT
(x1∨x1∨x2)∧(x1∨x2∨x3 )
〈G〉 ∈ 3COL
✔
x1
f has a SAT assignment
x2
x2
x3
X
x3
G has a valid 3-coloring c
Each variable gets value as in G
xi =
x1
true, if node xi has same color as T
false, if node xi has same color as F
Since each clause gadget is colored properly,
each clause must contain a true literal
T
F
© Copyright 2026 Paperzz