Throwing some light on NP-completeness and the P=NP problem
I want to say first that I am an usual reader of Jeff Atwood’s Coding Horror blog. His posts deal with a wide range of interesting subjects, not delving deep into technical topics. Some time ago, though, he tried to explain NP-Complete problems and the P=NP problem (which are different things, by the way), without good results, because he didn’t understand completely the topic. Now, he tried again, but he hardly had better luck.
It seems that this is a confusing topic, and although every computer scientist, programmer, software engineer or related professional has heard about it, many, if not most of them, can’t provide a definition any better than an intuitive one, which is usually not very accurate.
I hope this post is useful to clarify the doubts some of you can have on this topic. And maybe it helps Jeff to write a third part of the “series”, hopefully better than the previous two, and matching his usual writing quality.
First of all, say that unlike what many people think, P and NP are not the only 2 complexity classes. There are many of them. Hundreds of them are being listed and explained on Stanford University’s Complexity Zoo. It’s an extremely deep topic you could spend your whole life learning about. Of course, you don’t need to know everything of it, but it’s good to know the most common classes.
P and NP
So, what are P and NP?
- P is the class containing all the problems solvable, by a deterministic Turing machine, in a polynomial amount of time depending on the input size. This seems easy to understand to most of us.
- NP, on the other hand, does not seem so straightforward to understand to everybody. The definition of NP is the same as given for P, but changing “deterministic Turing machine” for “non-deterministic Turing machine”. Adding these 3 letters makes the machine more powerful, so NP includes many problems which are not in P (if P != NP). You can think of the non-determinism as if, every time the machine has to explore 2 different branches, it can explore both of them at the same time. You can also view it as if, when having to explore 2 different branches, it always chooses a branch that leads to accepting (if such a branch exists). Another interesting feature of NP problems is that a solution can be checked in polynomial time by a deterministic Turing machine. So, checking a solution of a NP problem is a P problem.
The problem with non-deterministic machines is just that we don’t have them. So, to solve a problem in NP and not in P (all problems in P are also in NP, as we can just use the non-deterministic Turing machine in a deterministic way) we need to execute the branches one after the other instead of executing them at the same time, and as there can be an exponential number of them, we need an exponential amount of time, depending on the input size, to solve the problem in a current processor.
NP-complete
Another term widely used is NP-complete. When is a problem in the NP-complete class? It has to have two properties:
- It has to be a NP problem.
- If it can be solved in polynomial time, then all of NP problems can.
The second is in itself a class: NP-hard. A problem can be NP-hard and not NP-complete, because it can be of more difficult complexity class than NP.
To demonstrate that a problem is NP-complete, we can reduce it to a known NP-complete a known NP-complete problem to it in polynomial time. To reduce a problem to another means transform its input to the input of another problem, with the condition of both problems with their respective inputs generating the same output. This would be of limited usefulness if we did not have an initial NP-complete problem, but we have it thanks to Cook’s theorem demonstrating that the Boolean satisfiability problem, also known as SAT, is NP-complete.
Given that, if an algorithm to solve a NP-complete problem in polynomial time in a deterministic machine is found, then all of NP problems would be in P, and the statement P = NP would be true.
P = NP?
As you will probably know, this is a really important open question, and really hard to solve as can be seen from the fact that a million dollars has been offered since year 2000 to the first who proves either its equivalence or its non equivalence.
But, whether P = NP or not, and until the unlikely fact of that being demonstrated, my advice is to, while bearing in mind that the question is open, act as if it was demonstrated that they were different. Deal with NP-complete problems as difficult ones. If you have a NP-complete problem you have to expect exponentially large times for algorithms solving it. This means that if your input is going to be large, the best you can do is look for approximation, heuristic or other non-exact methods. They will not give you the best solution, but, at least, they will last less than the age of the universe, which is something valuable.