最近因为要证明np问题,所以找了一系列概念去理解这4个问题。理解的时候看到好多人给出了不同的答案,我下面会借鉴别人的答案来总结出一份对于我自己来说,最容易理解这4个问题的说法。
这部分内容的参考链接
表明问题规模扩大后,程序需要的时间长度增长得有多快。程序的时间复杂度一般可以分为两种级别:
(个人感想:这个对于程序员或者学数学或其他的来说不陌生,从程序的角度出发,就是一个程序运行所需要的时间和输入size存在的一个关于时间上的关系。)
- 多项式级的复杂度,如O(1),O(log(n))、O(n^a)等,
- 非多项式级的,如O(a^n)、O(n!)等。后者的复杂度计算机往往不能承受。
(个人感想:这个理解是我看过那么多概念觉得最能理解的,最简洁的。约化是这4个问题之间相互转换的关键)
简单的说,一个问题A可以约化为问题B的含义是,可以用问题B的解法解决问题A。(个人感觉也就是说,问题A是B的一种特殊情况。)标准化的定义是,如果能找到一个变化法则,对任意一个A程序的输入,都能按照这个法则变换成B程序的输入,使两程序的输出相同,那么我们说,问题A可以约化为问题B。
例如求解一元一次方程这个问题可以约化为求解一元二次方程,即可以令对应项系数不变,二次项的系数为0,将A的问题的输入参数带入到B问题的求解程序去求解。
另外,约化还具有传递性,A可以化约为B,B可以约化为C,那么A也可以约化为C。
(个人感想:这个概念也是很重要的,我觉得对于理解是不可忽视的一个概念)
比如说著名的背包问题Knapsack problem,你要在多项式的时间内找出这个解释很困难的,但是如果有人给了一个解给你,你来验证这个解(看一下每个背包是否可以装得下这个解分配的物品),这个是可以在有效时间内验证的,毕竟对于这个背包问题来说,它的验证十分简单。
stackoverflow上的回答?
quora上的回答
(个人感想:这个stackoverflow上面最高票的答案的说法是我觉得,没有能够比拟的,还有一个不错的是一个斯坦福的TA在quora上的回答,下面我会粘贴stackoverflow上最高票的这个答案,如果第一遍读了不能理解,那就读第二遍,直到读懂为止。)
P is a complexity class that represents the set of all decision problems that can be solved in polynomial time.
That is, given an instance of the problem, the answer yes or no can be decided in polynomial time.
Example
Given a connected graph G
, can its vertices be coloured using two colours so that no edge is monochromatic?
Algorithm: start with an arbitrary vertex, color it red and all of its neighbours blue and continue. Stop when you run out of vertices or you are forced to make an edge have both of its endpoints be the same color.
NP is a complexity class that represents the set of all decision problems for which the instances where the answer is "yes" have proofs that can be verified in polynomial time.
This means that if someone gives us an instance of the problem and a certificate (sometimes called a witness) to the answer being yes, we can check that it is correct in polynomial time.
Example
Integer factorisation is in NP. This is the problem that given integers n
and m
, is there an integer f
with 1 < f < m
, such that f
divides n
(f
is a small factor of n
)?
This is a decision problem because the answers are yes or no. If someone hands us an instance of the problem (so they hand us integers n
and m
) and an integer f
with 1 < f < m
, and claim that f
is a factor of n
(the certificate), we can check the answer in polynomial time by performing the division n / f
.
NP-Complete is a complexity class which represents the set of all problems X
in NP for which it is possible to reduce any other NP problem Y
to X
in polynomial time.
Intuitively this means that we can solve Y
quickly if we know how to solve X
quickly. Precisely, Y
is reducible to X
, if there is a polynomial time algorithm f
to transform instances y
of Y
to instances x = f(y)
of X
in polynomial time, with the property that the answer to y
is yes, if and only if the answer to f(y)
is yes.
Example
3-SAT
. This is the problem wherein we are given a conjunction (ANDs) of 3-clause disjunctions (ORs), statements of the form
(x_v11 OR x_v21 OR x_v31) AND
(x_v12 OR x_v22 OR x_v32) AND
... AND
(x_v1n OR x_v2n OR x_v3n)
where each x_vij
is a boolean variable or the negation of a variable from a finite predefined list (x_1, x_2, ... x_n)
.
It can be shown that every NP problem can be reduced to 3-SAT. The proof of this is technical and requires use of the technical definition of NP (based on non-deterministic Turing machines). This is known as Cook's theorem.
What makes NP-complete problems important is that if a deterministic polynomial time algorithm can be found to solve one of them, every NP problem is solvable in polynomial time (one problem to rule them all).
Intuitively, these are the problems that are at least as hard as the NP-complete problems. Note that NP-hard problems do not have to be in NP, and they do not have to be decision problems.
The precise definition here is that a problem X
is NP-hard, if there is an NP-complete problem Y
, such that Y
is reducible to X
in polynomial time.
But since any NP-complete problem can be reduced to any other NP-complete problem in polynomial time, all NP-complete problems can be reduced to any NP-hard problem in polynomial time. Then, if there is a solution to one NP-hard problem in polynomial time, there is a solution to all NP problems in polynomial time.
Example
The halting problem is an NP-hard problem. This is the problem that given a program P
and input I
, will it halt? This is a decision problem but it is not in NP. It is clear that any NP-complete problem can be reduced to this one. As another example, any NP-complete problem is NP-hard.
My favorite NP-complete problem is the Minesweeper problem.
This one is the most famous problem in computer science, and one of the most important outstanding questions in the mathematical sciences. In fact, the Clay Institute is offering one million dollars for a solution to the problem (Stephen Cook's writeup on the Clay website is quite good).
It's clear that P is a subset of NP. The open question is whether or not NP problems have deterministic polynomial time solutions. It is largely believed that they do not. Here is an outstanding recent article on the latest (and the importance) of the P = NP problem: The Status of the P versus NP problem.
The best book on the subject is Computers and Intractability by Garey and Johnson.
个人对这4个np问题的小总结:
1. p问题只是np问题的一个子类。
2. 所有的np问题都可以归约为npc问题(这就是为什么说如果能在解决了npc问题,即在多项式时间内找出npc问题的解,就可以得出np=p。因为在多项式时间内找出npc问题的解,那么肯定可以在在多项式时间内找出np问题的解,即np=p)。
3. 所有的npc问题之间都可以相互转换。
4. 所有的npc问题都可以归约为np-hard问题(这就是为什么说如果能在解决了np-hard问题,即在多项式时间内找出np-hard问题的解,就可以得出np=p。因为在多项式时间内找出np-hard问题的解,那么肯定可以在在多项式时间内找出npc问题的解,最后结果就是在在多项式时间内找出np问题的解,即np=p)。
所以实质上np、npc、np-hard只是科学家在为解决np问题上将问题进一步简化,将很多个np问题用一个npc问题去解决,接着将很多个npc问题用一个np-hard问题去解决。但是我还是有点不太理解npc是np和np-hard的交集这个说法,我承认npc是np里面的一个子类,也能理解有的问题是as hard as npc but could not prove it in in polynomial time。但这个交集的说法我时而理解时而不理解,也希望有好心人能给我解答一下。这个交集是指这个问题的难度吗?
暂时还没看,大概是关于图灵的转换机,是cook提出并且证实了SAT是第一个npc问题,即可以在polynomial time验证这个解,但这个问题还没有被解决。
我滴妈,我真的看了好久这个啊,最终看了好几个才理解。这个我最终觉得有点像一个问题:到底是有鸡先还是有蛋先
首先大家可以先理解一下经典的理发师问题
克里克岛的一座小城里有位理发师, 有一天他做出一项规定: 他给并且只给那些不给自己理发的人理发. 理发师的这个规定似乎很有道理, 既然有人自己给自己理发了, 那么我就不用"多此一举", 我再给这个人理发.
最初, 这个规定并没什么问题, 后来, 随着这个理发师自己的头发越来越长, 他发现他陷入了一个两难的境地: 他该不该给自己理发?
综合以上两种情况, "他为自己理发"当且仅当"他不为自己理发", 这成为了一个悖论.
理发师悖论在很多领域有重要的应用, 比如罗素利用理发师悖论发现了集合论的缺陷, 在当时学术界引起了极大震动. 在这里, 我们要用理发师悖论分析停机问题.
(个人感想:用程序来理解是最好的,下面这个代码的解释我是参考知乎上面的一个回答,出处在答案的低端,我觉得这个答案是比较简洁明了的)
1.首先定义一个检测程序
boolean detect(P) //输入参数为函数P 结果返回是否停机 停机true 否则 false
2.定义一个程序,根据detect返回的结果来决定自身是 无限循环 还是 停机
其中 T 为test函数本身
boolean test(T){
if(detect(test(T))){ //检测是否停机
loop() // 无限循环
}else{
return //返回 程序停止
}
}
很显然,当判定程序 detect(test(T)) 检测结果为 true时,即停机的时候,test(T)进入 loop 即无限循环中,此处存在矛盾。反之,当检测结果为false时,即没有停机,test(T) 程序结束,也是有矛盾的。
作者:知乎用户
链接:https://www.zhihu.com/question/20081359/answer/279950224
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
(个人感想:为什么说解决了np-hard这个停机问题,所有的npc问题都能解决了呢?我觉得下面这篇文章也给了大家一个很好的理解方法去理解这4个问题之间。)
文章链接
这个是维基百科的common npc problems,我觉得很有用,大家如果想要证明自己的问题,可以规约为npc
https://en.wikipedia.org/wiki/List_of_NP-complete_problems