Full backpack problem (implemented in Java)

Source: Internet
Author: User

Http://www.concretevitamin.com.cn/informatics/Pack/P02.html

 

Question

There are n items and a backpack with a capacity of V, each of which has unlimited items available. The cost of the I-th item is C [I], and the value is W [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 similar01 backpack ProblemsThe difference is that each item has an infinite number of 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, make f [I] [v] to indicate that the first I items are placed in the maximum weight of a backpack with a capacity of v. You can still write the state transition equation based on different policies for each item, as shown in the following code:

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

This is the same as the 0th backpack problem. There are O (VN) states that need to be solved, but the time for solving each State is no longer a constant, the time for solving the state f [I] [v] is O (V/C [I]), the overall complexity can be considered O (V * Σ (V/C [I]), which is relatively large.

I improved the basic idea of the backpack problem and obtained such a clear method. This shows that the equation of the 01 knapsack problem is indeed very important, and can be considered as another type of knapsack problem. But we are still trying to improve this complexity.

A simple and effective optimization

There is a very simple and effective optimization for the full backpack problem, as shown in the following code: if two items I and j meet the requirements of C [I] <= C [J] and w [I]> = W [J], remove item j. The correctness of this optimization is obvious: under any circumstances, you can replace the small-cost high-value J with inexpensive I, and get at least no worse solution. For randomly generated data, this method often greatly reduces the number of items, thus accelerating the speed. However, this does not improve the complexity of the worst case, because it is possible that specially designed data cannot be removed from an item.

This optimization can be implemented in a simple O (N ^ 2) manner and is generally acceptable. In addition, one of the better solutions for the knapsack problem is: first, remove the items with a higher cost than V, and then use a similar counting sorting method, calculate which of the items with the same cost has the highest value. The optimization can be completed by O (V + n. This unimportant process does not provide pseudocode. I hope you can think about writing pseudocode or programs independently.

Convert to 01 solve the Knapsack Problem

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 V/C [I] items for the I-th item, therefore, you can convert the I-th item into an item with the same cost and value as the V/C [I] item, and then solve this 01 backpack problem. 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 cost of C [I] * 2 ^ K and a value of W [I] * 2 ^ K, k meetsc[i]*2^k<=V. 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 V/C [I]) item is a great improvement.

But we have a better O (VN) algorithm.

O (VN) Algorithm

This algorithm uses a one-dimensional array. first look at the pseudo code:

for i=1..N    for v=0..V        f[v]=max{f[v],f[v-cost]+weight}

You will find that this pseudocode correspondsP01The pseudo-code only has a different loop order of v. Why is this change feasible? First, let's think about why the V = V .. 0 in p01 should be reversed. This is because we need to ensure that the State f [I] [v] in the I-th loop is recursive from the state f [I-1] [V-C [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] [V-C [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] [V-C [I] that may have been selected for Type I. Therefore, you can use V = 0 .. v. This is why this simple program is established.

It is worth mentioning that the order of the two-layer for loop in the above pseudo code can be reversed. This conclusion may lead to optimization of the algorithm time constant.

This algorithm can also be derived from other ideas. For example, explicitly write the state transition equation for solving f [I] [V-C [I] in the basic idea into the original process, we will find that this equation can be equivalent to this form:

f[i][v]=max{f[i-1][v],f[i][v-c[i]]+w[i]}

This equation is implemented using a one-dimensional array and the above pseudo code is obtained.

Finally, the process of abstracting the source to handle a complete backpack-type item is pseudocode:

procedure CompletePack(cost,weight)    for v=cost..V        f[v]=max{f[v],f[v-c[i]]+w[i]}
Summary

The complete knapsack problem is also a very basic knapsack problem. It has two state transfer equations, which are given in the section "basic idea" and "O (VN) algorithm" respectively. I hope that you can carefully understand these two state transition equations, not only remember, but also understand how they come out. It is best to think of a method to obtain these equations on your own. In fact, it is a good way to deepen understanding of dynamic planning and improve the skill of dynamic planning to think about the significance of its equation and how to obtain it.

 

 

 

  1. Public class completepack {
  2. Public completepack (INT v ){
  3. This. V = V;
  4. This. maxvalues = new int [This. V + 1];
  5. }
  6. Private int V;
  7. Private int [] maxvalues;
  8. Public int add (goods Goods ){
  9. If (goods. getcost ()> V ){
  10. Return getmaxvalue ();
  11. }
  12. For (INT v = goods. getcost (); V <= V; V ++ ){
  13. Maxvalues [v] = math. Max (maxvalues [v],
  14. Maxvalues [V-goods. getcost ()] + goods. getvalue ());
  15. }
  16. Return getmaxvalue ();
  17. }
  18. Public int getmaxvalue (){
  19. Return maxvalues [v];
  20. }
  21. /**
  22. * @ Param ARGs
  23. */
  24. Public static void main (string [] ARGs ){
  25. Completepack pack = new completepack (100 );
  26. For (INT I = 0; I <10; I ++ ){
  27. Goods G1 = new goods (INT) (math. Random () * 100), (INT) (math
  28. . Random () * 100 ));
  29. Pack. Add (G1 );
  30. System. Out. println (g1.getcost () + "," + g1.getvalue () + "MAX :"
  31. + Pack. getmaxvalue ());
  32. }
  33. }
  34. }
Related Article

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.