P, NP, and NP-Complete problems... Explain, please!

Status
Not open for further replies.

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
I'm expected to know all about the P, NP, and NP-Complete decision problem classifications, but I'm pretty lost.

My professor is a jerk; he offers no help. My book is pompous, so I can't really understand the concepts from reading it. Can anyone explain what P, NP, and NP-Complete are, in plain English?

I think that if I just get a feel for what they mean conceptually, I should be able to figure out the rest. This is purely for studying purposes - we don't have homework in this class... so no, I'm not trying to get you to do my homework ;)

Thanks a lot to those brave souls who are willing to attempt to explain this to me. :p
 

DanDaManJC

Senior member
Oct 31, 2004
776
0
76
Very, very generally.. those types of problems are problems that are computationally intense to solve.

For instance.. stuff like solving the traveling salesman problem --- what's the optimum route to hit every major city in the USA? You could brute force the answer... but that'd be some permutation (or maybe it was a combination?) of all the major cities... which is a huge number and not ideal with current computing systems.

that said.. i never did get a good definition of terms around the P, N, NP terms either.. too many proofs and no practical discussion xD. Id suggest posting in the programming section as well --- there should be plenty of CS people in there that would've had to take the same algorithms class.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
If you could post some more specific questions about what you don't understand, that'd help us help you better.

If you really have no idea at all, just start googling. P-NP is a big field in CS, and there are tons of websites/sources/etc dedicated to explaining anything from the basics to more advanced topics. I'm way too lazy to start from scratch :p
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
It's best to go from broad to narrow. We start with the class called NP, which is short for non-deterministic polynomial time problems. That is the set of all decision problems that can be solved in polynomial time by a non-deterministic Turing machine.

Now I introduced two terms there that need further clarification. First, a decision problem is basically a problem where you can answer "yes" or "no". For example, in a searching problem this may be "does element N exist in set S?" or "are there any subsets of S that sum to zero?" Second, a non-deterministic Turing machine is basically a fictional device that can make decisions non-deterministically and solve problems very quickly as a result. For example, you can think of a traditional Turing machine like your average computer: it executes a fixed program. A non-deterministic Turing machine is a fictional computer whose program makes decisions in the most opportunistic manner possible so that it can solve problems very quickly.

Consider a normal Turing machine trying to determine is element N exists in set S. This machine has to iterate over the elements of S searching to see if any is equal to N. The non-deterministic Turing machine just happens to test the element of S that equals N first and returns "yes" immediately as a result.

An equivalent way of defining NP is the class of search problems whose answers can be verified in polynomial time. This is easier to understand conceptually. It means that given a decision problem in NP and a possible answer, you can efficiently (i.e., in polynomial time), verify whether that answer is correct. A simple example is the subset sum problem, which asks is there any subset of length N of the set of integers S that sums to zero. For example, let S = {-2, 5, 3, -9, 8, 4} and N = 3. So pick any subset of length N, such as {5, 8, -2}. Is this a "yes" or a "no"? Well it's a "no" and it's very fast to verify, it's just O(N) (add up N elements), a linear time problem.

Now, why are these definitions equivalent? You can consider that our non-deterministic Turing machine could come up with guesses very efficiently due to its non-deterministic nature. These could then be verified in polynomial time. So being able to verify in polynomial time is the same as having a non-deterministic Turing machine solve it in polynomial time.

P is a subset of NP which is the set of all problems that can be solved in polynomial time on a deterministic Turing machine. Again, a deterministic Turing machine is something like a traditional computer running a piece of software. It follows fixed instructions and can't rely on magically guessing the solution all of the time. Naturally this is less powerful than a non-deterministic Turing machine, hence P is a subset of NP.

NP complete problems are the hardest problems in NP. They are the set of NP problems for which ANY problem within NP can be transformed into that problem in polynomial time by switching around its inputs properly. That means basically a problem is NP complete if it meets two conditions:

* It is in NP.
* Any NP problem can be transformed into the given problem in polynomial time.

So you can see why these are the "hardest" NP problems: because they are as hard as the hardest problem in NP (since any NP problem is equivalent by a polynomial time transformation).

What this also means is that if we can solve ANY NP complete problem in polynomial time, then we can solve ANY problem within NP in polynomial time. That is powerful! That means we can efficiently solve any problem in NP. We do this by transforming that problem into the NP complete problem we can solve efficiently (and we know we can do this transformation efficiently, i.e., in polynomial time, by the above definitions), then solving the NP complete problem using our efficient polynomial time algorithm.

The million dollar question (quite literally there is a million dollar bounty out for the answer to this question) is whether such an algorithm exists that can solve an NP complete problem in polynomial time. That is, is P = NP? If it isn't clear why this proves that P = NP, consider what I already said: if any such algorithm exists, we can solve all of NP in polynomial time on a deterministic Turing machine, which is the class P I described earlier. Thus, the existence of such an algorithm means that P and NP are actually the same.

