Sub topic 18
Handling NP-Hard Problems in Real Systems
This chapter assumes you know what NP-hard and NP-complete mean, that you have seen Vertex Cover and SAT, and that you understand the difference between pseudo-polynomial and polynomial time. An NP-hardness proof does not mean you should stop working on the problem. It means you should stop looking for an algorithm that is exact, fast on every input, and fully general. Real systems keep two of those three properties and give up the third.
The Main Strategies
| Strategy | What you give up | Typical example |
|---|---|---|
| Approximation algorithm | Exact optimality, in exchange for a proven bound | 2-approximation for Vertex Cover |
| Heuristics and local search | Any guarantee on quality or running time | Simulated annealing for scheduling |
| Special cases | Generality | 2-coloring, trees, bounded treewidth |
| Pseudo-polynomial or parameterized algorithms | Speed when numbers or parameters get large | Subset Sum DP when target values are small |
| SAT/ILP solvers | A worst-case time bound | Hardware verification, package dependency resolution |
Approximation With a Guarantee
An approximation algorithm runs in polynomial time and returns a solution within a proven factor of the optimum. For Vertex Cover, the algorithm below picks any uncovered edge and adds both of its endpoints to the cover. The chosen edges share no endpoints, so they form a matching. Any cover must contain at least one endpoint of each matched edge. The result is therefore at most twice the optimal size.
def vertex_cover_2approx(edges):
"""Returns a vertex cover at most twice the minimum size."""
cover = set()
for u, v in edges:
if u not in cover and v not in cover:
cover.add(u)
cover.add(v)
return cover
edges = [(1, 2), (1, 3), (2, 4), (3, 4), (4, 5)]
cover = vertex_cover_2approx(edges)
print(cover) # {1, 2, 3, 4}; the optimum {1, 4} has size 2
print(all(u in cover or v in cover for u, v in edges)) # TrueNot every NP-hard problem can be approximated this well. General TSP has no constant-factor approximation unless P = NP, so check the known approximation results for your problem before you promise users any quality level.
Exploit the Structure of Your Inputs
Hardness results describe the worst case over all inputs, and your inputs may be much easier. Graphs that are trees, bipartite, or planar often allow polynomial algorithms. A small parameter can also help. For example, Vertex Cover with cover size k can be solved in O(2^k · n) time, which is practical when k stays small even if n is large.
Hand It to a Solver
Modern SAT solvers based on conflict-driven clause learning (CDCL) routinely handle industrial formulas with millions of clauses. You can encode your problem as CNF, or as an integer linear program for an ILP solver, and let tuned search do the work. Cook-Levin explains why this is always possible: every NP problem reduces to SAT.
Common Mistakes
- Treating "NP-hard" as "infeasible." Many NP-hard instances that come up in practice solve in seconds.
- Reporting a heuristic's result as optimal. A heuristic has no quality guarantee, so describe its output as a good solution, not the best one.
- Forgetting the timeout. Without one, a single hard instance can stall the whole pipeline.
Exercise
The 2-approximation above returns {1, 2, 3, 4} for the example graph. Explain why it can never return a cover more than twice the optimal size.
Show answer
The edges whose endpoints get added share no vertices, so they form a matching M. Every vertex cover, including an optimal one, must contain at least one endpoint of each edge in M, so the optimum is at least |M|. The algorithm returns exactly 2|M| vertices, which is at most twice the optimum.
Exercise
You must schedule 50 jobs with strict deadlines each night, and an exact answer is required. Which strategy fits best, and what safeguard should you add?
Show answer
Encode the problem for a SAT or ILP solver, since 50 jobs is a moderate size that solvers usually handle quickly. Add a timeout with a defined fallback, such as returning the best feasible schedule found so far or raising an alert.