DP10 0-1 backpack Problem 0-1 Knapsack Problem @ geeksforgeeks

Source: Internet
Author: User

01 the problem of obtaining and not obtaining a backpack is actually an opportunity cost problem. If you take something, although the current value has increased for the moment, you have paid the opportunity cost. If not, the space left may be more valuable in the future. Therefore, space and value are always in conflict. Our goal is to use limited space to load the best value.


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. 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't pick it (0-1 property ).

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.

1) Optimal Substructure:
To consider all subsets of items, there can be two cases for every item: (1) the item is encoded in the optimal subset, (2) not supported ded in the optimal set.
Therefore, the maximum value that can be obtained from n items is max of following two values.
1) Maximum value obtained by n-1 items and W weight (excluding nth item ).
2) Value of nth item plus maximum value obtained by n-1 items and W minus weight of the nth item (including nth item ).

If weight of nth item is greater than W, then the nth item cannot be encoded and case 1 is the only possibility.

2) Overlapping Subproblems
Following is recursive implementation that simply follows the recursive structure mentioned above.



Package DP; public class ZeroOneKnapsack {public static void main (String [] args) {int [] wt = {10, 20, 30}; int [] val = {60,100,120 }; int cap = 50; int n = wt. length; System. out. println (knapSackRec (cap, wt, val, n); System. out. println (knapSackDP (cap, wt, val, n);} // Returns the maximum value that can be put in a knapsack of capacity cappublic static int knapSackRec (int cap, int [] wt, int [] val, int n) {if (n = 0 | cap = 0) {return 0 ;} // If weight of the nth item is more than Knapsack capacity W, then // this item cannot be attached ded in the optimal solutionif (wt [n-1]> cap) {return knapSackRec (cap, wt, val, n-1);} else {// Return the maximum of two cases: (1) not supported ded (2) nth item includedreturn Math. max (knapSackRec (cap, wt, val, n-1), knapSackRec (cap-wt [n-1], wt, val, n-1) + val [n-1]);} public static int knapSackDP (int cap, int [] wt, int [] val, int n) {int [] [] dp = new int [n + 1] [cap + 1]; for (int I = 0; I <= n; I ++) {for (int w = 0; w <= cap; w ++) {if (I = 0 | w = 0) {dp [I] [w] = 0;} else if (wt [I-1]> w) {// because it is overweight, therefore, do not select dp [I] [w] = dp [I-1] [w];} else {// The value between unselected and selected is greater than that of dp [I] [w] = Math. max (dp [I-1] [w], dp [I-1] [w-wt [I-1] + val [I-1]);} return dp [n] [cap] ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.