Sub topic 11
A Tour of Classic NP-Complete Problems
This chapter assumes you know the definitions of NP and NP-completeness and the Cook-Levin result that SAT is NP-complete. You should also be able to read simple Python and basic graph terminology.
Once SAT was known to be NP-complete, proving other problems NP-complete became much easier. To show a new problem X is NP-complete, you show X is in NP and reduce a known NP-complete problem to X. In 1972, Richard Karp used this method to show that 21 well-known problems are NP-complete. The ones below come up most often.
The Core Problems
| Problem | Input | Question |
|---|---|---|
| 3-SAT | CNF formula, exactly 3 literals per clause | Is there a satisfying assignment? |
| Independent Set | Graph G, integer k | Are there k vertices with no edge between any two of them? |
| Vertex Cover | Graph G, integer k | Are there k vertices that touch every edge? |
| Clique | Graph G, integer k | Are there k vertices that are all pairwise adjacent? |
| 3-Coloring | Graph G | Can vertices get 3 colors so that no edge joins two vertices of the same color? |
| Hamiltonian Cycle | Graph G | Is there a cycle that visits every vertex exactly once? |
| Traveling Salesman (decision) | Weighted graph, bound B | Is there a tour of total weight at most B? |
| Subset Sum | Integers, target t | Does some subset sum to exactly t? |
3-SAT matters most as a starting point. Its clauses have a fixed, uniform shape, which makes it easy to build "gadgets" from them in other problems.
How They Connect
Each arrow points from a known-hard problem to the problem being proved hard. Every problem in the diagram is also in NP, because a proposed solution can be checked quickly. Here is a verifier for Hamiltonian Cycle:
def verify_ham_cycle(adj, order):
"""adj: dict vertex -> set of neighbors; order: proposed cycle as a vertex list."""
if len(order) != len(adj) or set(order) != set(adj):
return False
return all(order[(i + 1) % len(order)] in adj[order[i]]
for i in range(len(order)))
square = {1: {2, 4}, 2: {1, 3}, 3: {2, 4}, 4: {3, 1}}
print(verify_ham_cycle(square, [1, 2, 3, 4])) # True
print(verify_ham_cycle(square, [1, 3, 2, 4])) # FalseCommon Mistakes: Look-Alikes in P
A small change to a problem can move it from NP-complete into P. Learners often mix up these pairs:
| NP-complete | Similar problem in P |
|---|---|
| 3-SAT | 2-SAT (linear time, using strongly connected components) |
| 3-Coloring | 2-Coloring (breadth-first search) |
| Hamiltonian Cycle (visit each vertex once) | Eulerian Cycle (use each edge once) |
| Longest simple path | Shortest path (Dijkstra) |
| Max cut | Min cut (max-flow) |
Exercise
You want to prove that a new scheduling problem is NP-complete. You have shown it is in NP. Should you reduce your problem to 3-SAT, or 3-SAT to your problem? Why?
Show answer
Reduce 3-SAT to your problem. If your problem were solvable in polynomial time, the reduction would then solve 3-SAT in polynomial time too. That makes your problem at least as hard as 3-SAT. A reduction in the other direction only shows that your problem is no harder than 3-SAT.
Exercise
Is deciding whether a graph has a cycle that uses every edge exactly once NP-complete?
Show answer
No. That is the Eulerian Cycle problem. A connected graph has one exactly when every vertex has even degree, which you can check in polynomial time.