3

I'm learning how to prove something is NP. In Thomas Cormen's intro to algorithm book, he states something is NP if given a solution to some problem, you can verify it is correct in polynomial time.

Say the problem is 2x + 9 = 55, and let's pretend I don't know how long it takes the find the correct x value, but an algorithm to solve the problem returned the solution 23. Then to show it is NP, do I only need to plug 23 in back in the equation, and see if that took a polynomial time and gave me 55? Thanks.

roverred
  • 1,841
  • 5
  • 29
  • 46

3 Answers3

4

Yes; if you can check the correctness of every correct and every incorrect answer for every instance of this problem in polynomial time, then the problem is in NP.

amit
  • 175,853
  • 27
  • 231
  • 333
user541686
  • 205,094
  • 128
  • 528
  • 886
  • NO! You are describing an *algorithm* not a *problem*! NP-Completeness is defined on *problems* not *algorithms*. A problem is NP-Complete if and only if it is: (1) In NP. (2) has a polynomial reduction from each problem in NP. – amit Dec 01 '12 at 07:43
  • @amit: ...who was talking about NP-completeness in the first place? – user541686 Dec 01 '12 at 07:46
  • However - note that the answer is still not complete, a problem is in NP if you can check the correctness of every correct and every incorrect answer for every instance of this problem *in polynomial time*. – amit Dec 01 '12 at 07:57
  • @amit: Yeah I forgot to mention that, since it was already in the question... feel free to edit it if you'd like – user541686 Dec 01 '12 at 08:03
  • I thought only a "yes" answer needed to be verifiable in poly time for a problem to be in NP? If the answer is "no", it could run forever and still be in NP. Is my understanding wrong? I'm referring to a deterministic machine. – goat Dec 01 '12 at 16:26
  • @rambocoder: What do you mean by "yes" and "no", exactly? "Is there a path..." and "is there *not* a path..." obviously have opposite answers, but they're asking the same question... – user541686 Dec 01 '12 at 19:27
  • I believe the first paragraph on wikipedia conflicts with your answer. [NP](http://en.wikipedia.org/wiki/NP_(complexity)) – goat Dec 01 '12 at 19:50
  • @rambocoder: Oh, now it's clearer what you mean. My answer isn't wrong, though, because I was referring to instances "of this problem" (i.e. the OP's example problem), which is clearly looking for a "yes"-type answer. He has to choose his example problem carefully of course, but as currently worded I think this is correct, right? – user541686 Dec 01 '12 at 19:55
  • I think his statement was wrong. I think `isInNp = (plug23AndEvaluateTookPolyTime && plug23EvaluatedTo55) || !plug23EvaluatedTo55;` is correct. I think 23 is one of many instances. For other instances, like 24(which will not fulfill the equation), there is no restriction on how long it takes to eval/verify it. – goat Dec 01 '12 at 20:19
  • @rambocoder: I think you're confusing something here (or maybe I worded my answer poorly). If someone gives you a graph and says "is there a Hamiltonian cycle in this graph?" then a "no"-type answer is indeed believed to be not verifiable in P-time. But the question of *"is **this** particular path indeed a Hamiltonian cycle?"* is indeed verifiable in P-time, whether the answer is "yes" or "no". I think you're talking about the former, whereas I'm talking about the latter... or maybe I'm just confused? – user541686 Dec 01 '12 at 20:21
  • I'm a bit confused too, but i feel the subtlety is in the word "verifiable". if the ham path is truely valid, then a verification algo must be able to confirm that particular paths validity in poly time. if the ham path is not valid, then, the verification algo can take longer than poly time to report invalidity(maybe it never needs to report?). – goat Dec 01 '12 at 20:41
  • @rambocoder: I believe if the Hamiltonian path is not valid, then that must still be decidable in P-time. What *doesn't* have to be decidable in P-time is verifying that there doesn't *exist* such a path, if someone comes along and tells you there isn't. – user541686 Dec 01 '12 at 20:54
  • You must be right. I thought I read my claim somewhere, but I can't find it. Thanks for straitening me out. – goat Dec 02 '12 at 00:35
2

Adding information to @Mehrdad answer:

Note that NP stands for Nondeterministic Polynomial time - it means that by definition - a problem is in NP if and only if it can be solved polynomially by a Non-deterministic Turing Machine.

It is equivalent to saying that in the standard computation model (RAM machine/ deterministic turing machine) - you can verify an answer in polynomial time (like @Mehrdad answered). The proof for the equivalence is described in the wikipedia page for equivalence of definitions

Bonus:
The question of "is everything that is verifiable (polynomially) on turing machine is also solveable polynomially" and the questions "is everything that is solveable on non-deterministic turing machine polynomially also solveable on deterministic turing machine polynomially" are also equivalent.
The answer is yet unknown and the problem is known as the P vs NP problem, which is the most important open question on computer science.

amit
  • 175,853
  • 27
  • 231
  • 333
1

While the problems above cover the last, and perhaps, most identifiable step of NP-ness, there are 3 basic steps to showing that a problem is in NP.

  1. Decision Problem: Can you turn your problem into a decision problem? In the case of the equation problem,the decision problem would be "is there an x such that 2x+9=55?" ?

  2. Certificate: Can you identify an answer to your decision question? Again, in the case of the equation problem, an answer might be x = 23

  3. Verification: Can you verify a certificate in polynomial time. By verifying in polynomial time, you know that the problem is not NP-Hard. Some verification steps might be: is x a number? is X equal to one half of 55-9?

That is how you know that your problem is completely in NP.