Among NP problems some are the hardest of all. They are linked so that a fast solution to any one would give a fast solution to every problem in NP.
Reductions
A problem A is polynomial-time reducible to a problem B, written A ≤ₚ B, if there is a polynomial-time algorithm converting any instance of A into an instance of B so that the answers agree (yes maps to yes, no to no). Then a fast algorithm for B gives a fast algorithm for A. Reduction thus shows that B is at least as hard as A. The relation is transitive.
NP-Hard and NP-Complete
A problem is NP-hard if every problem in NP reduces to it in polynomial time; it is at least as hard as every NP problem, but it need not itself be in NP (it might not even be a decision problem). A problem is NP-complete if it is both in NP and NP-hard: it is among the hardest problems in NP. Hence NP-complete = NP ∩ NP-hard. If any NP-complete problem had a polynomial algorithm, then P = NP; and if any NP-complete problem is proved to need exponential time, then P ≠ NP.
Definition: NP-complete problem. A problem that belongs to NP and to which every other NP problem can be reduced in polynomial time; the hardest problems in NP.
Cook's Theorem and the First Problem
To prove a problem NP-complete one needs a first one. In 1971 Stephen Cook (and independently Leonid Levin) proved that the Boolean satisfiability problem (SAT) is NP-complete: whether there is a truth assignment making a given Boolean formula true. Once SAT is known to be NP-complete, other problems are proved NP-complete by showing (1) they are in NP and (2) a known NP-complete problem reduces to them. In 1972 Richard Karp used this to list 21 NP-complete problems.
A Chain of Reductions
SAT ≤ₚ 3-SAT ≤ₚ Clique ≤ₚ Vertex Cover ≤ₚ Hamiltonian Circuit ≤ₚ Travelling Salesman (decision form); also 3-SAT ≤ₚ Subset Sum ≤ₚ Partition and Knapsack. So all of these are NP-complete. Examples of NP-hard problems that are not decision problems are the optimisation versions: find the shortest tour (TSP) or the minimum vertex cover. The halting problem is NP-hard but not in NP.
Coping with NP-Hard Problems
Since no fast exact algorithm is known, we use: backtracking and branch and bound for small instances; approximation algorithms with a guaranteed quality (the vertex cover 2-approximation); heuristics such as local search, genetic algorithms and simulated annealing, with no guarantee; special cases that are easy (for example, vertex cover on trees); and randomisation or fixed-parameter methods.
Table 4.6: Classes of problems
| Class | In NP? | Hardness | Examples |
|---|---|---|---|
| P | Yes | Easy | Sorting, MST |
| NP-complete | Yes | NP-hard | SAT, Hamiltonian circuit, subset sum |
| NP-hard | Maybe not | At least as hard as NP-complete | TSP optimisation, halting problem |
Example 4.6: Reasoning with reductions
Problem: A new problem X is in NP, and SAT ≤ₚ X. What can be said about X? What if a polynomial algorithm for X were found?
Solution:
- SAT is NP-complete, so every NP problem reduces to SAT, and by transitivity to X: X is NP-hard.
- X is also in NP, so X is NP-complete.
- If X had a polynomial algorithm, every NP problem could be solved by reducing to X and running that algorithm, so P = NP.
Result: X is NP-complete; a polynomial algorithm for X would prove P = NP.
from itertools import product
clauses = [(1, -2, 3), (-1, 2), (2, 3), (-3, 1)]
def sat(assign):
return all(any((lit > 0) == assign[abs(lit) - 1] for lit in c)
for c in clauses)
sols = [a for a in product([False, True], repeat=3) if sat(a)]
print("assignments tried: 8, satisfying:", len(sols))
print("first:", sols[0] if sols else None)
print("verification of one guess is fast; search is 2^n")
Output
assignments tried: 8, satisfying: 2
first: (True, True, False)
verification of one guess is fast; search is 2^n
Common mistakes
- Saying NP-hard and NP-complete mean the same; NP-hard problems need not be in NP.
- Proving NP-completeness by reducing the new problem to a known one; the reduction must go from the known problem to the new one.
- Assuming NP-complete means impossible; small instances and approximations are often fine.
- Forgetting to show membership in NP in an NP-completeness proof.
Key takeaways
- A ≤ₚ B means a fast algorithm for B gives one for A.
- NP-hard: every NP problem reduces to it; NP-complete: NP-hard and in NP.
- SAT was the first NP-complete problem (Cook, 1971).
- A polynomial algorithm for one NP-complete problem would give P = NP.
Practice questions
- What is the difference between NP-hard and NP-complete?
- Which problem did Cook prove NP-complete?
- In which direction must a reduction go to prove a new problem NP-complete?
Answers to the practice questions
- NP-complete problems are NP-hard and also in NP; NP-hard problems need not be in NP.
- The Boolean satisfiability problem (SAT).
- From a known NP-complete problem to the new problem.
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit IV
