0-1 knapsack problem-Dynamic Programming

Source: Internet
Author: User

0-1 knapsack problem-Dynamic Programming
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:
Vc + jrMTH1ta94bn7tcS82yYjMjA1NDA7tPO + signature + PHByZSBjbGFzcz0 = "brush: java;"> # include Const int c = 10; // capacity of the backpack const int w [] = {, 4}; // item weight const int v [] = {6, 3, 5, 4, 6}; // 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 <最后一个物品的重量时* m[n][j-1]="v[n];" else *背包能够放下最后一个物品时* } *********************************放置前n-1个物品********************************* 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.