NP-complete problems
NP-полные задачи
These are yes-or-no problems where a proposed proof of a “yes” answer can be checked quickly. A polynomial-time algorithm for any one of them would give a polynomial-time algorithm for every problem in NP.
Also known as: P vs NP
The idea
Suppose a delivery driver must visit every stop exactly once and return to the depot. Given a route, it’s easy to check that it visits all the stops and stays within a distance budget. Finding such a route from scratch is a different job. The question “Is there a tour of length at most K?” is the decision version of the traveling salesperson problem (TSP), a classic NP-complete problem.
What makes a problem NP-complete?
- P contains decision problems (questions with a yes-or-no answer) that an algorithm can solve in time polynomial in the input size.
- NP contains decision problems for which every yes instance has a polynomial-size certificate that an algorithm can verify in polynomial time. In the tour example, the certificate is an ordered list of stops.
- NP-hard means every problem in NP can be reduced to the problem in polynomial time. NP-complete means both NP-hard and in NP.
A reduction from A to B turns an input for A into an input for B, preserving the yes-or-no answer. If the transformation takes polynomial time and B has a polynomial-time solver, then A does too. That’s why putting even one NP-complete problem in P would prove that P = NP. Whether P equals NP is still open; “P vs NP” is the name of that question, not another name for NP-complete problems.
Example: where the hardness comes from
Start with Hamiltonian Cycle: given an undirected graph, can you visit every vertex exactly once and return to the start using only its edges? Build a TSP instance on the same n vertices. Give each original edge weight 1 and each previously missing edge weight 2, and set the tour budget to n. This construction takes polynomial time. A tour uses exactly n edges, so it costs at most n exactly when all its edges were in the original graph. Since Hamiltonian Cycle is already known to be NP-complete, the reduction shows that decision TSP is NP-hard. Checking a proposed tour’s visits and total cost puts it in NP as well.
What this means in practice
NP-completeness is a worst-case classification. It doesn’t tell you how hard a particular input will be, or prove that exponential time is necessary. Small instances may be manageable with exact search, and a restricted version can be much easier: if the stops lie on a line and travel cost is ordinary distance, the optimal round trip just needs to reach both extremes and return. For large routing instances, teams often use heuristics when a good tour is enough and they don’t need proof that it’s the shortest.
How it comes up in interviews
Separate the decision problem (“Is any tour within budget?”) from the optimization problem (“What is the shortest tour?”). The former is NP-complete; the latter is NP-hard and isn’t itself a yes-or-no problem. To prove a new decision problem NP-complete, describe a polynomial-time verifier, then reduce a known NP-complete problem to the new problem, not the other way around. Show that the transformation preserves both yes and no answers.