Module A-12·18 min read

Why some problems have no fast solution — P, NP, NP-Complete, and how production systems cope with intractability.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module A-12 — P vs NP: Recognizing Intractable Problems

What this module covers: Every algorithm in this course so far has had a fast solution — or a fast enough one. This module covers what happens when that stops being true: a class of problems where no fast algorithm is known to exist, and where the honest, production-grade response isn't "look harder for a clever trick" — it's recognizing the category and switching strategies entirely.


P: Problems With a Fast Solution

P (polynomial time) is every problem this course has already solved efficiently: sorting (Modules P-9, P-10 — O(n log n)), searching (Module F-11 — O(log n)), shortest paths (Modules A-6, A-7 — O((V+E) log V) or O(V·E)), tree/graph traversal (Modules P-4, A-4, A-5 — O(V + E)). "Polynomial" means the cost is bounded by n raised to some fixed power (n, n², n log n) — it grows, but predictably and manageably as input size increases.


NP: Problems That Are Easy to Check, Even If Hard to Solve

NP (Nondeterministic Polynomial) is a different category: problems where, if someone hands you a proposed solution, you can verify it's correct quickly (in polynomial time) — even if finding that solution in the first place might be extremely hard.

typescript

Checking a specific proposed route against a budget is easy — O(n). Finding the actual cheapest route among all possible orderings, with no proposed answer to check, is a completely different problem: there are (n − 1)!/2 distinct possible routes for n cities, and without more structure to exploit, checking every single one is the only way to be certain you've found the true optimum. This gap — trivial to verify, seemingly impossible to efficiently solve — is the defining shape of NP.


NP-Complete: The Hardest Problems in NP

NP-Complete problems are, in a precise sense, the hardest problems in NP: they're all equally hard, in the specific sense that if you found a genuinely fast (polynomial-time) algorithm for even one NP-Complete problem, that algorithm could be adapted (via a "reduction") to solve every NP-Complete problem quickly. The Traveling Salesman Problem, Boolean Satisfiability (SAT), Graph Coloring, and — directly relevant to this course — 0/1 Knapsack are all NP-Complete.

This is worth connecting directly back to Module P-13: 0/1 knapsack's exact dynamic programming solution runs in O(n × capacity) time — which looks polynomial, but capacity is a number, not the size of the input as typically measured (the number of bits needed to represent that number). This is called pseudo-polynomial time — fast when capacity is small, but capable of becoming effectively exponential relative to the actual size of the input in bits when capacity is astronomically large (say, a capacity value in the billions represented by just a handful of digits/bits). This is precisely why 0/1 knapsack is still classified as NP-Complete despite Module P-13's DP solution existing — that solution's performance depends on the numeric magnitude of the input, not just how many characters it takes to write the input down.


NP-Hard: At Least as Hard, Possibly Harder

NP-Hard problems are at least as hard as the hardest NP problems, but might not even be in NP — meaning even verifying a proposed solution might not be fast. The classic example: the Halting Problem — determine whether an arbitrary program will eventually stop running or loop forever — is proven to have no algorithmic solution at all, for any input, ever, no matter how much time is allowed. NP-Hard is a strictly broader category than NP-Complete; every NP-Complete problem is NP-Hard, but not every NP-Hard problem is in NP.


The Open Question: Does P = NP?

Nobody has ever found a genuinely polynomial-time algorithm for any NP-Complete problem — but nobody has proven one can't exist, either. This is the famous, unsolved P vs NP question, one of the most significant open problems in computer science (with a $1 million prize attached). Most computer scientists believe P ≠ NP — that these problems really do require fundamentally more time than polynomial algorithms can offer — but this remains formally unproven.


Why This Matters: Stop Looking for a Faster Exact Algorithm

The practical payoff of recognizing "this is NP-Complete" isn't giving up — it's redirecting effort. If a problem is provably NP-Complete, spending more engineering time searching for a clever polynomial-time exact algorithm is very likely wasted effort (every top researcher who's tried has failed, for every problem in this class, for decades). The productive response is choosing a different kind of solution:

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.