DD2352 Algorithms & Complexity Lecture 9: NP-Completeness
Per Austrin
2020-02-11
Agenda
Recap From Yesterday
NP-completeness
NP-completeness examples
P and NP (Recap)
I P (Polynomial time):
the set of (decision) problems that can be solved in polynomial time
I NP (Nondeterministic Polynomial time):
the set of (decision) problems where a “yes” answer can be verified in polynomial time given an additional “witness” or
“proof”
Karp Reductions (Recap)
X ≤pY (“X Karp-reduces to Y ”) if there is a reduction of the following form from X to Y :
1 function SolveX(instance x of problem X):
2 Create (in polynomial time) an instance y of problem Y in some way from x
3 return SolveY(y )
Put differently:
there is a polynomial time function f that maps instances x of problem X
into instances f (x) of problem Y such that x ∈ X if and only if f (x) ∈ Y
Caveat: the book never introduces Karp reductions and only uses Turing reductions. This leads to subtle but crucial differences in the material that follows. We will follow the standard definitions and use Karp reductions.
The P vs NP problem
The million dollar question (literally):
Is P = NP?
If a problem admits a solution that can be easily checked step-by-step, is it then also easy to find a solution?
(Probably not, but humanity is nowhere near a proof.)
NP-hardness
We say that a problem Y isNP-hardif every problem in NP can be reduced to Y
I.e., if X ≤pY for every X ∈ NP
Intuitive meaning: Y is at least as hard as every problem in NP
In particular, ifanyproblem in NP is difficult to solve (has no poly-time algorithm), then neither does Y
NP-completeness
Definition
A problem Y isNP-completeif (i) Y ∈ NP, and
(ii) Y is NP-hard.
Intuition:
the NP-complete problems are the hardest problems in NP (being NP-hard, they are at least as hard as every problem in NP)
NP-completeness, What’s the Big Deal?
At first glance, it is far from obvious whether this is a useful definition – not even clear whether any NP-complete problems even exist.
And even if they exist, it seems like a pretty unwieldy notion to work with, to prove NP-hardness for Y we need to prove thatall problems in NP can be reduced to Y .
Or do we?
Nice feature of NP-hardness: if X is NP-hard and X ≤p Y , then Y is NP-hard as well.
In other words: to prove NP-hardness for Y weonly have to find one other NP-hard problem X and reduce X to Y, we don’t have to reduce all problems in NP to Y .
The CNF-Sat Problem
A CNF formula is a logic formula of the form:
(x1∨ x2) ∧ (x4∨ ¬x2∨ ¬x17∨ x4711) ∧ : : : In words:
I Aconjunction(logical AND) of a bunch of clauses
I Each clause is a disjunction(logical OR) of some number of literals
I Each literal is either a variable or a negated variable.
(CNF = Conjunctive Normal Form) Sat
Input: A CNF formula Φ.
Output: Is Φ satisfiable?
Yesterday we talked about the closely related 3-Sat problem. The only difference is that in 3-Sat, the clauses have only 3 literals.
The Cook-Levin Theorem
Theorem (Cook-Levin) The Sat problem is NP-hard.
This is a remarkable result!
Next week we will see how such a theorem can be proved.
The Sat problem is also in NP
(given a satisfying assignment we can check it in polynomial time) We have our first NP-complete problem!
3-Sat
If Sat is NP-hard, perhaps also 3-Sat is?
We have that 3-Sat ≤pSat
(because 3-Sat is a special case of Sat) Does it follow that 3-Sat is NP-hard?
No! That’s the wrong direction of the reduction!
To establish NP-hardness of 3-Sat, we would need to show Sat ≤p 3-Sat
Reduction from Sat to 3-Sat
Given a Sat instance Φ, we construct a new 3-Sat instance Ψ as follows.
I Clauses of length ≤ 3 in Φ we keep as they are (since they are allowed in 3-Sat)
I For a clause (x1∨x2∨ : : : ∨xk) of length k > 3 in Φ, we introduce k − 3 new variables y1;y2; : : : ;yk−3, and create the following k − 2 new clauses of size 3:
(x1∨x2∨¬y1) (y1∨x3∨¬y2) (y2∨x4∨¬y3)
: : :
(yk−4∨xk−2∨¬yk−3) (yk−3∨xk−1∨xk)
I The intuition behind the y variables is that yi is an indicator of whether at least one of the first i + 1 x-variables are true.
Analyzing the reduction
To prove that a reduction like this is correct, we need to establish three things:
1. The reduction is polynomial-time This is in general the easiest part
2. If the original Sat instance Φ is satisfiable, then the new 3-Sat instance Ψ produced by the reduction is also satisfiable.
3. If the new 3-Sat instance Ψ is satisfiable, then the original Sat instance Φ is also satisfiable.
This is often the most difficult part.
Proofs of these properties for the reduction from Sat to 3-Sat shown on whiteboard.
Examples from Previous Lecture
Yesterday we saw the following chain of relations:
3-Sat ≤T Independent Set ≤T Vertex Cover ≤T Set Cover
All three reductions were Karp reductions, so we actually proved the following (stronger) chain of relations:
3-Sat ≤pIndependent Set ≤pVertex Cover ≤pSet Cover
We just saw that 3-Sat is NP-hard, and from this it follows that the other three problems are NP-hard as well.
We saw yesterday that all four problems are in NP, so they are all NP-complete.
Hamiltonian Cycle
AHamiltonian Cyclein a directed graph G is a cycle that visits every vertex exactly once.
Hamiltonian Cycle Input: Graph G.
Output: Does G have a Hamiltonian Cycle?
Theorem: Hamiltonian Cycle is NP-hard.
(Reduction from 3-Sat on whiteboard.)
Travelling Salesman
Given a set of locations, and the n × n matrix D of all pairwise distances between them.
(D(u; v ) is the distance of going from u to v )
What is the shortest distance we need to travel to visit all locations and return to where we started?
This is the famousTravelling Salesmanproblem.
Travelling Salesman
Input: Distance matrix D on n points, and parameter k.
Output: Is there a travelling salesman tour of length at most k?
Theorem: Travelling Salesman is NP-hard.
(Reduction from Hamiltonian Cycle on whiteboard.)
Graph Coloring
Acoloring of an undirected graph G is an assignment of colors to the vertices so that no two adjacent vertices have the same color.
Ak-coloringis a coloring using only k different colors.
R
R R
R G R
G G
G B
B B
B B
R
R R
R G R
G G
G B
B B
B B
A 3-coloring of G 3-Coloring
Input: Graph G
Output: Is G 3-colorable?
Theorem: 3-Coloring is NP-hard.
(Proof in book.)
Subset Sum
Subset Sum
Input: A list of n integers x1; : : : ; xn and a target integer t.
Output: Is there a subset S ⊆ {1; : : : ; n} such thatPi ∈Sxi = t?
Example: can we choose some of the numbers 385189571, 4785917510, 4758197610, and 3185971612 to get the sum 5143377181?
Theorem: Subset Sum is NP-hard.
(Proof in book.)
Knapsack
Knapsack
Input: A capacity C, parameter k, and a list of objects, each object has a value vi and weight wi
Output: Is there a subset S of objects such thatPi ∈Swi ≤ C and P
i ∈Svi ≥ k?
(We saw this problem in Lecture 4. Then we phrased it as an optimization problem where the goal was to maximizeP
i ∈Svi, now we are formulating it as a decision problem.)
Theorem: Knapsack is NP-hard.
(Reduction from Subset Sum on whiteboard.)
Wait a minute...
How can Knapsack be NP-hard, we came up with a dynamic programming algorithm for it in Lecture 4? Why didn’t this just prove P = NP?
The algorithm we saw in Lecture 4 had time complexity O(n · C).
What is the size of a Knapsack instance?
I We need log(C) bits to represent C.
I Assuming all the wi’s and vi’s are between 0 and C, the total size of the input is O(n log C) bits
I n · C is not polynomialin n log C!
I For example, suppose C ≈ 2n, all the values and weights are n-bit integers.
I Then the size of the input is O(n2) bits, but the running time of the algorithm is O(n2n)
NP-completeness, summary
We have seen a few NP-completeness results:
Sat, 3-Sat, Independent Set, Vertex Cover, Set Cover, Hamiltonian Cycle, Travelling Salesman, 3-Coloring, Subset Sum, Knapsack
There are literally thousands upon thousands of other problems that have been shown to be NP-complete over the years.
These are all interreducible – ifany single one of all these problems cannot be solved in polynomial time, then neither of them can.
When faced with a new problem, in order to add it to the
“NP-hardness club”, “all” we have to do is reduce one other NP-hard problem to it.
Next week
Lectures 10 and 11 on Wednesday and Thursday More complexity classes: CoNP and PSPACE.
Turing machines, decidability, and how the Cook-Levin Theorem is proved