Multiple Backpack 2016.5.8

Source: Internet
Author: User

First, the topic

There are n items and a backpack with a capacity of V

I can use up to Mi pieces, each space is CI, the value is W

Solve what items are loaded into the backpack so that the total amount of space spent on these items does not exceed the backpack capacity, and the total value of the largest


Second, the basic algorithm

It's similar to a complete backpack problem.

The basic equation simply changes the equation of the complete knapsack problem slightly.
Because there are mi+1 strategies for article I:

Take 0 pieces, take 1 pieces ... Take Mi Pieces

Dp[i, V] indicates the maximum value of the first I object in a backpack with a capacity of V, then there is a state transition equation:
Dp[i, V] = max{dp[i? 1, v? k? Ci] + k? Wi | 0≤K≤MI}
Complexity of O (V *σmi)


3, converted to 01 knapsack problem

Another good way to write the basic method is to convert to 01 Knapsack solution:

Replace the item I in the Mi piece 01 backpack items, then get the number of items for Σmi 01 knapsack problem

Direct solution, the complexity is still O (V *σmi)


But we're looking to turn it into a 01 knapsack problem, to reduce complexity like a full backpack.


Still considering the idea of binary, we consider replacing item I with a number of items

Each strategy that makes the article I in the original question-take 0...Mi-can be equivalent to taking several items after substitution

In addition, the strategy to take more than MI pieces must not appear


The method is:

Divide item I into several 01 items in the backpack, each of which has a factor

The cost and value of this item are the original cost and value multiplied by this factor

These coefficients were 1, 2, 2^2 ... 2^ (k?1), Mi? 2^k + 1, and K is satisfied Mi? 2^k + 1 > 0 Maximum integer

For example, if the MI is 13, then the corresponding k = 3, the item with a maximum of 13 pieces should be divided into 2 items with coefficients of 1, 4, 6, four respectively
The coefficients of these items and for MI, which indicate that it is not possible to take more than MI pieces of article I items

In addition this method can also guarantee for 0 ... Each integer in MI can be represented by a number of coefficients

Here the proof of algorithm correctness can be divided into 0 ... 2k?1 and 2k ... Mi Two section to discuss separately


In this way, the article I is divided into O (logmi) items, the original problem into the complexity of O (vσlogmi) 01 knapsack problem, is a great improvement


The following is an O (LOGM) time process for handling items in a multiple backpack:
def multiplepack (DP, C, W, M)
If C. M≥v
Completepack (DP, C, W)
Return
K: = 1
While K < M
Zeroonepack (KC, KW)
M: = m? K
K: = 2k
Zeroonepack (C. M, W. M



Four, O (V * N) algorithm

Multi-knapsack problem also has O (V N) complexity algorithm

This algorithm is based on the state transfer equation of the basic algorithm

However, the method of applying a monotone queue allows the value of each state to be solved at a time of averaging O (1)


This method is on the "Eight Men" slide in the building.


Explanation of the monotone queue:


V. Summary

In this talk, we see the process of improving the complexity of an algorithm from O (vσmi) to O (Vσlogmi)

And I know the existence of an algorithm with an O (V N) complexity


Pay special attention to the idea and method of "splitting items", prove its correctness, and write out the complete program code



From "Backpack Nine v_2.0"%%% author, thank the author














Multiple Backpack 2016.5.8

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.