P, NP and hard problems
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Some problems are easy to check but seem hard to solve. Take subset sum on four numbers: check a proposed answer in one pass, then watch brute force count through every pick, 2ⁿ of them. From that one example come the classes P and NP, why P sits inside NP, what makes a problem NP-complete, and why P = NP is still an open question. The lesson "Comparing growth rates: O, Omega and Theta" covers the bounds used here, and "The knapsack problem" covers the table that solves subset sum when the target is small.
A yes-or-no puzzle
Subset sum: here are 4 numbers, 3, 5, 6, 7. Can you choose some of them that add up to exactly t = 15? The answer is yes or no, so this is a decision problem. Those are the problems P and NP talk about.
nums = [3, 5, 6, 7] n = 4 t = 15 question: is there a subset of nums with sum exactly t? answer: YES or NO (a decision problem)
اقرأ الدرس خطوة بخطوة
A yes-or-no puzzle
Subset sum: here are 4 numbers, 3, 5, 6, 7. Can you choose some of them that add up to exactly t = 15? The answer is yes or no, so this is a decision problem. Those are the problems P and NP talk about.
nums = [3, 5, 6, 7] n = 4 t = 15 question: is there a subset of nums with sum exactly t? answer: YES or NO (a decision problem)Someone hands you a pick
A friend says yes and hands you a pick: one bit per number, 1 for use it, 0 for skip it. Their pick is 1, 1, 0, 1. A pick like this, offered as proof of a yes, is called a certificate.
pick = (1, 1, 0, 1) nums[0] = 3 1 use it nums[1] = 5 1 use it nums[2] = 6 0 skip it nums[3] = 7 1 use it certificate: a short proof that the answer is YESChecking takes one pass
check() walks the numbers once and adds the picked ones: s goes 3, 8, 8, 15. s equals t, so the pick is right. That is 4 loop passes for 4 numbers: checking costs O(n) steps.
for x, p in zip(nums, pick): x = 3 p = 1 s = 3 x = 5 p = 1 s = 8 x = 6 p = 0 s = 8 x = 7 p = 1 s = 15 return s == t -> 15 == 15 -> True 4 loop passes: O(n)Finding means trying picks
With no pick handed over, solve() tries them all. product counts through picks like an odometer, rightmost bit fastest. There are 2^4 = 16 picks. Here the 14th, 1, 1, 0, 1, is the first that check() accepts.
for pick in product((0, 1), repeat=n): try 1 (0, 0, 0, 0) s = 0 try 2 (0, 0, 0, 1) s = 7 try 3 (0, 0, 1, 0) s = 6 try 4 (0, 0, 1, 1) s = 13 try 5 (0, 1, 0, 0) s = 5 try 6 (0, 1, 0, 1) s = 12 try 7 (0, 1, 1, 0) s = 11 try 8 (0, 1, 1, 1) s = 18 try 9 (1, 0, 0, 0) s = 3 try 10 (1, 0, 0, 1) s = 10 try 11 (1, 0, 1, 0) s = 9 try 12 (1, 0, 1, 1) s = 16 try 13 (1, 1, 0, 0) s = 8 try 14 (1, 1, 0, 1) s = 15 == t returnEach number doubles the work
Every extra number doubles the picks: 2^n. With 20 numbers that is 1,048,576. With 60 it is about 1.2 × 10^18, some 36 years at a billion picks a second. Checking one pick still takes only n steps.
n picks to try (2^n) check() 4 16 4 steps 20 1,048,576 20 steps 40 1.1 × 10^12 40 steps 60 1.2 × 10^18 60 steps 2^60 picks at 10^9 a second ≈ 36.5 years brute force is exponential; checking is polynomialP: solved fast
P is the set of decision problems some algorithm solves in polynomial time: O(n^k) steps for a fixed k. Example: is there a path from s to t of length at most d? Dijkstra answers that fast, so it is in P.
P = decision problems solvable in poly(n) time poly(n): O(n^k) for some constant k example: s-t shortest path ≤ d (Dijkstra, BFS) not polynomial: 2^n, n!NP: checked fast
NP is the set of decision problems where every yes has a certificate that a polynomial-time verifier accepts, and no certificate fools it on a no. Subset sum is in NP: check() is that verifier.
NP = decision problems with a poly-time verifier V(I, c): YES input -> some certificate c makes V say YES NO input -> no certificate makes V say YES subset sum: c = the pick, V = check(), O(n) NP stands for nondeterministic polynomial timeP sits inside NP
Every problem in P is also in NP: a verifier can ignore the certificate and solve the problem itself, still in polynomial time. So P sits inside NP. Brute force also shows NP problems are solvable in exponential time.
P ⊆ NP: V(I, c) ignores c and runs the P algorithm NP ⊆ EXP: try every certificate (at most 2^poly(n)) subset sum: in NP; nobody knows a poly-time solve()NP-complete: the hardest
A problem is NP-hard if every NP problem reduces to it: converts into it in polynomial time. NP-complete means NP-hard and in NP. Subset sum is NP-complete: Karp proved it in 1972, under the name knapsack.
reduction A -> B: turn any A input into a B input in poly time, with the same YES/NO answer NP-hard: every problem in NP reduces to it NP-complete = NP ∩ NP-hard subset sum, SAT, 3-colouring, longest path: NP-completeThe open question
If any NP-complete problem had a polynomial algorithm, every NP problem would, and P would equal NP. Nobody has found one or proved it impossible. Most experts think P ≠ NP. A proof either way wins $1 million.
poly solve() for one NP-complete problem -> reduce any NP problem to it -> P = NP open since Cook and Levin posed it (1971) Clay Millennium Prize: $1,000,000 consensus: P ≠ NP, unproved
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01nums = [3, 5, 6, 7] and t = 15. Someone hands you the pick (1, 1, 0, 1). How many loop passes does check() make?
check() looks at each number once, so it makes n = 4 passes and finds s = 3 + 5 + 7 = 15. Checking costs O(n). 14 is how many picks solve() tries before it finds this one, and 16 is the number of picks in total.
02With t = 4 instead, no pick of [3, 5, 6, 7] adds up to t. How many picks does solve() try before it returns None?
On a no input brute force cannot stop early: it tries all 2⁴ = 16 picks. Each extra number would double that.
03Which statement defines NP?
NP is about checking: every yes has a short proof a fast verifier accepts, and no proof fools it on a no. NP does not mean "not polynomial"; the third option is NP-hard.
04Why is every problem in P also in NP?
A problem in P has a polynomial algorithm. Use it as the verifier and throw the certificate away: that is a polynomial-time verifier, so the problem is in NP.
05Suppose someone finds a polynomial-time algorithm for subset sum. What follows?
Subset sum is NP-complete: every NP problem reduces to it in polynomial time. Reduce, then run the fast algorithm, and every NP problem is solved in polynomial time. Nobody knows such an algorithm.
المسرد
- Decision problem
- A problem whose answer is yes or no, such as "is there a subset of these numbers that adds up to t?".
- Certificate
- A short piece of evidence that the answer is yes, such as the pick of numbers that adds up to t.
- Verifier
- An algorithm that takes an input and a certificate and checks the certificate. check() is the verifier for subset sum.
- Polynomial time
- At most c·n^k steps for some constants c and k, where n is the input size.
- P
- The decision problems some algorithm solves in polynomial time.
- NP
- The decision problems whose yes answers have a certificate that a polynomial-time verifier accepts. Short for nondeterministic polynomial time.
- Reduction
- A polynomial-time way to turn any input of problem A into an input of problem B with the same yes or no answer.
- NP-complete
- In NP, and every problem in NP reduces to it (NP-hard). Subset sum, SAT and 3-colouring are examples.
الملاحظات والمصادر
- P, NP, NP-hard and NP-complete follow MIT 6.006 (Spring 2020) lecture 19 and CLRS chapter 34. They are defined for decision problems; the size n is the length of the input written in bits, which for a list of small numbers is about the number of items.
- Subset sum has a dynamic-programming solution that takes about n·t steps (the knapsack problem uses the same table). That is fast when t is small, but t written in b bits can be as large as 2^b, so n·t is not polynomial in the input size. It is called pseudo-polynomial.
- itertools.product((0, 1), repeat=n) yields the picks like an odometer, rightmost bit fastest: (0, 0, 0, 0), (0, 0, 0, 1), and so on. With nums = [3, 5, 6, 7] and t = 15, the 14th pick, (1, 1, 0, 1), is the first and only one that works. With t = 4 no pick works, and solve() tries all 16 before returning None.
- NP does not mean "not polynomial". It stands for nondeterministic polynomial time: a machine that could guess the right certificate would only need polynomial time to check it.
- NP-hard problems need not be in NP at all, and a hard worst case does not make every input hard: solvers handle many large real instances of NP-complete problems.
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 19: Complexity
- Sedgewick and Wayne, Algorithms 4th ed., §6.6 Intractability
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 34: NP-Completeness
- Clay Mathematics Institute: P vs NP
- Clay Mathematics Institute: The Millennium Prize Problems
- Karp's 21 NP-complete problems (Karp, "Reducibility Among Combinatorial Problems", 1972)
- Python documentation: itertools.product
اختبار يتاح بعد الخطوة الأخيرة.