0-1 knapsack problem-Dynamic Programming

Source: Internet
Author: User
Problem description: N items and a backpack are given. The Weight of item I is W [I], the value is V [I], and the size of the backpack is C. Q: How should I select the items to be loaded into the backpack to maximize the total value of the items in the backpack?
Analysis: For an item, either a backpack or no package is loaded. Therefore, the loading status of an item can be 0 or 1. Set the loading status of item I to XI, XI, (0, 1). This problem is called the 0-1 backpack problem.
Data: number of items n = 5, item weight W [5] = {2, 6, 5, 4}, item value V [5] = {6, 3, 5, 4, 6 }, total weight c = 10. The maximum size of the backpack is 10, so in the array m size, you can set the row and column values to 5 and 10, then, for M (I, j) it indicates the maximum value of items in the backpack when the optional items are I to N and the size of the backpack is J (total weight. The following table shows the process of solving the 0-1 knapsack problem by using the dynamic programming method:

Gray in the table is the serial number: 1 ~ of the column ~ 5 indicates five items, 1 ~ 10 indicates the size of the backpack, the green column indicates the weight of the corresponding item, and the light blue column indicates the value of the corresponding item. Assume that a new 5*10 array corresponds to the brown part in the image, and the brown part is the process of solving the problem. The specific process is as follows:
The process shown in the figure is to solve the problem from the bottom up, that is, to analyze 5th items, and finally analyze the first item. When the backpack is empty, consider 5th items. When the size of the backpack is 1, 2, 3, this backpack is not loaded with 5 items, because the weight of 5 items is 4, therefore, when the size of a backpack is 1, 3, or 3, the value of the content in the backpack (the value in the brown table) is also 0, when the backpack capacity is greater than or equal to 4, the backpack can put 5 items, so the value of the items in the backpack is 6 (because only five items are considered here ), by now, only item 5 has been analyzed;
Next, we will analyze the situation of having items 5 and 4 at the same time. When the size of the backpack is 1, 2, 3, there will be no more than 4 items or 5 items in the backpack, therefore, the value of items in the backpack is 0. When the size of the backpack is greater than or equal to 4, at least 5 items can be put down. At this time, we have to choose between them, whether to put Item 5 into a greater value or put Item 4 into a greater value, when the backpack capacity is 4, you can only put the item 5, the value is 6, when the backpack capacity is 5, if you select house item 4, the remaining backpack capacity is 0 and the weight of the backpack is 0 (in the section that has been filled in the previous step, only the position of the 5th columns in the 1st rows is filled here. The maximum value of this column is 0, so when you choose to put item 4, the maximum value of the backpack is 4 <do not put Item 4 (the remaining backpack capacity is 5, find the filled part of the backpack capacity 5, the maximum value is 6) 6, so when the backpack capacity is 5, the optimal value is to put Item 5 instead of item 4, it is analyzed that when the backpack capacity is 9, when you choose to put item 4, the remaining backpack capacity is 4, find the part that has been filled in when the backpack capacity is 4 (that is, the last line), you can find the maximum The value is 6, so the maximum value you can obtain when you choose to add item 4 is 10 ....
This is the process in the middle. Here, we will analyze the placement of the last item. When the backpack capacity is 2, if you put Item 1, the value will be 6, and the remaining backpack capacity will be 0, the maximum value obtained by the remaining backpack capacity is 0, so 6 + 0 = 6 is the maximum value. If you do not place 1 item, the remaining backpack capacity is 2, the maximum value available for the remaining backpack capacity is 3, so the best time should be put into item 1; when the backpack capacity is 3, if put 1 item, get value 6, the maximum value available for the remaining backpack capacity 1 is 0, so the maximum value available for storing Item 1 is 6. If you do not place item 1, so the maximum value available for 3 of the remaining backpack capacity (found in the filled Section) is 3, so it is also the best to put Item 1 in this case ...... when the backpack capacity is 8, put item 1 to get value 6, remaining backpack capacity 6 to get the maximum value of 9, put item 1 to get the maximum value of 6 + 9 = 15, if you do not put Item 1, the maximum value of the remaining 8 backpack capacity is 9, so it is also the best to put item 1.

Conclusion: Through the above analysis process, we can first consider the maximum value this item can obtain when it is placed (the value of this item + the remaining backpack (total size of the backpack-the weight of this item) capacity can obtain the maximum value), and then consider the maximum value that can be obtained without putting this item (the remaining backpack capacity (this is the total weight of the backpack ), then compare the two, and save the value of the result if the value of the result is greater, and so on.

# Include <stdio. h> const int c = 10; // capacity of the backpack const int W [] = {, 6, 5, 4 }; // item weight const int V [] = {6, 3, 5, 4, 6}; // the item to be added const int n = sizeof (W) /sizeof (W [0])-1; // n indicates the number of items int X [N]; void package0_1 (int m [] [10], const int W [], const int V [], const int N) // n indicates the number of items {int I, J; /********************************** place the last item separately * * ******************************/For (j = 1; j <= C; j ++) {If (j <W [N])/* backpack capacity <last thing Product weight */M [N] [J-1] = 0; else/* When the backpack can put down the last item */M [N] [J-1] = V [N];} /********************************** place the first n-1 items * * ******************************/for (I = n-1; i> = 0; I --) for (j = 1; j <= C; j ++) {If (j <W [I]) M [I] [J-1] = m [I + 1] [J-1]; // If j <W [I], the current position cannot be placed, it is equal to the value of else at the previous position. Otherwise, it compares whether the placed value is large or not, choose M [I] [J-1] = m [I + 1] [J-1]> M [I + 1] [j-1-w [I] + V [I]? M [I + 1] [J-1]: M [I + 1] [j-1-w [I] + V [I];} void answer (int m [] [10], const int N) {Int J = C-1;/* I = 0, j = the maximum value of storing a backpack with C x/int I; for (I = 0; I <n; I ++) if (M [I] [J] = m [I + 1] [J]) x [I] = 0; else {x [I] = 1; /* if the current item is in a backpack */J = J-W [I];/* re-calculate the remaining size of the backpack, in order to calculate the trade-off between other items during the optimization */} I-= 1; X [N] = m [I] [J]? 1: 0;} int main () {int M [6] [10] = {0}; int I, j; package0_1 (M, W, V, N ); for (I = 0; I <= 4; I ++) {for (j = 0; j <10; j ++) printf ("% 3d ", M [I] [J]); printf ("\ n");} answer (m, n); printf ("the best answer is: \ n "); for (I = 0; I <5; I ++) printf ("% d", X [I]); printf ("\ n"); Return 0 ;}


The program running result is as follows:


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.