Backpack 01 Backpack, full backpack, multi-backpack detailed

Source: Internet
Author: User

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.

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

First to analyze the backpack :

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.

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]}

把这个过程理解下:在前i件物品放进容量v的背包时,

它有两种情况:

第一种是第i件不放进去,这时所得价值为:f[i-1][v]

第二种是第i件放进去,这时所得价值为:f[i-1][v-c[i]]+w[i]

(第二种是什么意思?就是如果第i件放进去,那么在容量v-c[i]里就要放进前i-1件物品)

最后比较第一种与第二种所得价值的大小,哪种相对大,f[i][v]的值就是哪种。

(这是基础,要理解!)

这里是用二位数组存储的,可以把空间优化,用一位数组存储。

用f[0..v]表示,f[v]表示把前i件物品放入容量为v的背包里得到的价值。把i从1~n(n件)循环后,最后f[v]表示所求最大值。

* 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 it can be f[v] that the current state is the value of a backpack with a capacity of V, while the f[v] and F[v-c[i]]+w[i] label the value of the previous state?

reverse order!

That's the point!

123 fori=1..N   forv=V..0        f[v]=max{f[v],f[v-c[i]]+w[i]};

Analyze the above code: when the inner loop is in reverse order, you can guarantee that the latter f[v] and F[v-c[i]]+w[i] is 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)

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}

同样可以转换成一维数组来表示:

伪代码如下:

123 fori=1..N    forv=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 next time we recall, 01 of the reasons for the reverse order of the backpack? Is that two items in Max are the previous state value, which is the right one.
So here, in order, we write 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 transition 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."

After a short talk, with more in-depth understanding, I will continue to improve the information for you to study together. (My blog: www.wutianqi.com If everyone has a problem or the contents of the material is wrong, you can leave a message, thank you for your support. )

Original: (Word version)
http://download.csdn.net/source/2587577

Personal original, reproduced please indicate this article link: http://www.wutianqi.com/?p=539

Backpack 01 Backpack, full backpack, multi-backpack detailed

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.