<link type="text/css" rel="stylesheet" href="http://explorations.chasrmartin.com/wp-content/plugins/ajax-comment-posting/acp.css" /><?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Explorations &#187; Tutorial</title>
	<atom:link href="http://explorations.chasrmartin.com/category/technical/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://explorations.chasrmartin.com</link>
	<description>Believe nothing, no matter where you read it, or who said it, unless it agrees with your own reason and your own common sense.</description>
	<lastBuildDate>Sun, 13 Feb 2011 04:41:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>What are P, NP-Complete, and NP-Hard?</title>
		<link>http://explorations.chasrmartin.com/2008/11/24/what-are-p-np-complete-and-np-hard/</link>
		<comments>http://explorations.chasrmartin.com/2008/11/24/what-are-p-np-complete-and-np-hard/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 20:40:22 +0000</pubDate>
		<dc:creator>Charlie</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://explorations.chasrmartin.com/?p=1549</guid>
		<description><![CDATA[I ended up writing this for someone on StackOverflow; thought I&#8217;d preserve it here too. We start with the idea of a decision problem, a problem for which an algorithm can always answer &#8220;yes&#8221; or &#8220;no.&#8221; We also need the idea of two models of computer (Turing machine, really): deterministic and non-deterministic. A deterministic computer [...]]]></description>
			<content:encoded><![CDATA[<p>I ended up writing <a href="http://stackoverflow.com/questions/308213/explaining-computational-complexity-theory#309507">this</a> for someone on <a href="http://stackoverflow.com/questions/308213/explaining-computational-complexity-theory#309507">StackOverflow</a>; thought I&#8217;d preserve it here too.</p>
<p>We start with the idea of a decision problem, a problem for which an algorithm can always answer &#8220;yes&#8221; or &#8220;no.&#8221; We also need the idea of two models of computer (Turing machine, really): deterministic and non-deterministic. A deterministic computer is the regular computer we always thinking of; a non-deterministic computer is one that is just like we&#8217;re used to except that is has unlimited parallelism, so that any time you come to a branch, you spawn a new &#8220;process&#8221; and examine both sides. Like Yogi Berra said, when you come to a fork in the road, you should take it.</p>
<p>A decision problem is in P if there is a known polynomial-time algorithm to get that answer. A decision problem is in NP if there is a known polynomial-time algorithm for a non-deterministic machine to get the answer.</p>
<p>Problems known to be in P are trivially in NP &#8212; the nondeterministic machine just never troubles itself to fork another process, and acts just like a deterministic one. There are problems that are known to be neither in P nor NP; a simple example is to enumerate all the bit vectors of length n. No matter what, that takes 2<sup>n</sup> steps.</p>
<p>(Strictly, a decision problem is in NP if a nondeterministic machine can arrive at an answer in poly-time, and a deterministic machine can <em>verify</em> that the solution is correct in poly time.)</p>
<p>But there are some problems which are known to be in NP for which no poly-time deterministic algorithm is known; in other words, we know they&#8217;re in NP, but don&#8217;t know if they&#8217;re in P. The traditional example is the decision-problem version of the Traveling Salesman Problem (decision-TSP): given the cities and distances, is there a route that covers all the cities, returning to the starting point, in less than <em>x</em> distance? It&#8217;s easy in a nondeterministic machine, because every time the nondeterministic traveling salesman comes to a fork in the road, he takes it: his clones head on to the next city they haven&#8217;t visited, and at the end they compare notes and see if any of the clones took less than <em>x</em> distance.</p>
<p>(Then, the exponentially many clones get to fight it out for which ones must be killed.)[<a href="http://explorations.chasrmartin.com/2008/11/24/what-are-p-np-complete-and-np-hard/#footnote_0_1549" id="identifier_0_1549" class="footnote-link footnote-identifier-link" title="Or, of course, the first clone into home-base first checks to see if their distance was less than x, hangs the yes or no sign, and then machine guns all the late arrivals.&nbsp; Simultaneous arrivals are left as an exercise.">1</a>]</p>
<p>It&#8217;s not known whether decision-TSP is in P: there&#8217;s no known poly-time solution, but there&#8217;s no proof such a solution doesn&#8217;t exist.</p>
<p>Now, one more concept: given decision problems P and Q, if an algorithm can transform a solution for P into a solution for Q in polynomial time, it&#8217;s said that Q is <em>poly-time reducible</em> (or just reducible) to P.</p>
<p>A problem is NP-complete if you can prove that (1) it&#8217;s in NP, and (2) show that it&#8217;s poly-time reducible to a problem already known to be NP-complete. (The hard part of that was proving the <em>first</em> example of an NP-complete problem: that was done by Steve Cook in <a rel="nofollow" href="http://en.wikipedia.org/wiki/Cook%27s_theorem">Cook&#8217;s Theorem</a>.)</p>
<p>So really, what it says is that if anyone ever finds a poly-time solution to one NP-complete problem, they&#8217;ve automatically got one for <em>all</em> the NP-complete problems; that will also mean that P=NP.</p>
<p>A problem is NP-hard if and only if it&#8217;s &#8220;at least as&#8221; hard as an NP-complete problem. The more conventional Traveling Salesman Problem of finding the shortest route is NP-hard, not strictly NP-complete.</p>
Footnotes:<ol class="footnotes"><li id="footnote_0_1549" class="footnote">Or, of course, the first clone into home-base first checks to see if their distance was less than <em>x</em>, hangs the yes or no sign, and then machine guns all the late arrivals.  Simultaneous arrivals are left as an exercise.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://explorations.chasrmartin.com/2008/11/24/what-are-p-np-complete-and-np-hard/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

