P06: group backpack Problems

Source: Internet
Author: User
P06: group backpack Problems

There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. These items are divided into several groups. items in each group conflict with each other and you can select at most one item. 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.

Algorithm

This problem becomes that there are several strategies for each group of items: whether to select one item in the group or not. That is to say, if f [k] [v] is set, it indicates the maximum weight value that can be obtained for the first K items in the Group. The options are:

F [k] [v] = max {f [k-1] [v], f [k-1] [V-C [I] + W [I] | item I belongs to group k}

Use the pseudo-ID of a one-dimensional arrayCodeAs follows:

For all group K
For v = V .. 0
For all I belong to group K
F [v] = max {f [v], F [V-C [I] + W [I]}

Pay attention to the order of the three-tier loop here, even in the first beta version of this article, I wrote an error myself. The loop "for V = V .. 0" must be outside of "for all I belong to group K. In this way, only one item in each group can be added to the backpack.

In addition, it is obvious that "a simple and effective optimization" can be applied to items in each group in p02 ".

Summary

The grouping of backpacks refers to several items mutually exclusive to each other as a group, which establishes a good model. The deformation of many knapsack problems can be converted into a group's backpack problem (for example, p07). The concept of "Generalized items" can be further defined by the group's backpack problem, which is very helpful for solving the 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.