comp 382 - rice universitysc40/comp382/lectures/unit10.pdf · comp 382 unit 10: np-completeness....
TRANSCRIPT
COMP 382Unit 10: NP-Completeness
Time complexitynn
2n
n3
n2
nlog n
1
Space complexitynn
2n
n3
n2
nlog n
1
Complexity theory
Focus on decidability (yes/no) problems
What is P?
Deterministic, Sequential, Polynomial Time
Example problems in P
• Is 𝑥 a multiple of 𝑦?
• Is 𝑥 prime?
• Are 𝑥 and 𝑦 relatively prime?
• Is the edit distance between 𝑥 and 𝑦 less than 5?
• Is there a path between 𝑢 and 𝑣 of length less than 10?
Most problems mentioned so far in class.
Understanding non-determinism:Guess & checkdefine SubsetSum(input: set of integer,
value: integer)
returns boolean
subset = Guess()
return sum(subset) == value
Two views of semantics:
• “Oracle” makes correct guesses
• Guess forks for all possibilities at once. Search succeeds if check succeeds for any guess.
Understanding non-determinism:Given a certificate, check it
define SubsetSum_Certifier
(input: set of integer, value: integer,
witness: set of integer)
returns boolean
return subset(witness, input) &&
sum(witness) == value
Only needs to verify inputs on which SubetSum would return True.
Original inputs
Proof / Certificate / Witness
Can simulate non-determinism
• Previously discussed ND TMs – non-deterministic choice of transition
• Simulate guess & check:• Generate all possible guesses one at a time
• Check
• Backtrack
What is NP?
• Non-deterministic, Sequential, Polynomial Time• ND TM is polynomial time
• Check phase is polynomial time
• Includes all of P
• Includes lots not known to be in P
Example problems in NP: SAT
Given a Boolean formula, is there a satisfying value assignment?𝑎 ∧ 𝑏 ∨ ¬ 𝑏 ∨ 𝑑 ∧ ¬𝑐 ∧ ¬𝑏 ∨ 𝑐
• Deterministic algorithm to solve?
• Deterministic algorithm to check? What is appropriate certificate?
Example problems in NP: Directed Hamiltonian CycleGiven a directed graph, does it have a simple cycle through everyvertex?
• Deterministic algorithm to solve?
• Deterministic algorithm to check? What is appropriate certificate?
A
F
E
D
C
B
Example Problems in NP: Composite
Given an integer, is it composite?
• Deterministic algorithm to solve?
• Deterministic algorithm to check? What is appropriate certificate?
Many open questions
• L=P?
• P=BPP?
• P=NP?
• P=PSPACE?
• NP=co-NP?
• BPP=NEXP?
If P=NP
• Many “hard” problems would actually be in P. We’d have a path to finding polynomial algorithms for them.
• Many other complexity open questions would be answered.
• Someone wins $1M from Clay Math. Institute and becomes famous.
If P = NP, then the world would be a profoundly different place than we usually assume it to be. There would be no special value in “creative leaps”, no fundamental gap between solving a problem and recognizing the solution once it's found. -- Scott Aaronson
𝐴 reduces to 𝐵 in polynomial time: 𝐴 ≤𝑃 𝐵
Map instance of one problem to instance(s) of another in P-time.
Prove correctness: Problem A says “yes” exactly when this implementation of A says “yes”.
define A(args):
// Polynomial calls to subroutine B(), plus
// Polynomial other steps.
return …
𝐴 reduces to 𝐵 in polynomial time: 𝐴 ≤𝑃 𝐵
Typical: Map instance of one problem to instance of another in P-time.
Prove correctness: Problem A says “yes” exactly when problem B says “yes” on its constructed inputs.
define A(a_args):
b_args = … // Polynomial steps.
return B(b_args)
Using P-reductions: Which is true?
A. If 𝐵 ∈ 𝑃 and 𝐴 ≤𝑃 𝐵, then 𝐴 ∈ 𝑃.
B. If 𝐴 ∈ 𝑃 and 𝐴 ≤𝑃 𝐵, then 𝐵 ∈ 𝑃.
Using P-reductions: Which is true?
A. If 𝐵 ∉ 𝑃 and 𝐴 ≤𝑃 𝐵, then 𝐴 ∉ 𝑃.
B. If 𝐴 ∉ 𝑃 and 𝐴 ≤𝑃 𝐵, then 𝐵 ∉ 𝑃.
NP-Complete
A problem 𝑌 ∈ NP is NPC if for all problems 𝑋 in NP, 𝑋 ≤𝑃 𝑌.
Theorem: If Y is NPC, then 𝑌 ∈ 𝑃, iff P=NP.
Proof:
• () By assumption, 𝑌 ∈ NP. If P=NP, then 𝑌 ∈ P.
• () Suppose 𝑌 ∈ P. Let 𝑋 be any problem in NP. Since 𝑋 ≤𝑃 𝑌, then 𝑋 ∈ P. So, NPP. Since we already know PNP, then P=NP.
𝑌 is NP-Hard
First NPC problem – Circuit-SAT
Previously said the equivalent Boolean-formula version is in NP.
First NPC problem – Circuit-SAT
For all problems 𝑋 in NP, 𝑋 ≤𝑃 Circuit−SAT:
𝑋 solvable by a ND TM in 𝑂 𝑓 𝑛 steps, where 𝑓 is polynomial.
Goal: Given input 𝑥, produce a circuit that is satisfiable iff 𝑥 ∈ 𝑋.
Circuit’s inputs:• Hardcoded 𝑥
• Unknown certificate c, of 𝑓 𝑥 bits
First NPC problem – Circuit-SAT:Example constructed circuit
General NPC Proof Strategy
• Show 𝑌 ∈ NP.
• Pick some NPC problem 𝑋.
• Prove 𝑋 ≤𝑃 𝑌.
In theory, the choice of 𝑋 is irrelevant.In practice, it is very important.
3-CNF SAT is NPC
Given a Boolean formula in 3-CNF, is there a satisfying value assignment?
𝑎 ∨ 𝑏 ∨ 𝑐 ∧ 𝑎 ∨ 𝑐 ∨ 𝑑 ∧ 𝑏 ∨ 𝑐 ∨ 𝑑 ∧ 𝑏 ∨ 𝑐 ∨ 𝑑
In NP – Just a special case of SAT.
Which do we need to show?
A. Circuit−SAT ≤𝑃 3−CNF−SAT
B. 3−CNF−SAT ≤𝑃 Circuit−SAT
Circuit−SAT ≤𝑃 3−CNF−SAT
Given a circuit, construct an equivalent 3-CNF formula:1. Label wires.
2. Construct 3-CNF clauses for each gate.
3. AND all the pieces together.
𝑎
𝑏
𝑐
𝑐 = 𝑎 ∨ 𝑏≡
𝑎 ∨ 𝑏 ∨ 𝑐 ∧
𝑎 ∨ 𝑏 ∨ 𝑐 ∧
𝑎 ∨ 𝑏 ∨ 𝑐 ∧
𝑎 ∨ 𝑏 ∨ 𝑐
𝒂 𝒃 𝒄 𝒂 ∨ 𝒃 = 𝒄
F F F T
F F T F
F T F F
F T T T
T F F F
T F T T
T T F F
T T T T
Independent set is NPC
Given an undirected graph and integer 𝑘, does the graph have a subset of 𝑘 vertices which are not adjacent to each other?
• Deterministic algorithm to check? What is appropriate certificate?
3−CNF−SAT ≤𝑃 IndependentSet
Given 3-CNF formula, construct a graph with a 𝑘-independent set iffthe formula is satisfiable.
1. Form a triangle for each clause.
2. Link complementary nodes.
3. Let 𝑘 = the number of clauses.
𝑎 ∨ 𝑏 ∨ 𝑐 ∧ 𝑎 ∨ 𝑏 ∨ 𝑑 ∧ 𝑏 ∨ 𝑐 ∨ 𝑑 ∧ 𝑎 ∨ 𝑑 ∨ 𝑒
𝑎
𝑏 𝑐
𝑎
𝑏 𝑑
𝑏
𝑐 𝑑
𝑎
𝑑 𝑒
Vertex cover is NPC
Given a graph and an integer 𝑘, is there a set 𝑆 of vertices, such that 𝑆 ≤ 𝑘 and every edge has an endpoint in 𝑆.
• Deterministic algorithm to check? What is appropriate certificate?
IndependentSet ≤𝑃 VertexCover
Given graph 𝐺, use the same 𝐺.
Claim: 𝑆 is an independent set of 𝐺 iff 𝑉 ∖ 𝑆 is a vertex cover for 𝐺.
IndependentSet ≤𝑃 VertexCover: Proof
Claim: 𝑆 is an independent set of 𝐺 iff 𝑉 ∖ 𝑆 is a vertex cover for 𝐺.
Let 𝑆 be any independent set.
Consider arbitrary edge 𝑢, 𝑣 .• 𝑢 ∉ 𝑆 or 𝑣 ∉ 𝑆
• 𝑢 ∈ 𝑉 ∖ 𝑆 or 𝑣 ∈ 𝑉 ∖ 𝑆
• 𝑉 ∖ 𝑆 covers 𝑢, 𝑣
IndependentSet ≤𝑃 VertexCover: Proof
Claim: 𝑆 is an independent set of 𝐺 iff 𝑉 ∖ 𝑆 is a vertex cover for 𝐺.
Let 𝑉 ∖ 𝑆 be any vertex cover.
Consider arbitrary 𝑢 ∈ 𝑆 and 𝑣 ∈ 𝑆.• 𝑢, 𝑣 ∉ 𝐸
Thus, no two nodes in 𝑆 are linked by an edge.
So, 𝑆 is an independent set.
Set cover is NPC
Given a set 𝑈 of elements, a collection of subsets of 𝑈, and an integer 𝑘, does there exist a collection of at most 𝑘 of these subsets whose union is 𝑈?
Set cover example
𝑈 = 1,2,3,4,5,6,7 , 𝑘 = 2
• 𝑆1 = 3,7
• 𝑆2 = 3,4,5,6
• 𝑆3 = 1
• 𝑆4 = 2,4
• 𝑆5 = 5
• 𝑆6 = 1,2,6,7
What is a set cover?
VertexCover ≤𝑃 SetCover
Given graph and 𝑘, construct 𝑈, a collection of subsets, and 𝑘′ that have a set cover iff the graph has a vertex cover.
• 𝑈 = 𝐸, Subsets = 𝑒 ∈ 𝐸: 𝑒 is incident to 𝑣 , 𝑘′ = 𝑘
𝑈 = 1,2,3,4,5,6,7 , 𝑘′ = 2
𝑆𝑎 = 3,7 , 𝑆𝑏 = 2,4𝑆𝑐 = 3,4,5,6 , 𝑆𝑑 = 5𝑆𝑒 = 1 , 𝑆𝑓 = 1,2,6,7
𝑎 𝑏
𝑒 𝑑
𝑓 𝑐
𝑒7𝑒3 𝑒2
𝑒6
𝑒4
𝑒1 𝑒5
Directed Hamiltonian cycle is NPC
Given a directed graph, does it have a simple cycle through everyvertex?
Previously saw in NP.
A
F
E
D
C
B
3−CNF−SAT ≤𝑃 DirHamCycle
Given a 3-CNF formula, construct a directed graph that has a HamCycleiff the formula is satisfiable.
3−CNF−SAT ≤𝑃 DirHamCycle: Step 1
…
…
…
𝑥1
𝑥2
𝑥3
One per variable
2𝑛 Ham. cycles
3−CNF−SAT ≤𝑃 DirHamCycle: Step 2
One pair per clause
2𝑘 + 2 nodes
3−CNF−SAT ≤𝑃 DirHamCycle: Step 3
𝑥1
𝑥2
𝑥3
𝑥1 ∨ 𝑥2 ∨ 𝑥3One per clausePositive literal: left-to-right
Negative literal: right-to-left
3−CNF−SAT ≤𝑃 DirHamCycle: Proof
Claim: Formula is satisfiable iff graph has Hamiltonian cycle.
Suppose formula has satisfying assignment 𝑥∗.
Define Hamiltonian cycle:• If 𝑥𝑖
∗ = 1, traverse row 𝑖 from left to right.
• If 𝑥𝑖∗ = 0, traverse row 𝑖 from right to left.
For each clause 𝑗, there will be at least one row 𝑖 in which the cycle goes in the “correct” direction to splice clause node 𝑗 into the cycle.
3−CNF−SAT ≤𝑃 DirHamCycle: Proof
Claim: Formula is satisfiable iff graph has Hamiltonian cycle.
Assume constructed graph 𝐺 has Hamiltonian cycle.
Cycle must traverse each row 𝑖.• Set 𝑥𝑖
∗ = 1 iff this cycle traverses row 𝑖 left to right.
Cycle must use each clause node 𝑗.• Must enter and leave via a pair of edges to/from same row.
• This pair of edges must go in same direction that cycle traverses the row.
• So each clause is satisfied by the truth assignment.
3-Coloring is NPC
Given an undirected graph, does there exist a way of coloring the nodes with three colors, so that no two adjacent nodes have the same color?
• Deterministic algorithm to check? What is appropriate certificate?
3−CNF−SAT ≤𝑃 3−Color
Given 3-CNF formula, create a graph that is 3-colorable iff the formula is satisfiable.
3−CNF−SAT ≤𝑃 3−Color: Step 1
T F
B
…𝑥1 𝑥1 𝑥2 𝑥2 𝑥3 𝑥3 𝑥𝑛 𝑥𝑛
One pair per variable
Ensures each variable has one literal colored like T, one like F.
3−CNF−SAT ≤𝑃 3−Color: Step 2
𝑥1 ∨ 𝑥2 ∨ 𝑥3
For each clause:
T F
B
𝑥1 𝑥2 𝑥3
Ensures each clause has at least one literal colored like T.
3−CNF−SAT ≤𝑃 3−Color: Gadget cases 0T F
B
Not 3-colorable
3−CNF−SAT ≤𝑃 3−Color: Gadget cases 1
T F
B
T F
B
T F
B
3−CNF−SAT ≤𝑃 3−Color: Gadget cases 2
T F
B
T F
B
T F
B
T F
B
T F
B
T F
B
T F
B
T F
B
T F
B
3−CNF−SAT ≤𝑃 3−Color: Gadget cases 3
T F
B
T F
B
T F
B
T F
B
Subset sum is NPC
Given a set of natural numbers and a number 𝑆, is there a subset that adds up to 𝑆?
Note: Reduction must be polynomial in the encoding size.
• Deterministic algorithm to check? What is appropriate certificate?
𝑈 = −9,−2,3,6,7,8,12,35𝑆 = 16
3−CNF−SAT ≤𝑃 SubsetSum
Given 3-CNF formula, create a set and total that has a subset sum iffthe formula is satisfiable.
𝑎 ∨ 𝑏 ∨ 𝑐 ∧ 𝑎 ∨ 𝑏 ∨ 𝑑 ∧ 𝑏 ∨ 𝑐 ∨ 𝑑
𝑎 𝑏 𝑐 𝒅 𝑪𝟏 𝑪𝟐 𝑪𝟑 Set:
𝒂 1 1 1 1,000,110
𝒂 1 1,000,000
𝒃 1 1 100,010
𝒃 1 1 1 100,101
𝒄 1 1 10,100
𝒄 1 1 10,001
𝒅 1 1 1,001
𝒅 1 1 1,010
1 100
2 200
1 10
2 20
1 1
2 2
𝑺 1 1 1 1 4 4 4 1,111,444
3−CNF−SAT ≤𝑃 SubsetSum
Given 3-CNF formula, create a set and total that has a subset sum iffthe formula is satisfiable.
𝑎 ∨ 𝑏 ∨ 𝑐 ∧ 𝑎 ∨ 𝑏 ∨ 𝑑 ∧ 𝑏 ∨ 𝑐 ∨ 𝑑
𝑎 𝑏 𝑐 𝒅 𝑪𝟏 𝑪𝟐 𝑪𝟑 Set:
𝒂 1 1 1 1,000,110
𝒂 1 1,000,000
𝒃 1 1 100,010
𝒃 1 1 1 100,101
𝒄 1 1 10,100
𝒄 1 1 10,001
𝒅 1 1 1,001
𝒅 1 1 1,010
1 100
2 200
1 10
2 20
1 1
2 2
𝑺 1 1 1 1 4 4 4 1,111,444
Exactly one literal picked for each variable.Exactly one number of each pair picked for sum.
3−CNF−SAT ≤𝑃 SubsetSum
Given 3-CNF formula, create a set and total that has a subset sum iffthe formula is satisfiable.
𝑎 ∨ 𝑏 ∨ 𝑐 ∧ 𝑎 ∨ 𝑏 ∨ 𝑑 ∧ 𝑏 ∨ 𝑐 ∨ 𝑑
𝑎 𝑏 𝑐 𝒅 𝑪𝟏 𝑪𝟐 𝑪𝟑 Set:
𝒂 1 1 1 1,000,110
𝒂 1 1,000,000
𝒃 1 1 100,010
𝒃 1 1 1 100,101
𝒄 1 1 10,100
𝒄 1 1 10,001
𝒅 1 1 1,001
𝒅 1 1 1,010
1 100
2 200
1 10
2 20
1 1
2 2
𝑺 1 1 1 1 4 4 4 1,111,444
Picking literal satisfies corresponding clauses.
3−CNF−SAT ≤𝑃 SubsetSum
Given 3-CNF formula, create a set and total that has a subset sum iffthe formula is satisfiable.
𝑎 ∨ 𝑏 ∨ 𝑐 ∧ 𝑎 ∨ 𝑏 ∨ 𝑑 ∧ 𝑏 ∨ 𝑐 ∨ 𝑑
𝑎 𝑏 𝑐 𝒅 𝑪𝟏 𝑪𝟐 𝑪𝟑 Set:
𝒂 1 1 1 1,000,110
𝒂 1 1,000,000
𝒃 1 1 100,010
𝒃 1 1 1 100,101
𝒄 1 1 10,100
𝒄 1 1 10,001
𝒅 1 1 1,001
𝒅 1 1 1,010
1 100
2 200
1 10
2 20
1 1
2 2
𝑺 1 1 1 1 4 4 4 1,111,444
Must pick at least one row/number to satisfy each clause. 𝐶𝑖’s total from the top is 1, 2, or 3 – the bottom rows/numbers take up the slack but aren’t sufficient alone.
Common Reduction Strategies
• Simple mapping/equivalence• IndependentSet ≤𝑃 VertexCover
• Special to general case• VertexCover ≤𝑃 SetCover
• Complicated encodings• Circuit−SAT ≤𝑃 3−CNF−SAT (general to special case)
• 3−CNF−SAT ≤𝑃 IndependentSet
• 3−CNF−SAT ≤𝑃 DirHamCycle
• 3−CNF−SAT ≤𝑃 3−Color
• 3−CNF−SAT ≤𝑃 SubsetSum
Exercise: Hitting set is NPC
Given a set 𝑈 of elements, a collection of subsets, and integer 𝑘, does there exist a subset of 𝑈 of size ≤ 𝑘 that overlaps with all the given subsets?
Exercise: Efficient recruiting is NPC
Suppose you are helping organize a summer camp. The camp is supposed to have at least one counselor who is skilled at each of the 𝑛sports covered by the camp (baseball, volleyball, …).
They have received job applicants from 𝑚 potential counselors.
For each of the 𝑛 sports, there is a subset of the 𝑚 applicants qualified in that sport.
The question: For a given number 𝑘 < 𝑚, is it possible to hire at most 𝑘 counselors and have at least one counselor qualified in each sport?
Exercise: Zero-weight cycle is NPC
Given a directed graph with weighted edges, is there a simple cycle of total weight 0?