CSCI 3130: Formal languages and automata theory Andrej Bogdanov http://www.cse.cuhk.edu.hk/ ~andrejb/csc3130 The Chinese University of Hong Kong More NP-complete problems Fall 2010
Jan 03, 2016
CSCI 3130: Formal languages and automata theory
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
The Chinese University of Hong Kong
More NP-complete problems
Fall 2010
NP-complete problems
CLIQUE
IND SET
VERTEX COVER
DOM SET MIN COVER
HAM PATH
HAM CYCLE
PARTITION
SUBSET SUM
P
NP
NP-complete
SAT
3SAT
SAT and 3SAT
SAT = { 〈〉 : is a satisfiable Boolean formula}
3SAT = { 〈〉 : is a satisfiable 3CNF formula}
(x1∨x2∨x2 ) ∧ (x2∨x3∨x4) ∧ (x2∨x3∨x5) ∧ (x1∨x5∨x5)
((x1∨x2 ) ∧ (x1∨x2)) ∨ ((x1∨(x2 ∧ x3)) ∧ x3)
Reducing SAT to 3SAT
• Example: =(x2∨(x1∧x2 ))∧(x1∧(x1∨x
2 ))
We add extra variables for every gate (“wire”)
x3
x6 x7
x8 x9
x4 x5
x10
AND
OR NOT
AND
NOT OR
AND
NOT
x2 x1 x2 x1 x1 x2
x7 = x4 ∧ x5x4 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
(x4∨x5∨x7)(x4∨x5∨x7)
(x4∨x5∨x7)
(x4∨x5∨x7)
(x4∨x5∨x7)∧(x4∨x5∨x7)∧(x4∨x5∨x7)∧(x4∨x5∨x7)
Turning gates into 3CNFs
AND
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
z = y ∧ xx y z
x y
z
(x∨y∨z)∧(x∨y∨z) ∧(x∨y∨z)∧(x∨y∨z)
OR
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
z = y ∨ xx y z
x y
z
(x∨y∨z)∧(x∨y∨z) ∧(x∨y∨z)∧(x∨y∨z)
NOT
T T FT F T F T TF F F
z = xx z
x
z
(x∨z)∧(x∨z)
(x∨x∨z)∧(x∨x∨z)
Gj:
j:
Reducing SAT to 3SAT
Boolean formula 3CNF formula ’R
’ = n+1 ∧ n+2 ∧ ... ∧ t ∧ (xn+t ∨xn+t ∨xn+t )
requires thatoutput of f is true
On input , where is a boolean formula
Construct and output the following 3CNF formula ’:
R:=
’ has extra variables xn+1, ..., xn+t one for each gate Gj in
For each gate Gj, construct the formula fj Output
Example
AND
NOT
x1 x2
f = x3
x4
f’ := (x2∨x2∨x3)∧(x2∨x2∨x3)(x1∨x3∨x4)∧(x1∨x3∨x4) ∧(x1∨x3∨x4)∧(x1∨x3∨x4)
∧
(x4∨x4∨x4)∧
AND
NOT
T F
T
T
x1 = T, x2 = F, x3 = T, x4 = T
is a satisfying assignment of f’
NOT gate
AND gate
output
Reducing SAT to 3SAT
• Every satisfying assignment of f extends uniquely to a satisfying assignment of f’
• In the other direction, in every satisfying assignment of f’ the part x1, ..., xn is a satisfying assignment of f
Boolean formula f 3CNF formula f’R
f’ is satisfiablef is satisfiable ✔
How to argue NP-completeness
SUBGRAPH = { 〈 G, H 〉 : G and H are graphs whereH is a subgraph of G}
G H
SUBGRAPH is NP-complete
1 2 3
4 5
6 7
1 2
3
4
Every edge of H must be included in G
1 2 3
4 5
6 7
How to argue NP-completeness
• Step 1: SUBGRAPH is in NP
G H1 2
3
4
What is a solution for SUBGRAPH?
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:
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.
Running time = O(m)m = number edges in H
✔
How to argue NP-completeness
• Step 2: Some NP-complete L reduces to SUBGRAPH
CLIQUEIND SET
VERTEX COVER
SAT 3SAT
DOM SETDirection of reduction is important!
You want to show SUBGRAPH is harder than L
So you assume you can solve SUBGRAPH, and show how to solve L
This requires converting instances of L into instances of SUBGRAPH How to choose L?
Tips on choosing L
• 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
G H1 2 3
4 5
6 7
1 2
3
4
G
4
CLIQUE SUBGRAPH
How to argue NP-completeness
• Step 3: Reduce CLIQUE to SUBGRAPH
SUBGRAPH = { 〈 G, H 〉 : H is a subgraph of G}
CLIQUE = { 〈 G, k 〉 : G has a k-clique}
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?
Does it have a 1-cover for triangles?
No, because it has two vertex-disjoint triangles
Yes
TRICOVER = { 〈 G, k 〉 : G has a k-cover for triangles}
TRICOVER is NP-complete
Cover for triangles
• Step 1: TRICOVER is in NP
What is a solution for TRICOVER?
A solution s is a subset of vertices like {2,5}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)
✔
1 2 3
45
6 7
CLIQUEIND SET
VERTEX COVER
SAT 3SAT
DOM SETSUBGRAPH
Cover for triangles
• Step 2: Some NP-complete L reduces to TRICOVER
TRICOVER = { 〈 G, k 〉 : G has a k-cover for triangles}
Some vertex in every triangle is covered
Some endpoint of every edge is covered
VC = { 〈 G, k 〉 : G has a k-vertex cover}
Cover for triangles
• Step 3: Reduce VC to TRICOVER
TRICOVER = { 〈 G’, k’ 〉 : G’ has k’-cover for triangles}
VC = { 〈 G, k 〉 : G has a k-vertex cover (for edges)}
Idea: Replace edges by triangles
G G’
vertex-cover in 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 G’
〈 G, k 〉 ∈ VC 〈 G’, k 〉 ∈ TRICOVER
G has a k-vertex cover C 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 G’
〈 G, k 〉 ∈ VC 〈 G’, k 〉 ∈ TRICOVER
G has a k-vertex cover C’ G’ has a k-vertex cover C
Some vertices in C may notcome from G!
But we can move them and stillcover the same triangle
✔
C’ is obtained after moving somevertices of C
Since C’ covers all triangles in G’,it must cover all edges in G
✔
Coloring
CSCI 3230
CSCI 2110
CSCI 3160CSCI 3130
Suppose you need to schedule exams on same day in 3 time slots
Y R B
1030-1230130-330 430-630
CSCI 3190
Some people are enrolled inmultiple classes, so there may be conflicts
Task: Schedule the exams so there are no conflicts
Coloring
Given a graph, we want to colorthe vertices so every edge has endpoints of different colors G
Y R 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
What is a solution for 3COL?
A solution s is a coloring of vertices like {1B,2R,3Y,4R,5B}
V := On input 〈 G, s 〉For every edge {u, v} of G:
If u and v are assigned same color in s, reject.
Otherwise, accept.
Running time = O(m)
✔
G
1 2
3
4 5
CLIQUEIND SET
VERTEX COVER
SAT 3SAT
DOM SETSUBGRAPH
Coloring
• Step 2: Some NP-complete L reduces to 3COL
3COL = { 〈 G 〉 : G has a valid 3-coloring}
3COL does not look likeany of these problems
In such a case it is a goodidea to start with 3SAT
Coloring
• Step 3: Reduce 3SAT to 3COL
3COL = { 〈 G 〉 : G has a valid 3-coloring}
3SAT = { 〈〉 : is a 3CNF that has a satisfying assignment}
Idea: Assignment of Coloring of G
Part I: 3 special vertices T (true), F (false), and X
T F
X G
Coloring
Part 2: For each variable xi
xi
X
xi
Either xi has color of T and xi has color of F
Or xi has color of F and xi has color of T
Example: (x1∨x1∨x2)∧(x1∨x2∨x3 )
x1 x1 x2 x2 x3 x3
X
T F
T F F T F T
Coloring
• To “encode” the clauses of , we need a gadget
Can be colored if and only if at least one literal is colored TT
x1
x2
x3
x
yz
If x or y is colored T, z can be colored TIf x and y are colored F, z must be colored F
Part 3: For each clause like (x1∨x2∨x3 )
Coloring reduction: An example
Example: (x1∨x1∨x2)∧(x1∨x2∨x3 )
x1 x1 x2 x2 x3 x3
X
T F
T F F T F T
Coloring reduction: An example
Example: (x1∨x1∨x2)∧(x1∨x2∨x3 )
x1 x1 x2 x2 x3 x3
X
T F
T F F T F T
Coloring
• Step 3: Reduce 3SAT to 3COL
R := On input 〈〉 , where is a 3CNF:
Construct the following graph G:Add 3 special vertices T, F, X connected by a triangle.
For every variable xi of , add vertices xi and xi and include the triangle xi, xi, X.
Output 〈 G 〉 .
Running time = O(m + n)n = number of variablesm = number of clauses
For every clause u∨v∨w of , connect u, v, w, and T using the gadget below.
T
u
v
w
Coloring
• Step 4: Argue that the reduction is correct
〈〉 ∈ 3SAT
has a SAT assignment a
✔(x1∨x1∨x2)∧(x1∨x2∨x3 )
x1 x1 x2 x2 x3 x3
X
T F
〈 G 〉 ∈ 3COL
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
〈〉 ∈ 3SAT
has a SAT assignment
✔(x1∨x1∨x2)∧(x1∨x2∨x3 )
x1 x1 x2 x2 x3 x3
X
T F
〈 G 〉 ∈ 3COL
G has a valid 3-coloring c
Each variable gets value as in G
Since each clause gadget is colored properly,each clause must contain a true literal
xi = true, if node xi has same color as T false, if node xi has same color as F