ENع
→P vs NP
الدرس S5 · Special topics

P, NP and hard problems

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل S5 · تفاعلي
01 / 10

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)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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)
  2. 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 YES
  3. Checking 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)
  4. 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  return
  5. Each 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 polynomial
  6. P: 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!
  7. 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 time
  8. P 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()
  9. 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-complete
  10. The 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

اختبار يتاح بعد الخطوة الأخيرة.