Sub topic 11
A Tour of Classic NP-Complete Problems
This chapter assumes you know the definitions of P, NP, NP-hard, and NP-complete from earlier chapters. It also assumes you know that SAT is NP-complete by the Cook-Levin theorem, and that you can read Python and basic graph terminology. The goal here is recognition. When a problem from work lands on your desk, you should be able to see that it looks like one of these classics, and know which small change to it would make it easy.
Where the Classic List Comes From
Cook-Levin gave the field one NP-complete problem. A year later, in 1972, Richard Karp showed that 21 well-known combinatorial problems were NP-complete. He did it by chaining polynomial-time reductions out of SAT. Each new proof needed only one reduction from a problem already known to be NP-complete. Garey and Johnson's 1979 book extended the list to hundreds of problems, and it is still the standard reference.
The reductions form a tree rooted at SAT. You do not need to memorize the proofs. You do need to know the shape of the tree, because a new hardness proof usually starts from whichever classic problem looks most like your own.
The Problems Grouped by Family
Logic: SAT and 3-SAT
SAT asks whether a Boolean formula in conjunctive normal form (CNF) has a satisfying assignment. 3-SAT restricts every clause to exactly three literals. 3-SAT is still NP-complete. That matters because its uniform structure makes it much easier to build reduction gadgets from. Most hardness proofs in the literature start from 3-SAT, not from general SAT.
Graph Subsets: Independent Set, Clique, Vertex Cover
- Independent Set: given a graph G and an integer k, is there a set of k vertices with no edge between any two of them?
- Clique: is there a set of k vertices in which every pair is joined by an edge?
- Vertex Cover: is there a set of k vertices that touches every edge?
These three problems are rewrites of one another. A clique in G is an independent set in the complement graph, which has an edge exactly where G does not. The next chapter works through the reduction from Independent Set to Vertex Cover in detail. Set Cover generalizes Vertex Cover. You are given a universe of elements and a collection of subsets, and you ask whether k subsets can cover every element.
Graph Coloring
3-Coloring asks whether you can give each vertex one of three colors so that no edge joins two vertices of the same color. It stays NP-complete even on planar graphs. Planar 4-coloring is trivial because the Four Color Theorem says the answer is always yes. 2-coloring can be solved in O(V + E) time with breadth-first search, as an earlier chapter showed.
Tours: Hamiltonian Cycle and TSP
Hamiltonian Cycle asks whether some cycle visits every vertex exactly once. The decision version of the Traveling Salesperson Problem (TSP) gives you edge weights and a bound B and asks whether a tour of total weight at most B exists. Hamiltonian Cycle reduces to TSP directly. Give every edge of G weight 1 and every non-edge weight 2, then ask for a tour of weight at most n.
Numbers: Subset Sum, Partition, Knapsack
Subset Sum asks whether some subset of a list of integers adds up exactly to a target. Partition asks whether the list splits into two halves with equal sums. Knapsack (decision) asks whether some set of items fits within a weight limit and reaches at least a given value. These number problems are NP-complete only when the numbers are large compared with the input size. A later chapter covers this distinction under the name pseudo-polynomial time.
Summary Table
| Problem | Input | Yes-certificate | Similar problem in P |
|---|---|---|---|
| 3-SAT | CNF, 3 literals per clause | Truth assignment | 2-SAT |
| Independent Set | Graph G, integer k | k vertices | Independent Set on bipartite graphs |
| Vertex Cover | Graph G, integer k | k vertices | Vertex Cover on bipartite graphs (König's theorem) |
| Clique | Graph G, integer k | k vertices | Clique with fixed k, such as triangle detection |
| 3-Coloring | Graph G | Color for each vertex | 2-Coloring |
| Hamiltonian Cycle | Graph G | Vertex ordering | Eulerian circuit (uses every edge once) |
| TSP (decision) | Weighted graph, bound B | Vertex ordering | Minimum spanning tree |
| Subset Sum | Integers, target T | Index set | Subset Sum with small T, by dynamic programming |
| 3D Matching | Triples over three sets | Disjoint triples | Bipartite matching |
The last column is the most useful part of the table in practice. Many easy problems sit right next to an NP-complete one. The smallest change to a problem, such as moving from 2 literals per clause to 3, or from visiting every edge to visiting every vertex, can be the change that makes it intractable.
Verifiers for Two More Problems
Every problem on this list is in NP, so each one has a short certificate that can be checked in polynomial time. The code below gives verifiers for 3-Coloring and Hamiltonian Cycle. It also shows the complement-graph mapping from Clique to Independent Set.
from itertools import combinations
def verify_coloring(adj, coloring, k):
"""adj: dict vertex -> list of neighbors. coloring: dict vertex -> int."""
if set(coloring) != set(adj):
return False
if any(c not in range(k) for c in coloring.values()):
return False
return all(coloring[u] != coloring[v] for u in adj for v in adj[u])
def verify_hamiltonian_cycle(adj, order):
"""order: list of vertices proposed as a cycle, returning to order[0]."""
n = len(order)
if n < 3 or n != len(adj) or set(order) != set(adj):
return False
return all(order[(i + 1) % n] in adj[order[i]] for i in range(n))
def is_independent_set(edges, subset):
subset = set(subset)
return not any(u in subset and v in subset for u, v in edges)
def clique_to_independent_set(vertices, edges, k):
"""Maps a Clique instance (G, k) to an Independent Set instance (complement of G, k)."""
edge_set = {frozenset(e) for e in edges}
complement = [(u, v) for u, v in combinations(vertices, 2)
if frozenset((u, v)) not in edge_set]
return vertices, complement, k
g = {'a': ['b', 'c'], 'b': ['a', 'c'], 'c': ['a', 'b', 'd'], 'd': ['c']}
print(verify_coloring(g, {'a': 0, 'b': 1, 'c': 2, 'd': 0}, 3)) # True
print(verify_coloring(g, {'a': 0, 'b': 1, 'c': 2, 'd': 0}, 2)) # False
square = {1: [2, 4], 2: [1, 3], 3: [2, 4], 4: [3, 1]}
print(verify_hamiltonian_cycle(square, [1, 2, 3, 4])) # True
print(verify_hamiltonian_cycle(square, [1, 3, 2, 4])) # False: 1-3 is not an edge
verts = ['a', 'b', 'c', 'd']
edges = [('a', 'b'), ('b', 'c'), ('a', 'c'), ('c', 'd')]
_, comp, k = clique_to_independent_set(verts, edges, 3)
print(comp) # [('a', 'd'), ('b', 'd')]
print(is_independent_set(comp, ['a', 'b', 'c'])) # True: {a, b, c} is a clique in GCommon Mistakes When Recognizing These Problems
- Arguing hardness from looks. Longest simple path is NP-hard. Shortest path is in P. Two problems that look alike can sit in different classes, so only a reduction settles the question.
- Reducing in the wrong direction. To prove your problem X is NP-hard, reduce a known NP-complete problem to X. Reducing X to SAT only shows X is in NP, or easier.
- Treating a fixed parameter as part of the input. "Does G have a clique of size 4?" can be solved in O(n⁴) time, which is polynomial. Clique is NP-complete only when k is part of the input.
- Forgetting that the numbers' encoding matters. Subset Sum with targets in the hundreds is easy in practice. The hardness depends on targets with many bits.
Exercise
A scheduler must give each of n exams one of three time slots so that no two exams that share a student get the same slot. Which classic problem is this, and is the decision version NP-complete? What happens if only two slots are available?
Show answer
Make each exam a vertex and add an edge between two exams whenever they share a student. The question becomes whether this graph can be 3-colored, which is NP-complete. Any graph can be the conflict graph, so 3-Coloring reduces to the scheduler's problem. With two slots it becomes 2-Coloring, and breadth-first search solves it in O(V + E) time.
Exercise
Explain why the mapping "edges get weight 1, non-edges get weight 2, bound B = n" is a correct reduction from Hamiltonian Cycle to TSP decision.
Show answer
If G has a Hamiltonian cycle, that tour uses n edges of weight 1, so its total weight is n ≤ B. In the other direction, every tour has exactly n edges, each of weight at least 1. A tour of weight at most n can therefore use only weight-1 edges, which are edges of G, so it is a Hamiltonian cycle in G. Building the weight matrix takes O(n²) time, which is polynomial.
Exercise
Is "Does a graph have a vertex cover of size at most 5?" NP-complete when 5 is fixed and not part of the input?
Show answer
No, unless P = NP. Trying all O(n⁵) subsets of size at most 5 and checking each one in polynomial time gives a polynomial-time algorithm. Vertex Cover is NP-complete only when k is part of the input.