Notes Npcomplete
Notes Npcomplete
This document contains slides from the lecture, formatted to be suitable for printing or individ-
ual reading, and with some supplemental explanations added. It is intended as a supplement
to, rather than a replacement for, the lectures themselves — you should not expect the notes to
be self-contained or complete on their own.
1 Introduction
CLRS 34
The theory of NP-completeness can be used cast doubt on the existence of any polynomial-time GJ 1–3
algorithm for a given problem.
• So far, we have concentrated mostly on designing and analyzing efficient algorithms for
various problems. Algorithms are evidence of how easy those problems are.
• By showing that a problem is NP-complete, we are giving evidence of how hard a problem
is.
Practically, we can think of an NP-completeness proof as a ‘license’ to stop looking for an efficient
algorithm, and settle for approximation or to consider only special cases.
Note: Both CLRS and GJ define things at higher level of precision than we’ll examine in the lecture:
models of computation, abstract vs. concrete problems, encodings, etc.
4 Decision problems
A decision problem is a problem in which the correct output for each instance is either ‘Yes’ or
‘No’.
HAM-CYCLE:
Instance: An undirected graph G = (V, E).
Question: Does G contain a cycle that visits every vertex exactly once?
PATH:
Instance: A weighted directed graph G, a pair of vertices u, v ∈ V (G), and a number k.
Question: Does G contain a path from u to v with total weight at most k?
6 Problems as languages
We can think of a decision problem as a formal language.
• The input is a (binary) string s.
• The output is either ‘Yes’ or ‘No’.
The language of a problem is the set of binary input strings, under a suitable encoding, for which
the correct output is ‘Yes’.
The language LHAM-CYCLE is the set of strings describing undirected graphs that have Hamilto-
nian cycles.
8 Deciding a language
Definition: An algorithm decides a language if it correctly determines whether its input string is
a member of that language.
9 P
Intuition: P is the set of all problems that can be decided in polynomial time.
Definition: P is the set of all languages L for which there exists an algorithm A and a constant c,
such that
• A decides L, and
10 How to prove: P
To prove that your problem L is in P:
• Show that the worst-case run time of your algorithm is bounded by some polynomial.
Definition: NP is the set of all languages L for which there exists a verification algorithm A and a
constant c, such that
• A verifies L, and
• the worst-case run time of A is O(nc ).
15 How to prove: NP
To prove that your problem L is in NP:
• Decide what to use as the certificate. That is, for each yes instance, explain what a certificate
should be.
• Describe an algorithm whose input is an instance of L and a certificate.
• Show that your algorithm verifies L.
– If it’s a Yes instance and the certificate is correct, it returns Yes.
– Otherwise, it returns No.
∗ If it’s a Yes instance but the certificate is not correct.
∗ If it’s a No instance.
• Show that the worst-case run time of your algorithm is bounded by some polynomial.
16 P ⊆ NP
Every problem in P is also in NP.
Why?
17 NP ⊆ P?
Are there problems in NP that are not in P?
That is: Are all problems that are polynomially verifiable also polynomially solvable?
18 Reductions
We can show relationships between problems using reductions:
20 NP-hard
Definition: A language L is NP-hard if, for every L′ ∈ N P , L′ ≤P L.
21 Watch out!
It is very easy to get the direction of the reduction wrong.
• Double check the direction: Does your reduction algorithm accept an instance of L′ as its
input?
23 NP-complete
Definition: A language that is both NP and NP-hard is called NP-complete.
26 NP-completeness proofs
CLRS3.pdf
Intuition: This is essentially a proof by contradiction. We’re showing that, if we have a polynomial-
time algorithm to decide L2 , then we can use it form a polynomial-time algorithm to decide L1 .
28 Example: 3-SAT
Definition: A Boolean formula in conjunctive normal form is a series of clauses.
• Each clause is an OR of literals (that is, variables or negations of variables).
• The complete formula is the AND of all of the clauses.
(x1 ∨ x2 ∨ x3 ) ∧ (¬x2 ∨ x3 ∨ ¬x4 ) ∧ (¬x1 ∨ x2 ∨ x5 )
3-SAT:
Instance: A CNF formula with 3 literals in each clause.
Question: Is there an assignment of True/False values to the variables that makes the formula
evaluate to True?
Theorem: 3-SAT is NP-complete.
30 VERTEX COVER is in NP
Theorem: VERTEX COVER is in NP.
Proof: Use the set of vertices that covers the graph as the certificate. Verify that there are at most
K vertices (constant time) and that each edge touches at least one of them (linear time).
Proof: Reduction from 3-SAT. Given an arbitrary instance φ of 3-SAT with n variables and m
clauses, form an instance (G, K) of VERTEX COVER as follows.
• Truth-setting components: One vertex for each literal in φ, with each x connected to ¬x.
x1 ¬x1 x2 ¬x2
Idea: A vertex cover must contain at least one vertex from each pair.
¬x2 x3
¬x4
Idea: A vertex cover must select at least two vertices from each of these triangles.
• In clause-satisfaction components, find the first literal set to true, and choose the other two
vertices.
Clearly this set has size n + 2m. Note that it covers all edges (1) within truth-setting components,
(2) within clause-satisfaction components, and (3) between truth-setting and clause-satisfaction
components. Therefore, it is a vertex cover for G.
Proof: Suppose there exists a vertex cover A of G with size n + 2m. By construction, this cover
must include
Let t be the truth assignment that makes xi true iff its truth-setting vertex is in the vertex cover A.
• For each clause C in φ, the corresponding clause-satisfaction component has exactly one
vertex v that is not in the vertex cover.
• Therefore, the literal associated with u is satisfied in t, which implies that C is satisfied.
36 More examples
Additional reduction examples:
• CLRS 34.5
• GJ
• Fenner’s notes
• Erickson’s notes