"Algorithmic problem" 0-1 knapsack problem

Source: Internet
Author: User

0-1 backpack problem: A thief in the theft of a shop, found that there are n items, the value of the article I item VI, heavy WI-lb, where VI and WI are integers. He hopes to take away the goods the more valuable the better, but his backpack can only be loaded W-pounds of Things, W is an integer. What kind of things should I take? This problem is called the 0-1 backpack, because each item or is taken away, or is left, the thief can not only take a part of an item or take the same item two times.

In the fractional (partial) knapsack problem (fractional knapsack problem) , the scene is the same as the above problem, but thieves can take part of the item without having to make a 0-1 binary choice. You can think of the 0-1 knapsack problem as a gold ingot, and one of the items in some of the questions is more like Jinsha.

The two kinds of knapsack problems have the best sub-structure properties. For the 0-1 knapsack problem, consider the highest-value package solution with a weight not exceeding W. If we remove the product J from this scenario, the remaining item must be the highest value option with a weight of not more than W-WJ (thieves can only choose from n-1 items that do not include the commodity j).

Although the two problems are similar, but we can solve the knapsack problem with the greedy strategy, but not the 0-1 knapsack problem, in order to solve the problem of part number knapsack, we first calculate the value per pound of each product vi/wi. Following a greedy strategy, thieves first try to take as much of the highest value per pound as possible, and if the item is all taken away and the backpack is not full, he continues to take as much of his second-highest value as possible, and so on, until it reaches the upper weight of W. Thus, the time-to-run time of the greedy algorithm is O (NLGN) by sorting the goods by the value per pound.

In order to illustrate the greedy greedy strategy on the 0-1 knapsack problem is invalid, consider the problem example shown. This example contains 3 items and a backpack that can hold 50 pounds of weight. Merchandise 1 weighs 10 pounds and is worth $60. Merchandise 2 weighs 20 pounds and is worth $100. Merchandise 3 weighs 30 pounds and is worth $120. As a result, the value of 1 per pound of goods is $6, higher than the value of 2 per pound of goods worth $5 and 3 per pound of commodity 4. Therefore, the above greedy strategy will first take the goods 1. However, the optimal solution should be goods 2 and 3, leaving the goods 1. The two options for taking the merchandise 1 are suboptimal.

However, for the fractional knapsack problem, the above greedy strategy takes the product 1 first, it can generate the optimal solution. The strategy of taking the product 1 is not valid for the 0-1 knapsack problem because the thief cannot fill the backpack, and the free space reduces the effective per pound value of the scheme. In the 0-1 knapsack problem, when we consider whether to load a product into a backpack, we must compare the solution of the sub-problem containing the product with the solution of the sub-problem that does not contain it before making a choice. This results in a large number of overlapping sub-issues-the identity of the dynamic plan.

example : 0-1 knapsack problem. A total of three items, the backpack can accommodate 5 pounds of things, items 1 weight 1 pounds, worth 60 yuan. Item 2 weighs 2 pounds, value 100 yuan, item 3 weighs 3 pounds, value 120 yuan. How to maximize the value of the items in the backpack.

Answer : We can draw an item worth $60 per pound, more than $50 per pound for item two and 40 yuan per pound for item 3. If you follow the greedy algorithm, you need to take item 1. However, the optimal solution should take items 2 and 3, leaving 1.

The reason for not taking item 1 in the 0-1 knapsack problem is that it does not fill the backpack, and the free space reduces the effective value of the goods per pound.

We can use dynamic programming to solve 0-1 knapsack problems.

Suppose c[i] denotes the weight of the article I, w[i] denotes the value of the article I, f[i][j] means the backpack capacity is J, the optional item is the item 1~i, the backpack can get the maximum value.

Using dynamic programming to find out the maximum value that can be obtained when the knapsack capacity is small, and then find out the result when the backpack capacity is relatively small, that is, a recursive filling process.

When there is no optional item, the maximum value a backpack can get is 0. That is, the table can be initialized to

   

The process of filling the form (i.e. the state transition equation) is:

   

"J<c[i]" means that the weight of item I is greater than the capacity of the current Backpack J, this time obviously do not put article I items. The following explains the meaning of the above equation in the "other" case: "Put the first I item into the capacity of J backpack" This problem, if only consider the article I item put or not put, then can be converted to only the first I-1 items of the problem, namely:

1. If you do not place the item I, then the problem is converted to only "the first I-1 items into a backpack of capacity J"

2. If you put the article I item, then the problem translates into "The first I-1 items into the remaining capacity of j-c[i] in the backpack", the maximum value can be obtained at this time is f[i-1][j-c[i]], plus the value obtained by putting the items in the article I obtained value w[i].

In the "Other" case, F[i][j] is the largest value in 1, 2.

Obviously, from the lower left corner of the state transfer equation to fill in order to get f[3][5] (indicating that the optional items are 1, 2, 3, and the backpack capacity of 5 o'clock, the maximum value can be obtained), visible dynamic planning is indeed a recursive process. After filling in the form as shown in the following table:

    

The code is as follows:

intN =3, V =5;//N is the number of items, V is the backpack capacityintc[4] = {0,1,2,3};intw[4] = {0.60.100.120}; for(i =0; I <= V; i++) {//Step-by -step, I indicates the current number of items available, J indicates the capacity of the current backpackf[i][0] =0;  for(j =1; J <= V; J + +) {        if(J <C[i]) {F[i][j]= f[i-1][j]; } Else{F[i][j]= Max (f[i-1][J], f[i-1][j-c[i]] +W[i]); }    }}

  

Knapsack problem Initialization

In the knapsack problem of finding the optimal solution, there are actually two different kinds of questions. Some of the topics require "just fill the backpack" when the optimal solution, and some of the problems are not required to fill the backpack. The difference between the two methods is that the initialization of the solution is different.

If this is the first method of asking for a full backpack, then at initialization, except F[0][0] is 0, the other F[0][1~V] are set to-∞ so that the final explanation can be packaged in an optimal solution filled with a backpack.

If it is not required to fill the backpack, but only want the price as large as possible, the initialization should be F[0][1~V] all set to 0.

What is this for? It can be understood that the initialized F-array is in fact the legal state when no item can be placed in the backpack. If the backpack is full, then only the capacity of 0 of the backpack can be loaded at nothing and the value of 0 is "just full", the other capacity of the backpack are not valid solution, belongs to the undefined state, should be assigned to the value of-∞. If the backpack is not to be filled, then any capacity of the backpack has a legitimate solution "nothing", the value of this solution is 0, so the initial state value is all 0.

  

"Algorithmic problem" 0-1 knapsack problem

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.