Explanation of backpack 01, full, and multiple
PS: If you think you can write well, just help me pin my blog. Thank you.
First, let's talk about dynamic planning. Dynamic Planning is just like recursion. We can only find local relationships. If we want to list them all, it is very difficult, such as the tower of Hanoi. You can say that it is an intuitive relationship to move all the layers except the last layer to 2, move the last layer to 3, and then move the rest from 2 to 3, however, it is very difficult to list them. It may be possible to simulate them when the number of layers N is 3. Therefore, recursive, dynamic planning, and so on cannot be considered in detail, you can only find local links.
1. tower pictures
(Import to hangdian courseware: the most critical aspect of DP is the state. When an array is used in DP, It is the optimal value of each State stored, that is, the memory-based search)
To learn about backpacks, you must first understand the dynamic planning:
Dynamic PlanningAlgorithmIt can be divided into four steps:
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 the "bottom-up" mode;
4. Build the optimal path from the calculated information.
Step 1 ~ 3. It is the basis for dynamic planning and solving problems. If the question only requires the value of the optimal solution, step 4 can be omitted.
The basic model of a backpack is to give you a backpack with a capacity of v.
Put at most (minimum?) under certain restrictions ?) Value
Current Status → previous status
I saw dd Daniel'sBackpack 9 lecture (click to download)I am also summing up the usage and differences between the 001 backpack and the full backpack and the multiple backpacks. Some of them will reference dd Daniel's backpack 9 lecture. if there are any mistakes, please note.
(Leave a message at www.wutianqi.com)
First, let's take a look at three cases:
01 backpack (zeroonepack): There are n items and a backpack with a capacity of v. (Each item has only one) The cost of the I-th item is C [I], and the value is W [I]. Solving which items are loaded into a backpack can maximize the total value.
Completepack): There are n items and a backpack with a capacity of V,Each item has an unlimited number of items available. The cost of the I-th item is C [I], and the value is W [I]. 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.
Multiplepack): There are n items and a backpack with a capacity of v.A maximum of N [I] items are available for the I-th itemThe cost per part is C [I], and the value is W [I]. 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.
Comparing the three questions, we will find that the difference is the quantity of each type of backpack. 01 each type of backpack has only one, and each type of backpack is infinite, while multiple backpacks are each finite.
-----------------------------------------------------------
First analysis01Backpack:
01 backpack (zeroonepack): a backpack with N items and a capacity of v. (Each item has only one.) The cost of the I-th item is C [I], and the value is W [I]. Solving which items are loaded into a backpack can maximize the total value.
This is the most basic problem with a backpack. It features that each item has only one item, and you can choose to put it or not.
Define the state with a subproblem: that is, F [I] [v] indicates the maximum value that a backpack with a capacity of V can obtain when the first I item is placed. The state transition equation is:
F [I] [v] = max {f [I-1] [v], F [I-1] [V-C [I] + W [I]}
Understand this process: when I put the first item into a backpack with a capacity of V,
It has two scenarios:
The first is that the I part is not put in, then the value obtained is: F [I-1] [v]
The second is put in the I part, and the resulting value is: F [I-1] [V-C [I] + W [I]
(What does the second type mean? That is, if the I part is put in, then in capacity V-C [I] should put in front of the I-1 item)
Finally, compare the size of the first and second values, which is relatively large, and the value of F [I] [v.
(This is the foundation and you need to understand it !)
Here we use two-digit array storage, which can optimize the space and store it with one array.
In F [0. V], F [v] indicates the value of placing the first I item in a backpack with a capacity of v. From 1 ~ After n (n pieces) loops, F [v] indicates the maximum value.
* F [v] is equivalent to f [I] [v] of the two arrays. So how to get f [I-1] [v] and f [I-1] [V-C [I] + W [I]? (Important! Thinking)
First of all, we need to know that the first I items are saved in order through the I from 1 to n loop. That is, for I = 1 .. n
Now I want to think about how to use F [v] to indicate that the current State is the benefit of a backpack with a capacity of v, what makes f [v] and f [V-C [I] + W [I] labels the value of the previous State?
Reverse Order!
This is the key!
For I = 1 .. N for V = V .. 0 f [v] = max {f [v], F [V-C [I] + W [I]};
Analyze the aboveCode: When the inner loop is in reverse order, we can ensure that the last f [v] and f [V-C [I] + W [I] are in the previous state!
Here is a set of test data:
Test data:
10, 3
3, 4
4, 5
5, 6
This chart is well drawn for analysis:
C [v] starts from item I = 1 and loops to item 3. During this period, the maximum capacity V can obtain when it comes to the first item. (Please draw a picture on the draft.)
Here is a specific question:
Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2602
Code here: http://www.wutianqi.com /? P = 533
Analysis:
Based on the above explanation and the code analysis I have provided. This question is very basic. you should understand the above knowledge.
-----------------------------------------------------------
Full backpack:
Completepack: There are n items and a backpack with a capacity of V, each of which has an unlimited number of items available. The cost of the I-th item is C [I], and the value is W [I]. 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.
A two-dimensional array can still be used to write a full backpack based on its ideas:
F [I] [v] = max {f [I-1] [V-K * C [I] + K * W [I] | 0 <= K * C [I] <= V}
It can also be converted to a one-dimensional array for representation:
The pseudocode is as follows:
For I = 1 .. N for V = 0 .. V f [v] = max {f [v], F [V-C [I] + W [I]}
Order!
I think you can see the difference from the 01 backpack. The inner circle here is in order, while the 01 backpack is in reverse order.
Now, the key thing is to consider: Why is it possible to write a full backpack like this?
Let's first recall the reason why the backpack is in reverse order? So that the two items in Max are the previous State values, which is correct.
Here, we write it in sequence. The two items in Max here are of course the values of the current State. Why?
Because each type of backpack isUnlimited. When we take the I from 1 to n cycles, F [v] indicates that the capacity is the value v gained from the first I type of backpack. What we want to add here is not the previous backpack, but the current backpack. Therefore, we must consider the current status.
Here is also a question for everyone:
Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1114
Code: http://www.wutianqi.com /? P = 535
(Code analysis is also a way to learn algorithms. Sometimes it doesn't have to look at algorithm analysis, but it is easier to understand it by combining the questions .)
-----------------------------------------------------------
Multiple backpacks
Multiplepack: a backpack with N items and a capacity of v. A maximum of N [I] items are available for the I-th item. The cost per item is C [I] and the value is W [I]. 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.
This is similar to a full backpack problem. The basic equation only needs to slightly change the equation of the complete backpack problem, because there are N [I] + 1 strategy for the I-th item: Take 0, take 1 ...... N [I] parts. If f [I] [v] indicates that the first I items are placed in the maximum weight of a backpack with a capacity of V, there is a state transition equation:
F [I] [v] = max {f [I-1] [V-K * C [I] + K * W [I] | 0 <= k <= N [i]}
Here the conversion is also 01:
For a large number of conversions, the conversion may time out and be binary (unknown for the time being, so do not talk about it first)
For common. Is an intermediate loop, j = 0 ~ Bag [I] indicates that the backpack in Section I is enumerated from 0 to bag [I.
An example is provided:
Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2191
Code: http://www.wutianqi.com /? P = 537
Due to my individual abilities, I can only give a rough picture. Please take a good look at dd Daniel's "backpack 9 lecture".
After the lecture is complete, I will continue to improve the materials for you to study and discuss. (My blog: www.wutianqi.com. If you have any questions or the content in the documents is incorrect, please leave a message. Thank you for your support .)
Original article: (Word version)
Http://download.csdn.net/source/2587577
<HR>
personal originality, reprinted please indicate the link: http://www.wutianqi.com /? P = 539