That's about it. P, NP, NP-completeness, and why P =? NP is such an important question in a nutshell.
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
esun, I love you. That's good enough for now - I'll post again when I have more questions :D
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Interesting. Would anyone think that quantum computing will lead to P=NP?
 

iCyborg

Golden Member
Aug 8, 2008
1,342
59
91
No, quantum computers may be faster than classical, but they have no relevance on P!=NP problem, since these are mathematically defined computational classes.
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
Determining that it's P is easy. So... If I were given a generic problem/algorithm, how would I determine whether the algorithm is NP or NP-Complete?

I'll give it a shot with my logic. Let me know if this sounds right:

I want to argue that the Hamiltonian cycle decision problem (does a Hamiltonian cycle of weight <= w exist in graph G?) is NP-Complete. I must show that:
1. It itself is NP
2. Any NP problem can be reduced to the Hamiltonian cycle decision problem.

To show 1:

The only way to solve it in polynomial time is to guess all the vertices nondeterministically, since using a deterministic approach would run in exponential time (brute force) . Verifying the result of the algorithm is possible in deterministic polynomial time, which is what makes the actual yes/no decision. For a graph of n edges and m vertices, this verification would just check to see if all traversed edges are valid/exist in the graph (loop through m edges), the total weight <= w, and make sure that no vertices are repeated, except for the first and last vertices should be the same (three loops through n vertices). Thus, the verification to decide yes/no takes O(n + m) time, which is in polynomial time.

To show 2:

The classic traveling salesman decision problem problem (TSP)... I would argue that since the problem can be reduced to a Hamiltonian cycle problem (i.e. its input can be rephrased to "vertices" instead of cities, and "weights" instead of costs) AND this reduction can be done in polynomial time (should be O(n + m) for n cities and m costs), then the TSP must be at least as complex as the Hamiltonian cycle decision problem. So we can assume that TSP is class NP. This also shows that the Hamiltonian cycle problem is NP-Complete because we were able to reduce another NP algorithm and solve generally with the Hamiltonian cycle algorithm.

How close or far am I?
 
Last edited:

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Determining that it's P is easy. So... If I were given a generic problem/algorithm, how would I determine whether the algorithm is NP or NP-Complete?

I'll give it a shot with my logic. Let me know if this sounds right:

I want to argue that the Hamiltonian cycle decision problem (does a Hamiltonian cycle of weight <= w exist in graph G?) is NP-Complete. I must show that:
1. It itself is NP
2. Any NP problem can be reduced to the Hamiltonian cycle decision problem.

To show 1:

The only way to solve it in polynomial time is to guess all the vertices nondeterministically, since using a deterministic approach would run in exponential time (brute force) . Verifying the result of the algorithm is possible in deterministic polynomial time, which is what makes the actual yes/no decision. For a graph of n edges and m vertices, this verification would just check to see if all traversed edges are valid/exist in the graph (loop through m edges), the total weight <= w, and make sure that no vertices are repeated, except for the first and last vertices should be the same (three loops through n vertices). Thus, the verification to decide yes/no takes O(n + m) time, which is in polynomial time.

To show 2:

The classic traveling salesman decision problem problem (TSP)... I would argue that since the problem can be reduced to a Hamiltonian cycle problem (i.e. its input can be rephrased to "vertices" instead of cities, and "weights" instead of costs) AND this reduction can be done in polynomial time (should be O(n + m) for n cities and m costs), then the TSP must be at least as complex as the Hamiltonian cycle decision problem. So we can assume that TSP is class NP. This also shows that the Hamiltonian cycle problem is NP-Complete because we were able to reduce another NP algorithm and solve generally with the Hamiltonian cycle algorithm.

How close or far am I?

Showing that something is in NP is usually easy. You just have to exhibit a method of checking the validity of the yes/no solution in non-deterministic, polynomial time, but you already know this. A lot of problems you run into will be in NP.

To show that something is NP-complete, what you suggested would work. However, 99&#37; of the time, it will be easier to:
1) prove its in NP
2) reduce a known NP-complete problem to it
Whether by knowledge or luck, you chose to reduce from decision-TSP, which is known to be NP-complete.

