{"id":26071,"date":"2017-10-26T19:45:59","date_gmt":"2017-10-26T14:15:59","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26071"},"modified":"2017-10-26T19:45:59","modified_gmt":"2017-10-26T14:15:59","slug":"c-programming-0-1-knapsack-problem-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-0-1-knapsack-problem-2\/","title":{"rendered":"Cpp Programming &#8211; 0-1 Knapsack Problem"},"content":{"rendered":"<p>Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. <span id=\"more-18430\"><\/span>In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don\u2019t pick it (0-1 property).<\/p>\n<p>A simple solution is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.<\/p>\n<ul>\n<li><strong>Optimal Substructure:<\/strong><\/li>\n<\/ul>\n<p>To consider all subsets of items, there can be two cases for every item: (1) the item is included in the optimal subset, (2) not included in the optimal set.<br \/>\nTherefore, the maximum value that can be obtained from n items is max of following two values.<\/p>\n<ul>\n<li>Maximum value obtained by n-1 items and W weight (excluding nth item).<\/li>\n<li>Value of nth item plus maximum value obtained by n-1 items and W minus weight of the nth item (including nth item).<\/li>\n<\/ul>\n<p>If weight of nth item is greater than W, then the nth item cannot be included and case 1 is the only possibility.<\/p>\n<ul>\n<li><strong>Overlapping Subproblems<\/strong><\/li>\n<\/ul>\n<p>Following is recursive implementation that simply follows the recursive structure mentioned above.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/* A Naive recursive implementation of 0-1 Knapsack problem *\/<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ A utility function that returns maximum of two integers<br\/>int max(int a, int b) { return (a &gt; b)? a : b; }<br\/> <br\/>\/\/ Returns the maximum value that can be put in a knapsack of capacity W<br\/>int knapSack(int W, int wt[], int val[], int n)<br\/>{<br\/>   \/\/ Base Case<br\/>   if (n == 0 || W == 0)<br\/>       return 0;<br\/> <br\/>   \/\/ If weight of the nth item is more than Knapsack capacity W, then<br\/>   \/\/ this item cannot be included in the optimal solution<br\/>   if (wt[n-1] &gt; W)<br\/>       return knapSack(W, wt, val, n-1);<br\/> <br\/>   \/\/ Return the maximum of two cases: <br\/>   \/\/ (1) nth item included <br\/>   \/\/ (2) not included<br\/>   else return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),<br\/>                    knapSack(W, wt, val, n-1)<br\/>                  );<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int val[] = {60, 100, 120};<br\/>    int wt[] = {10, 20, 30};<br\/>    int  W = 50;<br\/>    int n = sizeof(val)\/sizeof(val[0]);<br\/>    printf(&quot;%d&quot;, knapSack(W, wt, val, n));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>220<\/pre>\n<p>It should be noted that the above function computes the same subproblems again and again. See the following recursion tree, K(1, 1) is being evaluated twice. Time complexity of this naive recursive solution is exponential (2^n).<\/p>\n<pre>In the following recursion tree, K() refers to knapSack().  The two \r\nparameters indicated in the following recursion tree are n and W.  \r\nThe recursion tree is for following sample inputs.\r\nwt[] = {1, 1, 1}, W = 2, val[] = {10, 20, 30}\r\n\r\n                       K(3, 2)         ---------&gt; K(n, W)\r\n                   \/            \\ \r\n                 \/                \\               \r\n            K(2,2)                  K(2,1)\r\n          \/       \\                  \/    \\ \r\n        \/           \\              \/        \\\r\n       K(1,2)      K(1,1)        K(1,1)     K(1,0)\r\n       \/  \\         \/   \\          \/   \\\r\n     \/      \\     \/       \\      \/       \\\r\nK(0,2)  K(0,1)  K(0,1)  K(0,0)  K(0,1)   K(0,0)\r\nRecursion tree for Knapsack capacity 2 units and 3 items of 1 unit weight.\r\n<\/pre>\n<p>Since suproblems are evaluated again, this problem has Overlapping Subprolems property. So the 0-1 Knapsack problem has both properties of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array K[][] in bottom up manner. Following is Dynamic Programming based implementation.<\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A Dynamic Programming based solution for 0-1 Knapsack problem<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ A utility function that returns maximum of two integers<br\/>int max(int a, int b) { return (a &gt; b)? a : b; }<br\/> <br\/>\/\/ Returns the maximum value that can be put in a knapsack of capacity W<br\/>int knapSack(int W, int wt[], int val[], int n)<br\/>{<br\/>   int i, w;<br\/>   int K[n+1][W+1];<br\/> <br\/>   \/\/ Build table K[][] in bottom up manner<br\/>   for (i = 0; i &lt;= n; i++)<br\/>   {<br\/>       for (w = 0; w &lt;= W; w++)<br\/>       {<br\/>           if (i==0 || w==0)<br\/>               K[i][w] = 0;<br\/>           else if (wt[i-1] &lt;= w)<br\/>                 K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]);<br\/>           else<br\/>                 K[i][w] = K[i-1][w];<br\/>       }<br\/>   }<br\/> <br\/>   return K[n][W];<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>    int val[] = {60, 100, 120};<br\/>    int wt[] = {10, 20, 30};<br\/>    int  W = 50;<br\/>    int n = sizeof(val)\/sizeof(val[0]);<br\/>    printf(&quot;%d&quot;, knapSack(W, wt, val, n));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>220<\/pre>\n<p>Time Complexity: O(nW) where n is the number of items and W is the capacity of knapsack.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; 0-1 Knapsack Problem &#8211; Dynamic Programming simple solution is to consider all subsets of items and calculate the total weight and value<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,70145],"tags":[76776,76782,76781,70610,76775,76771,76778,70612,70619,76780,72847,72842,72845,70483,72841,72848,72840,75844,72849,72846,76772,76774,76786,72843,72844,72850,72839,76783,76779,70580,76768,70579,76773,76777,76785,76784,71412,76770,70162,70605,76769,70604,76788,76787,72852,72853,72851],"class_list":["post-26071","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-dynamic-programming","tag-0-1-knapsack-dynamic-programming","tag-0-1-knapsack-problem-algorithm-in-dynamic-programming","tag-0-1-knapsack-problem-dynamic-programming-algorithm","tag-0-1-knapsack-problem-using-dynamic-programming","tag-0-1-knapsack-problem-using-dynamic-programming-algorithm","tag-0-1-knapsack-problem-using-dynamic-programming-c-code","tag-0-1-knapsack-problem-using-dynamic-programming-code-c","tag-0-1-knapsack-problem-using-dynamic-programming-example","tag-0-1-knapsack-problem-using-dynamic-programming-in-c","tag-0-1-knapsack-using-dynamic-programming","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-c","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-examples","tag-dynamic-programming-in-c","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-knapsack","tag-dynamic-programming-knapsack-problem","tag-dynamic-programming-knapsack-problem-example","tag-dynamic-programming-problems","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-implement-0-1-knapsack-problem-using-dynamic-programming","tag-knapsack-algorithm-dynamic-programming","tag-knapsack-dynamic-programming","tag-knapsack-problem-c","tag-knapsack-problem-dynamic-programming","tag-knapsack-problem-dynamic-programming-c-code","tag-knapsack-problem-dynamic-programming-example","tag-knapsack-problem-dynamic-programming-solution","tag-knapsack-problem-example-using-dynamic-programming","tag-knapsack-problem-example-using-greedy-method","tag-knapsack-problem-explained","tag-knapsack-problem-greedy-algorithm","tag-knapsack-problem-in-c","tag-knapsack-problem-java","tag-knapsack-problem-using-dynamic-programming","tag-knapsack-problem-using-dynamic-programming-c-code","tag-knapsack-problem-using-dynamic-programming-example","tag-problems-on-dynamic-programming","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26071"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26071\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}