Backpack problem summary

Source: Internet
Author: User
Document directory
  • Problem description
  • Basic Ideas
  • Optimize space complexity
  • Initialization details
  • Problem description
  • Conversion to 0-1 backpack
  • Further Optimization-O (CN) Solution
This article mainly comes from the nine lectures on backpacks. I have selected a simple 0-1 backpack problem and a summary of the full backpack problem. I added the Python code implementation and just reinstalled the system, there is no C compiler at hand, Khan.
I. Overview of knapsack problems

This includes the problem of 0-1 backpacks, full backpacks, and some other variants. Among them, the simplest is part of the backpack problem, which can be solved by greedy method, while several other backpack problems often need dynamic planning to solve. This article summarizes several knapsack problems and provides the implementation code. If any error occurs, please correct me.

Ii. Some backpack problems part of the Problem description: There are n items and a C-capacity backpack. The weight of the I-th item is W [I], and the value is V [I]. Solving which items are loaded into a backpack can maximize the total value. Note that you do not need to load the entire item here. You can only load the part of an item.Some knapsack problems are often solved using greedy algorithms. Calculate the weight value per unit for each item first V [I]/W [I], take the item with the largest unit value, and then take the item with the second largest value until it is filled with a backpack. According to this greedy strategy, the sum of value is the greatest. This implementation code is simple, so I will skip it here. Iii. Problem description for 0-1 backpacks: There are n items and a C-sized backpack. The weight of the I-th item is W [I], and the value is V [I]. Solving which items are loaded into a backpack can maximize the total value. Note that items can only be taken or not taken. This is exactly what 0-1 means.We can regard some backpacks as gold powder, while the 0-1 backpack is gold block, which can be divided into one and cannot be split. The basic idea is that this is the most basic problem with a backpack. It features that each item has only one item, and you can choose to put it or not. Define the state with a sub-problem: that is, F [I] [W] indicates the maximum value that can be obtained by placing the first I item into a C-sized backpack. The state transition equation is:
f[i][c]=max{f[i-1][c],f[i-1][c-w[i]]+v[i]} 