Your argument for a non-deterministic algorithm is mostly ok. The one issue I have is this: "since using a deterministic approach would run in exponential time (brute force)." That is not obvious. If P=NP (likely or unlikely as that is), then the statement is false. Yes, the state-space is exponential in size. Yes, checking every state exhaustively requires exponential time. But the state-space for MST is also exponential in size. Yet the time needed to compute MST is almost linear (known to be amortized linear if randomness is allowed; amortized to somewhere btwn linear and linear*inv_ackerman otherwise). You've stated that the state-space is exponential in size, but have given no indication that a deterministic algorithm must take exponential time. As esun said, if you can pull that off, you get $1M and everlasting fame... :p Either way, the performance of a determinstic polynomial checker is totally irrelevant here. You don't care at all whether a deterministic poly algo can get the job done (unless you want to show something is in NP by first showing that it is in P, since P is at least a subset of NP; but I've never seen anyone need to do that, lol).

In your reduction from TSP to Hamiltonian cycle is generally OK, but there are a few issues. Hamiltonian cycle is considered a special case of TSP. One minor technical detail to note is that TSP is usually considered on complete graphs; so to make your input to hamiltonian cycle a complete graph (if it isn't), what would you do?

Also the statement "So we can assume that TSP is class NP." is awkward. "So we can assume" indicates that something you've said implies this assumption. That is not true. The decision version of TSP has been proven to be NP-complete (the non-decision TSP is NP-hard). There's nothing to assume here.

Lastly, it is not enough that decision-TSP is in NP. If decision-TSP were only NP (not NP-complete or NP-hard), then reducing it to Hamiltonian cycle proves *nothing* :( In particular, one condition for Hamiltonian cycle to be NP-complete is (as you stated) that ANY NP problem can be reduced to it. If decision-TSP was 'merely' NP, then you've only reduced ONE NP problem to Hamiltonian cycle, not any. On the other hand, reducing a known NP-complete problem (call it A, e.g., decision-TSP) to hamiltonian cycle is the same as reducing ANY NP problem to hamiltonian cycle. That is, since A is NP-complete, then any NP problem can be reduced to A. And since you've shown a reduction from A to Hamiltonian cycle, then by transitivity, any NP problem can be reduced to hamiltonian cycle. Does that make sense?

Basically you got the main ideas right, but are still missing some of the details in this NP mess.

edit: oh and mad props/respect/etc to esun. That's one of the cleanest introductory-level explanations of P, NP, NP-complete, etc. that I've run across.
 
Last edited:

iCyborg

Golden Member
Aug 8, 2008
1,342
59
91
P, NP and NP-complete are not hard to grasp. But NP-hard class is a little confusing to me, I easily forget what exactly is its relation to the others and who can be reduced to what and have to think about it for a bit.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
P, NP and NP-complete are not hard to grasp. But NP-hard class is a little confusing to me, I easily forget what exactly is its relation to the others and who can be reduced to what and have to think about it for a bit.

NP-hard is just all NP-complete problems + all problems that can be reduced from an NP-complete problem. So they're 'harder than all NP problems'. NP-hard doesn't have to be in NP (e.g., not a decision problem). Like the decision 0-1 knapsack problem is NP-complete and the optimization 0-1 knapsack is NP-hard (simple reduction is to binary search on the weight). Though I don't know of any NP-hard problems that don't have very, very closely related NP-complete decision brothers.

Is there more to it than this? B/c that's all I know, lol.
 

iCyborg

Golden Member
Aug 8, 2008
1,342
59
91
The definitions are OK, when you know them, but after a few years you forget some details. And a couple of things are not very intuitive: NP-hard need not be in NP, the complete in NP-complete to me suggests that NP-complete is a bigger class than NP-hard (it's not). And knowing what has to be polytime reducible to what is easy if you memorize it as a fact, but to understand why you have to reduce NP-complete to NP-hard (also knowing that NP-complete is NP-hard as well), and not the other way around takes some thinking for me.
In other words, it's not hard to memorize facts, but the rationale for them always takes a bit of thinking for me after I've not dealt with them in a couple years.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Sorry just got your PM (I never check them...). Anyway, the general method for proving a problem is NP complete is:

1. Prove it is in NP.
2. Prove that it can be reduced to another NP complete problem in polynomial time.

Recall that you can't just pick any problem in NP to reduce your problem to. You need to prove that it ANY problem can be reduced to it. Thus, if you can prove it can be reduced to an NP complete problem, and since we know any problem in NP can be reduced to an NP complete problem, only then have you shown that any problem can be reduced to your problem (by first reducing to the NP complete problem, then using your proof to reduce it to your problem).

The most common way of proving 1. is to show that a solution can be verified in polynomial time. You also have to establish that it is in fact a decision problem but that is usually trivial (does it have a "yes/no" answer?). Proving 2. typically requires a complete description of the algorithm used to transform your problem into an NP complete problem (you can pick whichever known NP complete problem is most convenient) and a proof that your reduction is polynomial time.

If you want a ton of examples of these proofs, the classic paper on this was written by Richard Karp (a UC Berkeley professor). Here's a Wikipedia article on it:

http://en.wikipedia.org/wiki/Karp&#37;27s_21_NP-complete_problems

And here's a link to his paper:

http://www.cs.berkeley.edu/~luca/cs172/karp.pdf

I believe that proving a problem is NP complete without reduction is quite difficult, though I have not gone through any such proofs myself. I'm sure there are many papers on this, though, so if you do a literature search you'll likely find lots of material.
 
Status
Not open for further replies.