P vs NP

Today is #TheoryThursday ๐Ÿง!

I want to talk about what is, possibly, the most important question in all of Computer Science: Does P equal NP?

If you have heard of this and want to learn a bit more, read on... ๐Ÿงต๐Ÿ‘‡


Computer Science is all about finding clever ways to solve difficult problems.

We have found clever algorithms for a bunch of them: sorting stuff, finding shortest paths, solving equations, simulating physics...

But some problems seem to be way too hard ๐Ÿ‘‡


One example is the Travelling Salesman problem.

โ“ Find a cycle starting in your home city to visit all major cities in your country and return home with the least fuel cost.

This is the kind of problem we expect computers to solve easily, right? That's what computers are for!


๐Ÿ™ˆ Well, very smart people have tried, and no one has come up with an algorithm that is always better than simply trying all possible cycles.

The problem is that the number of cycles grows exponentially faster than the number of cities!


Let's make it even easier, what about if I simply ask:

โ“ Is it possible to visit all cities spending less than X dollars in fuel?

๐Ÿ™ˆ No one still knows an algorithm to answer that question precisely for any value of X without trying all cycles, which again is exponential.


๐Ÿ˜ฌ So, are we simply dumb, or is this problem so complex that it is impossible to find a clever algorithm to solve it, in the general case?

This is the root of possibly the most important question in all of Computer Science: P vs NP.


Answering this question is way harder than it seems. You see, most questions in CS are about objects: how to sort, how to compare, how to process...

But this is a meta-question:

โ“ Are there questions about objects that are intrinsically very hard to solve?


Stephen Cook tried to answer this question in the early days of Computer Science. He came up with the following definitions:

Suppose we have a question of the form:

โ“ Is there an object X with a given property Q?

๐Ÿ‘‰ We want to define how hard is this question to answer.


An example of an easy question of this type is the following:

โ“ Given an array of N elements, is there an element smaller than X?

Answering this question is easy. Look at each element, one by one, and compare it with X. It takes at most N steps, for any possible array.


This is an example of a problem in P.

๐Ÿ”‘ P here means "Polynomial-Time Complexity".

Intuitively, a problem is in P if there is an algorithm to compute a correct answer in polynomial time.


Now back to the Travelling Salesman, suppose I give you an answer:

๐Ÿ‘‰ Yes, here, this is a cycle with cost less than X.

How can you verify that answer is correct? You just add the costs of all edges in the cycle. It takes again N steps.


This is an example of a problem in NP.

๐Ÿ”‘ NP here means "Non-deterministic Polynomial-Time Complexity".

Intuitively, a problem is in NP if there is an algorithm to verify a correct answer in polynomial time.


P problems are easy to solve. NP problems, we don't know yet, but at least they are easy to verify. That's the key idea.

โš ๏ธ Note that P problems are also NP.

Now, the P vs NP question, formally, is this:

โ“ Are there problems in NP that are not in P?


P vs NP is basically asking if there are problems that are inherently harder to answer than to verify, independently of how smart we become in the future.

๐Ÿค” Think about what this means for a second, and ask yourself what's your intuition about it.


What's the right answer? We still don't know, but most computer scientists believe that P is not equal to NP.

The reasons are mostly philosophical but there is also evidence that, if P were equal to NP, a lot of weird things would happen.


This question is at the core of Computer Science because it talks about the nature of computation and its inherent limits, regardless of technological improvements.

We'll end here for now, but there is so much left to talk about (like NP-completeness). So stick around ๐Ÿ‘‹...


As usual, if you like this topic, reply in this thread or @ me at any time. Feel free to โค๏ธ like and ๐Ÿ” retweet if you think someone else could benefit from knowing this stuff.

๐Ÿงต Read this thread online at https://apiad.net/tweetstorms/theorythursday-pnp


Stay curious ๐Ÿ––:


๐Ÿ—จ๏ธ You can see this tweetstorm as originally posted in this Twitter thread.