The most understandable 01 backpack in history, full backpack, multi-backpack explanation

Source: Internet
Author: User

Backpack 01 Backpack, full backpack, multi-backpack detailed

PS: We feel that the writing is passable, help me to the top of the blog, thank you.

First of all, dynamic planning, dynamic planning this thing and recursion, can only find local relations, if you want to all listed, it is difficult, such as Hanoi. You can say that all the other layers except the last layer are moved to 2, and then the last layer to move to 3, and finally the rest from 2 to 3, this is an intuitive relationship, but it is difficult to list out, perhaps when the number of n=3 can also be simulated under, and then larger is impossible, so, such as recursion, Dynamic planning and the like, can not think, can only find local relations.

1. Hanoi Pictures

(led to Hangzhou Electric courseware: DP is the most critical state, in the DP when used in the array, that is, the optimal value of each state stored, that is, memory search)

To understand the backpack, you must first understand the dynamic planning:

The dynamic programming algorithm can be decomposed into 4 steps after Binate:

1. Describe the structure of an optimal solution;

2. Recursively define the value of the optimal solution;

3. Calculate the value of the optimal solution in a "bottom-up" manner;

4. Build the path to the optimal solution from the calculated information.

One of the steps is the basis of the dynamic Solver problem. If the title only requires the value of the optimal solution, then step 4 can be omitted.

The basic model of the backpack is to give you a backpack with a capacity of V

Under certain restrictions to the maximum (minimum?) Value of something

Current state → Previous state

Read DD Daniel's "Backpack Nine" (Click to download), confused with a trace of sobriety, here I also summarize the 01 backpack, complete backpack, multi-backpack the use and difference of the three, part will refer to DD Daniel's "backpack Nine", if wrong, welcome to point out.

(www.wutianqi.com message can be)

First of all, we put together three scenarios to see:

01 Backpack (Zeroonepack): There are n items and a backpack with a capacity of V. (Only one piece per item) The cost of item I is c[i] and the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.

Full Backpack (Completepack): There are n items and a backpack with a capacity of V, each item has unlimited pieces available. The cost of item I is c[i] and the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

Multiple backpacks (Multiplepack): There are n items and a backpack with a capacity of V. (i) Items with a maximum of n[i] pieces available, each cost is c[i], the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

Compare three topics, you will find that the difference is the number of each backpack, 01 backpack is only one piece of each, complete backpack is each unlimited pieces, and multi-pack is a limited piece of each.

——————————————————————————————————————————————————————————–

To analyze the 01 backpack First:

01 Backpack (Zeroonepack): There are n items and a backpack with a capacity of V. (Only one piece per item) The cost of item I is c[i] and the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.

This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not put.

In fact, the upper limit of the backpack from the change to 1-m, I for the maximum value of the upper limit

Define the state with sub-question: F[i][v] Indicates the maximum value that the first I item can get in a backpack with a capacity of V. The state transfer equation is: f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}

Understand this process: when the first I item is put into the backpack of capacity V,

It has two conditions:

The first is that I do not put in, then the value is: F[i-1][v]

The second is that the first piece is put in, and the value is: F[i-1][v-c[i]]+w[i]

(The second is what it means.) is if I put in, then in the capacity V-c[i] will be placed in the top I-1 items)

Finally compare the first and the second kind of the value of the size, which is relatively large, f[i][v] the value of which is.

(This is the basis to understand.) )

This is stored in a two-bit array that optimizes space and stores it with one array.

In F[0..V], F[v] represents the value of putting the first I item into a backpack with a capacity of V. After I have cycled from 1~n (n pieces), the last F[v] represents the maximum value to be asked.

* Here F[v] is equivalent to a two-bit array of f[i][v]. So, how to get f[i-1][v] and F[i-1][v-c[i]]+w[i]. Focus Thinking
The first thing to know is that we are using the I from 1 to n loops to indicate the status of the first I item deposited. That is: for I=1..N
Now think about how to be f[v] to indicate that the current state is the value of a backpack with a capacity of V, while making f[v] and F[v-c[i]]+w[i] label the value of the previous state. reverse.

That's the point.

1 2 3 For I=1..N for V=v. 0 F[v]=max{f[v],f[v-c[i]]+w[i]};

Analyze the code above: when the inner loop is in reverse order, you can guarantee that the latter f[v] and F[v-c[i]]+w[i] are the previous state.
Here we give you a set of test data:

Test data:
10,3
3,4
4,5
5,6

This chart is very well-drawn to analyze:

C[V] Starting from the item i=1, loop to the item 3, during which each reverse order gets the maximum value that the volume V can get in the first I-item. (Please draw your own picture on the draft paper)

Among them: according to whether the entire bag is filled, the initial value is not the same, only the maximum values, the initial F are all 0, to fill the entire schoolbag f "0" = 0; the rest are-1;

Here is a specific topic to see:

Title: http://acm.hdu.edu.cn/showproblem.php?pid=2602

Here's the code: http://www.wutianqi.com/?p=533

Analysis:

Based on the above explanations and the Code Analysis I gave. The problem is very basic, understand the above knowledge should be done.

——————————————————————————————————————————————————————————–

Full Backpack:

Full Backpack (Completepack): There are n items and a backpack with a capacity of V, each item has unlimited pieces available. The cost of item I is c[i] and the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

Complete backpack According to its thinking can still use a two-dimensional array to write: f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<=v}

The same can be converted into one-dimensional arrays to represent:

The pseudo code is as follows:

1 2 3 For I=1..N for V=0..V F[v]=max{f[v],f[v-c[i]]+w[i]}

order.

Presumably you see the difference between the 01 backpack, where the inner loop is sequential, and 01 of the backpack is reverse.
Now the key is to consider: why the complete backpack can be written like this.
In the second we first recall, 01 of the reasons for the reverse order. Is that two items in Max are the previous state value, which is the right one.
So here we write in order that two of Max here is of course the value of the current state, why.
Because each kind of backpack is infinite. When we take I from 1 to n Cycles, F[v] represents the value of V in the first I backpack, here we add not the previous backpack, but the current backpack. So what we have to consider is of course the current state.
Here is also a topic for everyone:

Title: http://acm.hdu.edu.cn/showproblem.php?pid=1114

Code: http://www.wutianqi.com/?p=535

(Analysis code is also a way to learn the algorithm, sometimes do not have to look at algorithm analysis, combined with the topic is easier to understand.) )

——————————————————————————————————————————————————————————–

Multiple Backpack

Multiple backpacks (Multiplepack): There are n items and a backpack with a capacity of V. (i) Items with a maximum of n[i] pieces available, each cost is c[i], the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

This topic is very similar to the complete knapsack problem. The basic equation simply changes the equation of the complete knapsack problem slightly, because there are n[i]+1 strategies for item I: Take 0 pieces, take 1 pieces ... Take n[i] pieces. F[I][V] indicates that the first I item fits into the maximum weight of a backpack with a capacity of V, then there is a state transfer equation: f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k<=n[i]}

This is also converted to 01 backpacks:

Normal conversion for a large number of times, it may time out, can be converted to binary (temporarily do not understand, so first not speaking)

For the ordinary. is to have a middle cycle, put j=0~bag[i], said the backpack from the first I take 0 pieces from the enumeration to take bag[i] pieces.

Give an example of:

Title: http://acm.hdu.edu.cn/showproblem.php?pid=2191

Code: http://www.wutianqi.com/?p=537

Because limited to personal ability, I can only tell a general, please be specific or take a good look at DD Daniel's "Backpack Nine talk."

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.