Quora上关于P, NP, NP-complete, and NP-hard问题的解答

来源:互联网 发布:魔女之泉3 知乎 编辑:程序博客网 时间:2024/06/06 01:57

NP问题的概念自己始终没有吃透。国内的博客有不少中文资料,但是写的好的真是很少。今天看到Quora上的关于NP问题的概念介绍,特别拿过来分享给大家,希望能帮助大家加深对该问题的理解。


Quora的关于P,NP等问题解释:

https://www.quora.com/What-are-P-NP-NP-complete-and-NP-hard

网页的pdf版本:

http://pan.baidu.com/s/1kTlO1bL



我不知道国内是否会屏蔽,这里我直接贴上部分原文,里边有不同人的解释。这里贴上两个浏览最多的解释,其他的我放在了一个pdf里,大家可以下载下来查看:


Jessica SuStanford algorithms TA

177.5k Views • Upvoted by Jeff NelsonInvented Chromebook. Former Googler. • Elynn LeeCS major at UT Austin, Google NYC 2013 and 2014 intern, Former Amazon and Facebook intern, research… • Shrey Banga (श्रेय)Platform Engineer at Quora

Jessica has 87 endorsements in Computer Science.

These refer to how long it takes a program to run.  Problems in class P can be solved with algorithms that run in polynomial time.

Say you have an algorithm that finds the smallest integer in an array.  One way to do this is by iterating over all the integers of the array and keeping track of the smallest number you've seen up to that point.  Every time you look at an element, you compare it to the current minimum, and if it's smaller, you update the minimum.

How long does this take?  Let's say there are n elements in the array.  For every element the algorithm has to perform a constant number of operations.  Therefore we can say that the algorithm runs in O(n) time, or that the runtime is a linear function of how many elements are in the array.*  So this algorithm runs in linear time.

You can also have algorithms that run in quadratic time (O(n^2)), exponential time (O(2^n)), or even logarithmic time (O(log n)).  Binary search (on a balanced tree) runs in logarithmic time because the height of the binary search tree is a logarithmic function of the number of elements in the tree.

If the running time is some polynomial function of the size of the input**, for instance if the algorithm runs in linear time or quadratic time or cubic time, then we say the algorithm runs in polynomial time and the problem it solves is in class P.


NP

Now there are a lot of programs that don't (necessarily) run in polynomial time on a regular computer, but do run in polynomial time on a nondeterministic Turing machine.  These programs solve problems in NP, which stands fornondeterministic polynomial time.  A nondeterministic Turing machine can do everything a regular computer can and more.***  This means all problems in P are also in NP.

An equivalent way to define NP is by pointing to the problems that can be verified in polynomial time.  This means there is not necessarily a polynomial-time way to find a solution, but once you have a solution it only takes polynomial time to verify that it is correct.

Some people think P = NP, which means any problem that can be verified in polynomial time can also be solved in polynomial time and vice versa.  If they could prove this, it would revolutionize computer science because people would be able to construct faster algorithms for a lot of important problems.


NP-hard

What does NP-hard mean?  A lot of times you can solve a problem by reducing it to a different problem.  I can reduce Problem B to Problem A if, given a solution to Problem A, I can easily construct a solution to Problem B.  (In this case, "easily" means "in polynomial time.")

If a problem is NP-hard, this means I can reduce any problem in NP to that problem.  This means if I can solve that problem, I can easily solve any problem in NP.  If we could solve an NP-hard problem in polynomial time, this would prove P = NP.


NP-complete

A problem is NP-complete if the problem is both
  • NP-hard, and
  • in NP.



* A technical point: O(n) actually means the algorithm runs in asymptotically linear time, which means the time complexity approaches a line as n gets very large.  Also, O(n) is technically an upper bound, so if the algorithm ran in sublinear time you could still say it's O(n), even if that's not the best description of it.

** Note that if the input has many different parameters, like n and k, it might be polynomial in n and exponential in k

*** Per Xuan Luo's comment, deterministic and nondeterministic Turing machines can compute exactly the same things, since every nondeterministic Turing machine can be simulated by a deterministic Turing machine (a "regular computer").  However, they may compute things in different amounts of time.

0 0
原创粉丝点击