NP-completeness: how SAT, 3-COLOR, and TSP are secretly the same problem
There is no known efficient algorithm for the Traveling Salesman Problem, graph coloring, or Boolean satisfiability β and it is not for lack of trying. Reductions are the proof technique that shows these problems are all, in a precise sense, the same problem wearing different disguises.
P, NP, and NP-hard
P is the class of decision problems solvable in polynomial time. NP is the class of decision problems whose solutions can be verified in polynomial time β even if finding one might take exponentially long. Every problem in P is trivially in NP (if you can solve it fast, you can verify a solution fast), but whether P = NP is the most famous open problem in computer science.
A problem is NP-hard if every problem in NP can be reduced to it in polynomial time β informally, it's "at least as hard as anything in NP." A problem that is both NP-hard and itself in NP is NP-complete.
P = NP ? open β a $1,000,000 Millennium Prize problem
NP-complete = NP β© NP-hard
Polynomial-time reductions
A polynomial-time reduction from problem A to problem B (written A β€β B) is a polynomial-time algorithm that transforms any instance of A into an instance of B, such that the answer to B's instance tells you the answer to A's instance. The key logical consequence:
Contrapositive: if A is NP-hard and A β€β B, then B is NP-hard too
Reductions transmit hardness "forward" β this is how the entire web of NP-complete problems is built from one seed problem
This is exactly the strategy used to prove new problems are NP-complete: don't design an algorithm from scratch, instead show that a known NP-complete problem reduces to it.
Cook-Levin: SAT is NP-complete
The Cook-Levin theorem (1971) is the seed of the entire reduction tree: it proves that Boolean satisfiability (SAT) is NP-complete directly, by simulating an arbitrary polynomial-time nondeterministic Turing machine's computation history as a giant Boolean formula. Any accepting computation of the machine corresponds to a satisfying assignment, and vice versa.
3-SAT β where every clause has exactly 3 literals β is also NP-complete (SAT reduces to 3-SAT by splitting long clauses with auxiliary variables), and is the most common starting point for reductions because its uniform structure is easy to encode into other combinatorial problems.
(xβ β¨ xΜβ β¨ xβ) β§ (xΜβ β¨ xβ β¨ xΜβ) β§ (xβ β¨ xβ β¨ xΜβ)
Question: does an assignment of xβ,xβ,xβ β {T,F} make every clause true?
Reducing 3-SAT to 3-COLOR
Graph 3-coloring asks: can the vertices of a graph be colored with 3 colors such that no edge connects two vertices of the same color? The classic reduction from 3-SAT builds three gadgets:
- Base triangle: three special vertices T (True), F (False), B (Base), mutually connected, fixing three distinct "reference colors."
- Variable gadget: for each variable xα΅’, a triangle {xα΅’, xΜα΅’, B} forces xα΅’ and xΜα΅’ to take the T or F color β but never the same one, exactly encoding "xα΅’ is true XOR xα΅’ is false."
- Clause gadget (OR-gadget): for each clause (a β¨ b β¨ c), a small 6-vertex gadget connected to literals a, b, c and to the T/F vertices, constructed so that it is 3-colorable if and only if at least one of a, b, c is colored T.
3-SAT formula satisfiable βΊ constructed graph is 3-colorable
Reducing 3-SAT to TSP (via Hamiltonian cycle)
The Traveling Salesman Problem (decision version: "is there a tour of length β€ k?") is usually shown NP-hard in two steps: 3-SAT reduces to Hamiltonian Cycle (does a cycle visiting every vertex exactly once exist?), and Hamiltonian Cycle reduces trivially to TSP.
Step 1 β Hamiltonian Cycle reduces to TSP
Given a graph G, build a complete weighted graph G' on the same vertices: edges present in G get weight 1, all other edges get weight 2. G has a Hamiltonian cycle if and only if G' has a tour of total weight exactly n (using only weight-1 edges):
TSP tour of weight n exists in G' βΊ Hamiltonian cycle exists in G
Step 2 β 3-SAT reduces to Hamiltonian Cycle
This reduction (due to Karp, 1972) is intricate: each variable xα΅’ is represented by a "twisted ladder" gadget with two possible traversal directions (corresponding to xα΅’ = True or False), and each clause is represented by a connector vertex that can only be visited by "borrowing" a pass from one of its three literal gadgets β forcing at least one literal per clause to be traversed in the "satisfying" direction.
| Reduction chain | What it establishes |
|---|---|
| 3-SAT β€β Hamiltonian Cycle | Ham. Cycle is NP-hard |
| Hamiltonian Cycle β€β TSP | TSP (decision) is NP-hard |
| TSP β NP | a candidate tour is checkable in O(n) |
| βΉ TSP is NP-complete |
What NP-completeness means in practice
Proving a problem NP-complete is not a dead end β it's actionable information. It tells you:
- Don't waste time hunting for a polynomial exact algorithm β thousands of researchers have tried on related problems since the 1970s without success.
- An exponential worst-case exact algorithm (branch-and-bound, DPLL for SAT) may still be fine for the instance sizes you actually care about.
- Approximation algorithms with provable guarantees exist for many NP-hard optimization problems (e.g. Christofides' 1.5-approximation for metric TSP).
- Special-case structure (planar graphs, bounded treewidth, small clause density) can make otherwise-hard instances tractable.
Living with NP-hardness: heuristics
For instances too large for exact methods, practitioners reach for heuristics that trade optimality guarantees for speed:
| Problem | Common heuristic | Typical quality |
|---|---|---|
| TSP | Nearest-neighbor + 2-opt | ~5% above optimal |
| TSP | Genetic / Lin-Kernighan | <1% above optimal |
| 3-SAT | WalkSAT (local search) | Solves most practical instances |
| Graph coloring | Greedy + DSATUR ordering | Usually within a few colors of optimal |
π€ Watch heuristics fight NP-hardness live
Compare greedy, 2-opt, and genetic algorithm solutions to the Traveling Salesman Problem