{"id":1127,"date":"2016-04-28T12:09:08","date_gmt":"2016-04-28T06:39:08","guid":{"rendered":"http:\/\/simonkayar.com\/?p=1127"},"modified":"2023-02-01T15:07:32","modified_gmt":"2023-02-01T09:37:32","slug":"p-np-np-complete-np-hard-omg","status":"publish","type":"post","link":"https:\/\/simonkayar.com\/?p=1127","title":{"rendered":"P, NP, NP Complete, NP Hard &#8211; OMG"},"content":{"rendered":"<p style=\"text-align: left;\">OK, so you want to learn about this no matter the price you have to pay \ud83d\ude42<br \/>\nThen have some patience and read through the text. It could take some time to understand but it will happen.\u00a0Let&#8217;s say this field is a marriage between classic mathematics and theoritical computer science.<\/p>\n<p style=\"text-align: left;\"><strong>What is it and Why is it requried?<\/strong><br \/>\nWhile trying to find solutions for problems using algorithms (steps) we often encounter problems that are seemingly very difficult to solve.<br \/>\nWe often end up racking our brain for some time to try and find out a simple solution. Now what if a solution might not exist and we spend our valuable time in attempting it.\u00a0Why not spend that time instead to come up with an approximate solution, for example.\u00a0So classifying problems might help us to know\u00a0in which class our problem falls and how to go about it<\/p>\n<p style=\"text-align: left;\"><strong>Optimization Problem Vs Decision Problem<\/strong><br \/>\nBefore starting with various complexity classes let us first figure out what is an optimzation problem.\u00a0Optimization problems are those where we try to maximize or minimize some value, say finding the shortest path in a graph for example.\u00a0Contrast to this, Decision problems are those which have only one of the two possible answers &#8211; &#8220;yes&#8221; or &#8220;no&#8221;.<br \/>\nSimple decision problem: Given 10 numbers is there any number greater than 25<br \/>\nAnother example: Given a graph, is there a cycle?<\/p>\n<p style=\"text-align: left;\">Now, let&#8217;s see few more definitions<\/p>\n<p style=\"text-align: left;\"><strong>Polynomial time:<\/strong><br \/>\nGiven an input n, polynomial time would be any polynomial based on n, like n<sup>2<\/sup> or n<sup>3<\/sup> or even n<sup>1000<\/sup> etc. However\u00a0n! , 2<sup>n<\/sup>\u00a0are all non polynomial time. We can say any algorithm that runs in polynomial time is a tractable, simple or solvable problem.<\/p>\n<p style=\"text-align: left;\"><strong>CLASS\u00a0P<\/strong><br \/>\nAll decision problems which have a solution working in polynomial time fall in this class\u00a0Simple example: Sum of n numbers O(n) or sorting O(nlog<sub>n<\/sub>) or O(n<sup>2<\/sup>)<\/p>\n<p style=\"text-align: left;\"><strong>CLASS NP<\/strong><br \/>\nThese are actually superset of P. However unlike P they don&#8217;t have (as yet) solution in polynomial time.\u00a0However if we give an instance of the input and say that for this input the decision problem returns &#8220;yes&#8221;, we can verify the same in polynomial time.\u00a0Non Polynomial time solution and polynomial time verification. P is a subset because its solutions can also be verified in polynomial time and solution runs in polynomial time also.<br \/>\nLet&#8217;s explain with an example: If there are 100 houses and each house has to be painted in a different color compared to all their friend&#8217;s house, it is not an easy problem to solve.\u00a0However, the verification is trivial. If I already paint them in different colors and ask you to check if the painting has been done as per the above constraints, it is rather easy to verify the same. Go one by one for each house, see its color, check that it does not match with the color of friend&#8217;s house. Done ! Polynomial time verification !!<\/p>\n<p style=\"text-align: left;\">Till now, we can quickly make out that P are &#8216;easy&#8217; problems and NP or &#8216;not so easy&#8217; problems since we do not have a solution that can work in reasonable time<\/p>\n<p style=\"text-align: left;\">One more definition before proceeding further<\/p>\n<p style=\"text-align: left;\"><em>Polynomial time reduction:<\/em><br \/>\nWe say that decision problem P reduces to decision problem Q in polynomial time if we can make a function f(x) to convert all inputs of P to Q such that if f(x) is given to Q and we get &#8220;yes&#8221; we will get &#8220;yes&#8221; if we give x to P. Basically what we are doing is converting one problem to another. It might seem funny to computer science graduates. Imagine if I were to tell that to sort n numbers we can actually convert it into a problem of graph theory and solve it. But that is what Polynomial reduction is and it is significant here.<\/p>\n<p style=\"text-align: left;\"><strong>CLASS NP-COMPLETE<\/strong><br \/>\nHow about the problems which are in NP and are the hardest to solve among them. Such problems are called NP-Complete.<br \/>\nSo, if we arrange in order the problems based on how easy it is solve, from easiest to hardest, it would be P &#8212; NP &#8212; NP Complete.<br \/>\nWe can also call the NP-Complete problems as &#8220;Hard&#8221; problems that are in NP.\u00a0Mathematically if all problems &#8216;x&#8217; in NP can reduce in polynomial time to another problem &#8216;y&#8217; in NP, we call the problem &#8216;y&#8217; as NP complete<br \/>\nPlease note: All NP-Complete problems have to reduce to each other because of the above definition.\u00a0Example: &#8220;TSP: Is there a solution not exceeding cost k&#8221;<\/p>\n<p style=\"text-align: left;\"><strong>Class NP-HARD<\/strong><br \/>\nNow what is this. We already saw that &#8216;hard&#8217; problems in NP are called NP-Complete.\u00a0What about the problems that are not in NP or for that matter not even decision problems.\u00a0So there is a class for them. If a problem is as hard as every other problem in NP (like NP-Complete problems) but need not be in NP (unlike NP Complete problems which have to be in NP), such problems are called NP-hard.\u00a0Example: Every NP-Complete problem is also NP-hard.\u00a0Optimization problem &#8211; &#8220;TSP: Find least cost&#8221; : This is optimization problem and hence not in NP<br \/>\nDecision Problem but not in NP: &#8220;Halting problem&#8221; &#8211; Given a program and its input, will the program halt? Even though it is a decision problem, it is not in NP because even the verification of its solution cannot be done in polynomial time<\/p>\n<p style=\"text-align: left;\"><strong>Finally<\/strong><br \/>\nPlease look at the figure below for a brief on what we have learned<\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/simonkayar.com\/wp-content\/uploads\/2016\/04\/np.jpg\" rel=\"attachment wp-att-1128\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1128\" src=\"https:\/\/simonkayar.com\/wp-content\/uploads\/2016\/04\/np.jpg\" alt=\"np\" width=\"537\" height=\"359\" srcset=\"https:\/\/simonkayar.com\/wp-content\/uploads\/2016\/04\/np.jpg 537w, https:\/\/simonkayar.com\/wp-content\/uploads\/2016\/04\/np-300x201.jpg 300w\" sizes=\"auto, (max-width: 537px) 100vw, 537px\" \/><\/a><\/p>\n<ul>\n<li style=\"text-align: left;\">You can see the &#8216;P&#8217; problems as green dots as subset of NP. They are the easy ones with polynomial time solution available<\/li>\n<li style=\"text-align: left;\">The &#8216;NP&#8217; can be seen as blue dots.\u00a0They don&#8217;t have a known polynomial time solution but their solution is verifiable in polynomial time<\/li>\n<li style=\"text-align: left;\">The &#8216;NP-Complete&#8217; happens to be red dots. They can reduce all NP problems to themselves and additionally they sit inside NP itself<\/li>\n<li style=\"text-align: left;\">The &#8216;NP-Hard&#8217; are similar that they can reduce all NP-Complete (thereby NP) to themselves but they don&#8217;t have to be part of NP<\/li>\n<li>Additional Information\n<ul>\n<li style=\"text-align: left;\">Though everyone is sure that P is a subset of NP, they don&#8217;t know if P=NP or P != NP. That is a million dollar prize question in clay university of mathematics. Since you are now armed with this learning why not attempt to crack the question and become a millionaire \ud83d\ude42<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>OK, so you want to learn about this no matter the price you have to pay \ud83d\ude42 Then have some patience and read through the text. It could take some time to understand but it will happen.\u00a0Let&#8217;s say this field is a marriage between classic mathematics and theoritical computer science. What is it and Why is it requried? While trying to find solutions for problems using algorithms (steps) we often encounter problems that are seemingly very difficult to solve. We often end up racking our brain for some time to try and find out a simple solution. Now what if &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[],"class_list":["post-1127","post","type-post","status-publish","format-standard","hentry","category-tech-ai"],"_links":{"self":[{"href":"https:\/\/simonkayar.com\/index.php?rest_route=\/wp\/v2\/posts\/1127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simonkayar.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simonkayar.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simonkayar.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simonkayar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1127"}],"version-history":[{"count":8,"href":"https:\/\/simonkayar.com\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1427,"href":"https:\/\/simonkayar.com\/index.php?rest_route=\/wp\/v2\/posts\/1127\/revisions\/1427"}],"wp:attachment":[{"href":"https:\/\/simonkayar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simonkayar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simonkayar.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}