This equation is very important. Basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a C-sized backpack, if you only consider the I-th item Policy (put or not put), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "pre-i-1 items into the capacity of V backpack", the value is f [I-1] [c];
If I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity C-W [I ", the greatest value that can be obtained at this time is f [I-1] [C-W [I] plus the value V [I] obtained by placing the I item. The time and space complexity of the above methods are O (CN). The time complexity should no longer be optimized, but the space complexity can be optimized to O (n ). Because when calculating f [I] [c], we only need to use F [I-1] [c] and f [I-1] [C-W [I], therefore, they can be stored in a one-dimensional array. The trick here is to use C = C... 0 start to push back, this ensures that f [c] saves the value of F [I-1] [C-W [I] When F [c] is obtained.Note: it cannot start from C = 0... c, this causes f [C-W [I] to have a value of F [I] [C-W [I] instead of F [I-1] [C-W [I]. The lower limit can be optimized here. In fact, you only need to use C = C... W [I] to avoid unnecessary computation.The pseudocode is as follows:

for i=0..N-1    for c=C..w[i]        f[c]=max{f[c],f[c-w[i]]+v[i]};

The Python code for Algorithm Implementation is as follows:

Def knap01 (N, C): // num indicates the number of items, C indicates the backpack capacity, W [I] indicates the Weight of item I, and V [I] indicates the value of item I, f is the result list for I in range (0, n): For C in range (C, W [I]-1,-1 ): f [c] = max (F [c], F [C-W [I] + V [I])

You can use the complete code below for testing:

W = [3, 4, 5] // item weight list v = [4, 5, 6] // item Value List F = [] def Init (c ): // initialize the list of saved results. f del f [:] For I in range (0, C + 1): f. append (0) print 0, F def knap01 (N, C): // 0-1 backpack main function for I in range (0, n): For C in range (C, W [I]-1,-1): F [c] = max (F [c], F [C-W [I] + V [I]) print I + 1, FIF _ name _ = "_ main _": num, totalcapacity = 3, 10 // specify the number of items and the size of the backpack Init (totalcapacity) knap01 (Num, totalcapacity)

The test results are as follows:

Initialization details

We can see that there are actually two different ways to solve the problem of a backpack. Some questions require the optimal solution when "just full of backpacks", and some questions do not require that the backpack be full. One difference is that the implementation methods of these two questions are different during initialization.

If the first method is used, it is required to be filled with a backpack. during initialization, F [0] is 0, and f [1 .. c] is set to-∞, which ensures that the final f [N] is an optimal solution just filled with a backpack.If you do not need to fill your backpack, but only want the price to be as high as possible, you should set f [0. C] to 0 during initialization.

Why? It can be understood as follows: The initialized F array is actually the legal status when no item can be put into a backpack. If a backpack is required to be filled, only a backpack with a capacity of 0 may be filled with nothing with a value of 0. There is no legal solution for a backpack with other capacities, for undefined states, their values should all be-∞. If the backpack does not have to be filled, then any capacity of the backpack has a legal solution: "Nothing is loaded." the value of this solution is 0, therefore, the initial status values are all 0.


4. Full backpack Problem description There are n items and a C-sized backpack, each of which has unlimited items available. The weight of the I-th item is W [I], and the value is V [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.
Basic Ideas

This problem is very similar to the 0-1 backpack problem. The difference is that each item has unlimited items. That is to say, from the perspective of each item, there are no or no two policies related to it, but 0, 1, and 2 ...... And so on. If you still follow the troubleshooting idea of 01, F [I] [c] indicates that the first I items are placed in the maximum weight of a C-sized backpack. You can still write the state transition equation based on different policies for each item, as shown in the following code:

 f[i][c]=max{f[i-1][c-k*w[i]]+k*w[i]| 0<=k*w[i]<=c }

This is the same as the 0-1 backpack problem. The O (CN) State needs to be solved, but the time for solving each State is no longer a constant, the time for solving the state f [I] [c] is O (C/W [I]), the overall complexity can be considered as O (Cn * Σ (C/W [I]), which is relatively large. The implementation code is as follows:

##### Solution to complete knapsack problems 1 ######## def knap_complete (N, C): for I in range (0, n ): for C in range (C,-1,-1): For k in range (0, C/W [I] + 1 ): if K * W [I] <= C: F [c] = max (F [c], f [c-k * W [I] + K * V [I])

The following is an example of the same problem with a 0-1 backpack ):

Conversion to 0-1 backpack

Since the question 01 is the most basic question about a backpack, we can consider converting the question of a full backpack into a question 01 about a backpack. The simplest idea is to select at most C/W [I] items for the I-th item, therefore, the I-th item can be converted into items with the same cost and value as C/W [I] items, and then the 01 backpack problem can be solved. In this way, there is no time complexity to improve the basic idea, but this gives us the idea of converting a full backpack problem into a 01 backpack problem: Splitting an item into multiple items.

A more efficient conversion method is to split the I-th item into several items with a weight of W [I] * 2 ^ K and a value of W [I] * 2 ^ K, k meetsw[i]*2^k<=C. This is a binary idea, because no matter how many items of the I-th item are selected in the optimal strategy, they can always be expressed as the sum of several 2 ^ K items. In this way, splitting each item into an O (log C/W [I]) item is a great improvement.However, we have a better O (CN) algorithm.


Further Optimization-O (CN) Solution

We can traverse the problem in the reverse order of the 0-1 backpack to obtain the solution of O (CN). The pseudocode is as follows:

for i=0..N-1    for c=w[i]..C        f[c]=max{f[c],f[c-w[i]]+v[i]};

This pseudo-code is different from the pseudo-code of the 0-1 backpack, but the sequential order of C is different. The reason why a 0-1 backpack needs to be circulated in the reverse order of V = V .. 0. This is because we need to ensure that the State f [I] [c] in the I-th loop is recursive from the state f [I-1] [C-W [I. In other words, this is to ensure that each item is selected only once, and to ensure that the policy of "selecting the I-item" is considered, it is based on a sub-result f [I-1] [C-W [I] That has never been selected for item I. Now, the unique feature of a backpack is that each type of item can be an unlimited number of items. Therefore, when considering the policy of "adding a first item, however, you need a sub-result f [I] [C-W [I], so we can use C = W [I] .. c. This is why this simple program is established. The Python implementation code is as follows:

##### Solution to a complete backpack problem 2 ######## def knap_complete_2 (N, C): for I in range (0, n ): for C in range (W [I], C + 1): F [c] = max (F [c], f [C-W [I] + V [I])

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.