0-1 knapsack problem; Dynamic Planning; time complexity O (N-party); maximum value and solution; Summary of dynamic planning ideas;

Source: Internet
Author: User

# Include <iostream> <br/> using namespace STD; </P> <p> // dynamic planning: 0-1 knapsack problem <br/> // bestvalue [I] [J] = max (bestvalue [I + 1] [J-W [I] + V [I], bestvalue [I + 1] [J]) W [I] <= j <br/> // bestvalue [I] [J] = bestvalue [I + 1] [J] W [I]> j </P> <p> class knapsack <br/> {<br/> PRIVATE: <br/> int * weight; // item weight array <br/> int * value; // item value array <br/> int numofitems; // number of items <br/> int bagspace; // backpack capacity <br/> int ** bestvalue; // dynamic plan table, record the value of bestvalue [I] [J], which is the optimal value. I indicates the item I... n the maximum value that a backpack with a size of J can achieve <br/> int ** path; // to obtain the solution for obtaining the optimal value, record the selection of different table items in the dynamic plan table <br/> Public: <br/> // constructor <br/> knapsack (INT numofitems, int bagspace) <br/>{< br/> Weight = new int [numofitems + 1]; <br/> value = new int [numofitems + 1]; <br/> This-> bagspace = bagspace; <br/> This-> numofitems = numofitems; </P> <p> bestvalue = new int * [numofitems + 1]; <br/> for (INT I = 0; I <numofitems + 1; I ++) <br/>{< br/> bestvalue [I] = new int [bagspace + 1]; <br/>}</P> <p> Path = new int * [numofitems + 1]; <br/> for (INT I = 0; I <numofitems + 1; I ++) <br/>{< br/> path [I] = new int [bagspace + 1]; <br/>}< br/> // enter the weight and value of an item <br/> void input () <br/>{< br/> int I = 1; <br/> while (I <= numofitems) <br/> {<br/> cout <"Enter the" <I <"weight of an item" <Endl; <br/> CIN> weight [I]; <br/> cout <"Enter the" <I <"value of items" <Endl; <br/> CIN> value [I]; <br/> ++ I; <br/>}< br/> // dynamic planning core algorithm <br/> void knapsack () <br/> {<br/> // initialize the bottommost layer of recursion. bestvalue [N] [0: C] is initialized. <br/> for (INT I = 0; I <= bagspace; I ++) <br/>{< br/> If (weight [numofitems] <= I) <br/> {<br/> bestvalue [numofitems] [I] = value [numofitems]; <br/> path [numofitems] [I] = 1; <br/>}< br/> else <br/> {<br/> bestvalue [numofitems] [I] = 0; <br/> path [numofitems] [I] = 0; <br/>}< br/> // recursive dynamic planning, bottom-up, the final bestvalue [1] [bagespace] is the maximum value of 1-N items placed in the capacity bagspace <br/> for (int K = numOfItems-1; k> = 1; k --) <br/> {<br/> for (Int J = 0; j <= bagspace; j ++) <br/> {<br/> bestvalue [k] [J] = bestvalue [k + 1] [J]; <br/> path [k] [J] = 0; // if not put <br/> If (weight [k] <= J) // if the capacity is sufficient, add the current item <br/>{< br/> If (bestvalue [k + 1] [J-weight [k] + value [k]> bestvalue [k] [J]) // if the value to be placed is greater than the value not to be placed <br/>{< br/> bestvalue [k] [J] = bestvalue [k + 1] [J-weight [K] + value [k]; <br/> path [k] [J] = 1; // select <br/>}< br/> // output maximum value., and the output Selection Method <br/> void display () <br/>{< br/> // print out bestvalue [1] [bagspace], indicating 1... the maximum value of numofitems item loading capacity is bagspace <br/> int I = 1; <br/> Int J = bagspace; <br/> cout <"maximum value:" <bestvalue [1] [J] <Endl; <br/> // start with the record of path [1] [bagspace], recursively returning to path [N] [a capacity], to print whether each item is selected into the backpack <br/> while (I <= numofitems) <br/> {<br/> If (path [I] [J] = 0) // If the I item is not put, check that I + 1 items are loaded into the capacity J backpack <br/>{< br/> + + I; <br/>}< br/> else <br/> {<br/> cout <"<weight:" <weight [I] <", value: "<value [I] <"> "Endl; <br/> J-= weight [I]; <br/> ++ I; <br/>}< br/>}; </P> <p> void main () <br/> {<br/> knapsack test (5, 50); // five items, with a backpack capacity of 50 <br/> test. input (); // enter the value and weight of five items. <br/> test. knapsack (); // dynamic plan <br/> test. display (); // print selection and maximum value <br/>}

 

 

Thought-out: to see a question, first look at what to ask. The following is an example of this question.

0-1 backpack Problems

1. What are the requirements for the problem?
A: The maximum value that can be achieved by placing n items in a C-sized backpack.

2. What is an abstract mathematical expression?
A: bestvalue [N] [c] indicates the maximum value of N items in a C-sized backpack.

3. Without considering how to choose algorithms, where did we start to solve this problem?
A: We have n items and a C-capacity backpack. So we started to solve the problem. I put the first item first. If I can put it in, I will put it in. Of course, I can also not put it.
After the first item is processed, we start with the second item. If we can put it in, we can also leave it.
Therefore, this is a decision-making problem. The decision-making is abstracted from our actual handling problems. We can only put items one by one, and the decision-making is put or not put.

4. What are the values of bestvalue [N] [c] in decision-making or not?
A: If we can put them in, we still have C-W [I] and n-1-1 items in our backpack. Of course, we can choose not to put them in, we still have C Capacity and n-1-1 items in our backpack. So let's modify our definition of bestvalue [N] [c] To get a recursive formula of the optimal sub-structure.

For our decision-making, that is, every decision we make is the most I-th item for decision-making, so bestvalue [N] [c] is changed to best [I] [c], indicating I, I + 1, I + 2... the maximum value of placing n items in a C-sized backpack.

Therefore, bestvalue [I] [J] = max (bestvalue [I + 1] [J-W [I] + V [I], bestvalue [I + 1] [J]) W [I] <= J
Bestvalue [I] [J] = bestvalue [I + 1] [J] W [I]> J

Meaning:
If the current capacity J cannot mount item I, then the maximum value of loading J from I to N is equal to the maximum value of loading J from I + 1 to n, which is the second line of the formula.
If the current capacity J can be loaded with item I, we can install it. Of course, we can also make a bid and not install it. Let's see how the result works, therefore, the maximum value of loading I to N items into a J-capacity backpack is equal to the maximum value of loading I + 1 to n items into a J-W [I] capacity backpack + value [I ], the maximum value of I + 1 to n items loaded into a J-capacity backpack is one of the two different decisions.

Summary: what is the solution? Where to start? What are the decisions? What will happen after the decision?

 

The recursion formula is found, which has the optimal sub-structure, that is, it can be simply understood that the current optimum is produced by the sub-problem, and then the sub-problem's optimum is not affected by the current optimum, by observing the recursive formula, we should find the bottom-layer I and J of recursion. We can see that I is gradually increasing and J is gradually decreasing. So when we are performing recursion, first, initialize the bottom layer, and then use the recursive formula to recursive upwards. Therefore, we need to first initialize bestvalue [N] [0: C], that is, to record the maximum value of a backpack with 0 to C for the nth item, when W [N] <= J, bestvalue [N] [J] is equal to value [N]. If W [N]> J, that is, the capacity is insufficient, it is 0.

 

An important reason for our bottom-up recursion is: optimal sub-structure + NO aftereffect. A lot of experience. This is basic understanding.
 

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.