Full backpack in Vernacular

Source: Internet
Author: User

With the previousVernacular backpack 01 backpackTo see what a backpack is. I hope I can see it clearly in the future. It is best to help the children's shoes I just got in touch.

I. About full backpacks

There are n items. Each item (with an infinite number of items) has a weight of W [I] and a value of VA [I]. There is a backpack that can carry M-weight items. Now, you can choose some items from the N-hour items. If the maximum number of items in the backpack is not exceeded, the maximum value of backpacking is made.

Ii. Preliminary understanding of the complete backpack algorithm

Now let's look at the state transition equation:

DP [I] [J] = max {DP [I-1] [J-K * W [I] + K * va [I] | 0 <= K * va [I] <= m}It is understood that when considering the I items, the remaining size of the backpack is the biggest benefit of J.

Look at the code: explain it clearly and then optimize it.

for(int i = 1; i <= N; i ++){    for(int j = w[i]; j <= M; j ++){        int Max = 0;        for(int k = 0; k*w[i] <= M; k ++){            Max = max(Max,dp[i-1][j-k*w[i]]+k*va[i]);        }        dp[i][j] = Max;    }}
Let's take a look at how this operation is performed. For example, if the first item W [1] = 3, VA [1] = 2; then we execute the above Code, such:


Consider the scenario of DP [1] [3] first. We find that here, K loops can only take 1, so that DP [1] [3] = DP [0] [3-1*3] + 1*2 = DP [0] [0] + 2 = 2; meaning, consider the first item. If the size of the backpack is 3, we can put the first item (if it is more than 3), which maximizes the benefits in this situation.

Similarly, when considering the first item, the other status of the backpack is updated as follows:


We can find that only the first item can be placed until the backpack capacity is 6, so when the capacity is 6, we can just put two


So here we can find that, in fact, the status of the first item, capacity of 6 DP [1] [6], can be obtained in the status of DP [1] [3, and other statuses are similar...

3: we can introduce optimized one-dimensional arrays and reduce the time complexity.

First, let's look at the algorithm of the one-dimensional array:

for(int i = 1; i <= N; i ++){    for(int j = w[i]; j <= M; j ++){        dp[j] = max(dp[j],dp[j-w[i]]+va[i]);    }}

The other States mentioned above are similar. How can they be similar ?, Figure:


So, it turns



When DP [6] is updated, you can use DP [3] + va [1]. This is similar when you consider other items. So here is the difference between the 01 backpack... That is, the for loop is small to large, because when considering the current status DP [J], we must rely on the existing status (that is, we must have put this item in front of it. Let's put another item to see if we can get more benefits.) to update the current state.

OK. The backpack is complete.

Personal ignorance. Thank you for your correction and discussion.

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.