{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/knapsackProblem","result":{"data":{"blog":{"frontmatter":{"title":"KNAPSACK PROBLEM","thumbnail":"blog72","date":"June 12, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/19QAgtu9Cke0fAxqNuB4hSa2xuBbzwsHD/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Given two arrays namely weights and price, and the capacity of\n                  the knapsack(container) M, determine the item(s) that are to\n                  be included in the knapsack so that the total weight is less\n                  than or equal to a given limit and the total price is as large\n                  as possible(max. profit).\n                </p>\n                <p>\n                  Simply we need to find the most valuable subset of the items\n                  that fit into the knapsack and here, will find the total value\n                  i.e max profit.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  Let weights[n], price[n] be two arrays and M be the max\n                  capacity of the knapsack. Variable i corresponds to items,(0\n                  to n) and j corresponds to capacity(0 to M).\n                </li>\n                <li>\n                  Let V[n+1][m+1] be the array to store profit amount(in\n                  program, will use recursion) and the max profit will be stored\n                  in V[n][m].\n                </li>\n                <li>\n                  If i==0 or j==0 then return 0. Because if either the no. of\n                  items selected or capacity is 0 then obviously the max profit\n                  is 0.\n                </li>\n                <li>\n                  If weights[ i ] > j then return V[ i-1 ][ j ]. That is to say if the\n                  weight of the item is greater than capacity then we can't\n                  include it, so return the previous item's profit corresponding\n                  to same capacity.\n                </li>\n                <li>\n                  Else if weights[ i ] <= j then return max(V[ i-1 ][ j ] ,\n                  price[ i ] + V[ i-1 ][ j-weights[ i ] ). That means if the\n                  weight is lesser than or equal to the capacity then if we have\n                  two options to find max profit for this capacity:\n                  <ol class=\"pl-4\">\n                    <li>\n                      Previous item's profit corresponding to same capacity.\n                    </li>\n                    <li>Current item + other items excluding current item.</li>\n                  </ol>\n                  Return the maximum of these two.\n                </li>\n              </ul>\n              <p></p>\n            </div>\n            <div class=\"m-2\">\n              <h4>Time Complexity: O(n*M)</h4>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/dynamicProgramming\">Dynamic Programming</a> |\n              <a href=\"/blogs/warshallsAlgorithm\">Warshall's Algorithm</a> |\n              <a href=\"/blogs/floydsAlgorithm\">Floyd's Algorithm</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Given two arrays namely weights and price, and the capacity of\n                  the knapsack(container) M, determine the item(s) that are to\n                  be included in the knapsack so that the total weight is less\n                  than or equal to a given limit and the total price is as large\n                  as possible(max. profit).\n                </p>\n                <p>\n                  Simply we need to find the most valuable subset of the items\n                  that fit into the knapsack and here, will find the total value\n                  i.e max profit.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  Let weights[n], price[n] be two arrays and M be the max\n                  capacity of the knapsack. Variable i corresponds to items,(0\n                  to n) and j corresponds to capacity(0 to M).\n                </li>\n                <li>\n                  Let V[n+1][m+1] be the array to store profit amount(in\n                  program, will use recursion) and the max profit will be stored\n                  in V[n][m].\n                </li>\n                <li>\n                  If i==0 or j==0 then return 0. Because if either the no. of\n                  items selected or capacity is 0 then obviously the max profit\n                  is 0.\n                </li>\n                <li>\n                  If weights[ i ] > j then return V[ i-1 ][ j ]. That is to say if the\n                  weight of the item is greater than capacity then we can't\n                  include it, so return the previous item's profit corresponding\n                  to same capacity.\n                </li>\n                <li>\n                  Else if weights[ i ] <= j then return max(V[ i-1 ][ j ] ,\n                  price[ i ] + V[ i-1 ][ j-weights[ i ] ). That means if the\n                  weight is lesser than or equal to the capacity then if we have\n                  two options to find max profit for this capacity:\n                  <ol class=\"pl-4\">\n                    <li>\n                      Previous item's profit corresponding to same capacity.\n                    </li>\n                    <li>Current item + other items excluding current item.</li>\n                  </ol>\n                  Return the maximum of these two.\n                </li>\n              </ul>\n              <p></p>\n            </div>\n            <div class=\"m-2\">\n              <h4>Time Complexity: O(n*M)</h4>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/dynamicProgramming\">Dynamic Programming</a> |\n              <a href=\"/blogs/warshallsAlgorithm\">Warshall's Algorithm</a> |\n              <a href=\"/blogs/floydsAlgorithm\">Floyd's Algorithm</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M172%2095c-5%205-4%2017%202%2017s10-14%205-17c-3-2-3-2-7%200M27%2095l-1%209c0%208%200%208%202%208s2-1%202-3c0-4%201-4%203%200%202%203%202%203%204%201h2c0%204%204%202%205-2l1-3%201%203%203%204c4-1%204-18%201-18-1%200-2%201-2%203l-1%203-1-3c-1-2-2-3-4-3s-3%203-3%2010v5l-2-3-3-3%203-3c4-5%201-8-4-4l-2%202v-2c0-2-3-3-4-1m29%206c-4%209-4%2011-1%2011l3-1c0-2%204-2%204-1%200%202%201%202%205%202%205%200%205%200%205-3l2-3c8-2%208-12%201-12s-7%201-7%209-1%208-2%200c-1-7-3-9-5-9s-3%201-5%207m28-5c-3%202-2%206%201%208%203%203%203%204-1%204-3-1-4%202-1%203%207%203%2012-3%207-8l-2-3h2c3%200%204-1%202-4-1-3-6-3-8%200m13%202l-4%2013%203%201%202-1c0-3%204-3%204-1%201%202%204%203%205%201l-2-15c-2-4-6-3-8%202m14-2c-5%205-3%2016%203%2016%203%200%207-5%206-7-1-1-2-1-3%201-3%204-5%201-3-4%201-3%203-6%203-2-1%203%203%202%204-1%201-5-6-7-10-3m11%207c0%208%200%209%202%209s2-1%202-3v-3l3%203c2%202%203%203%204%202%201-2%201-3-1-6-2-2-2-2%201-6%202-2%202-3%200-4-1-1-1-1-4%201l-2%202v-2l-2-2c-3%200-3%200-3%209m22-8c-3%202-3%2017%200%2017l2-2c0-2%201-3%204-4%205-2%206-6%204-10-2-2-7-3-10-1m13%200l-1%209%201%208c2%200%203-1%203-3l1-3%201%203c2%203%202%203%204%202s2-1%201-4V95h-10m27%200l-2%209v8h4c7%200%2010-4%208-8v-9h-10m12%207v8c-1%202%2011%202%2019%201%203%200%204-1%204-2%200-2-1-2-4-2s-4%200-3-1l2-1c3%200%204-2%203-3%200-2-1-2-3-2h-2l4-2c2%200%203-1%203-2%200-2-1-2-6-2l-5%201-1%205v6h-7l1-6c0-6%200-6-2-6-3%200-3%200-3%208m25-7l-1%209c0%207%200%208%202%208l3-1c0-1%201-2%202-1l2-1c1-1%201-1%201%201%200%201%201%202%203%202h2v-9c-1-10-3-12-6-5l-1%203-2-3c-1-3-4-5-5-3m78%2011l-1%208v7h9c8%200%209%201%209%203s0%202-2%202c-5-1-16%2013-21%2026l-3%2020%206-7c4-5%206-7%2010-8%206-3%206-2%206%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%205-17-1-20l-3-2-1-1-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%207v7h22v-3l1-3v-1l-1-3v-3h-22v6m-31%2028l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/b17e1349f5bbf0a6a8cfff353b4f7db7/ee604/blog72.png","srcSet":"/static/b17e1349f5bbf0a6a8cfff353b4f7db7/69585/blog72.png 200w,\n/static/b17e1349f5bbf0a6a8cfff353b4f7db7/497c6/blog72.png 400w,\n/static/b17e1349f5bbf0a6a8cfff353b4f7db7/ee604/blog72.png 800w,\n/static/b17e1349f5bbf0a6a8cfff353b4f7db7/f3583/blog72.png 1200w,\n/static/b17e1349f5bbf0a6a8cfff353b4f7db7/e4d72/blog72.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"knapsackProblem","thumbnail":"thumbnails/blog72.png"}},"staticQueryHashes":["2987289216","63159454"]}