Lecture 101Backpack Problems
This is the most basic problem with a backpack. Each item can be placed at most once.
Lecture 2Complete backpack Problems
The second basic model of a backpack problem, where each item can be placed unlimited times.
Lecture 3Multiple knapsack problems
Each item has a fixed maximum number of times.
Lecture 4Mixed three backpack Problems
Combine the preceding three simple problems into complex ones.
Lecture 5Two-dimensional knapsack problems
A simple common extension.
Lecture 6Group knapsack problems
A question type is also a useful model. The basis of the last two sections.
Lecture 7Dependent backpack Problems
Another method is to restrict the selection of items.
Lecture 8Generalized item
My own thoughts on the knapsack problem are a little abstract.
Lecture 9Question changes in backpacks
Try to bypass the class.
--------------------------------------------------------------------------------
P01: 01Backpack Problems
Question
There are n items and a backpack with a capacity of v. 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.
Basic Ideas
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]}
This equation is very important. Basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a backpack with a capacity of V, if you only consider the I-th item Policy (put or not put), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "pre-i-1 items into the capacity of V backpack", the value is f [I-1] [v]; if I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity V-C [I ", the greatest value that can be obtained at this time is f [I-1] [V-C [I] plus the value W [I] obtained by placing the I item.
Optimize space complexity
The time and space complexity of the preceding method are O (N * V). The time complexity cannot be optimized, but the space complexity can be optimized to O (V ).
First, consider how to implement the basic idea mentioned above. There must be a main loop I = 1 .. n, two-dimensional array f [I] [0 .. all values of V. Then, if only one array f [0 .. v]. can we ensure that f [I] [v] represents the state we defined after the end of the I-th loop? F [I] [v] is derived from two subproblems: F [I-1] [v] and f [I-1] [V-C [I, can we ensure that f [I] [v] is pushed (that is, when f [v] is pushed in the I Main Loop) can we get the values of F [I-1] [v] and f [I-1] [V-C [I? In fact, this requires that in each main loop we use v = V .. 0 in order. f [v], this ensures that f [v] f [V-C [I] saves the value of State f [I-1] [V-C [I. The pseudocode is as follows:
For I = 1. n
For v = V .. 0
F [v] = max {f [v], F [V-C [I] + W [I]};
F [v] = max {f [v], f [V-C [I]} is equivalent to our transfer equation f [I] [v] = max {f [I-1] [v], f [I-1] [V-C [I]}, because the current F [V-C [I] is equivalent to the original f [I-1] [V-C [I]. If we change the circular order of V from the forward order to the order, then f [I] [v] is deduced by F [I] [V-C [I, it is not consistent with the meaning of this question, but it is another important backpack problem p02 is the most simple solution, so it is necessary to learn to solve the problem of 01 backpack with only one-dimensional array.
In fact, the program that uses a one-dimensional array to solve the 01 backpack will be used multiple times later, so here we abstract a process to process an item in the 01 backpack, direct calls in future Code are not described.
Zeroonepack indicates processing an item in a 01 backpack. The two parameters cost and weight indicate the cost and value of this item respectively.
Procedure zeroonepack (cost, weight)
For v = V .. cost
F [v] = max {f [v], F [V-cost] + weight}
Note that the processing in this process is different from the pseudo code given above. The preceding example program v = V .. 0 is used to show that every state in the program is solved according to the equation, avoiding unnecessary complexity of thinking. Now that the process has been abstracted as a black box, you can add optimization. Items whose cost is cost will not affect the status f [0 .. cost-1], which is obvious.
With this process, you can write the pseudocode for the 01 backpack problem as follows:
For I = 1. n
Zeroonepack (C [I], W [I]);
Initialization details
We can see that there are actually two different ways to solve the problem of a backpack. Some questions require the optimal solution when "just full of backpacks", and some questions do not require that the backpack be full. One difference is that the implementation methods of these two questions are different during initialization.
If the first method is used, it is required to be filled with a backpack. during initialization, F [0] is 0, and f [1 .. v] is set to-∞, which ensures that the final f [N] is an optimal solution just filled with a backpack.
If you do not need to fill your backpack, but only want the price to be as high as possible, you should set all f [0.. V] to 0 during initialization.
Why? It can be understood as follows: The initialized F array is actually the legal status when no item can be put into a backpack. If a backpack is required to be filled, only a backpack with a capacity of 0 may be filled with nothing with a value of 0. There is no legal solution for a backpack with other capacities, for undefined states, their values should all be-∞. If the backpack does not have to be filled, then any capacity of the backpack has a legal solution: "Nothing is loaded." the value of this solution is 0, therefore, the initial status values are all 0.
This tips can be fully applied to other types of backpack problems, so we will not explain the initialization before the status transfer.
Summary
01 the backpack problem is the most basic problem. It contains the most basic idea of the design state and equation in the backpack problem. In addition, other types of knapsack problems can also be converted to 01. Therefore, you must carefully understand the methods of the above basic ideas, the significance of the state transition equation, and how to optimize the space complexity.
--------------------------------------------------------------------------------
P02:Complete backpack Problems
Question
There are n items and a backpack with a capacity of V, each of which has unlimited 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.
Basic Ideas
This problem is very similar to the 01 backpack problem. The difference is that each item has unlimited items. That is to say, from the perspective of each item, there are no or no two policies related to it, but 0, 1, and 2 ...... And so on. If you still follow the troubleshooting idea of 01, make f [I] [v] to indicate that the first I items are placed in the maximum weight of a backpack with a capacity of v. You can still write the state transition equation based on different policies for each item, as shown in the following code:
F [I] [v] = max {f [I-1] [V-K * C [I] + K * W [I] | 0 <= K * C [I] <= V}
This is the same as the 01 backpack problem. There are O (N * V) states to be solved, but the time for solving each State is no longer a constant, the time for solving the state f [I] [v] is O (V/C [I]), and the total complexity exceeds O (VN.
I improved the basic idea of the backpack problem and obtained such a clear method. This shows that the equation of the 01 knapsack problem is indeed very important, and can be considered as another type of knapsack problem. But we are still trying to improve this complexity.
A simple and effective optimization
There is a very simple and effective optimization for the full backpack problem. If two items I and j meet the requirements of C [I] <= C [J] and w [I] & gt; = W [J], remove item j. The correctness of this optimization is obvious: under any circumstances, you can replace the small-cost high-value J with inexpensive I, and get at least no worse solution. For randomly generated data, this method often greatly reduces the number of items, thus accelerating the speed. However, this does not improve the complexity of the worst case, because it is possible that specially designed data cannot be removed from an item.
This optimization can be implemented in a simple O (N ^ 2) manner and is generally acceptable. In addition, one of the better solutions for the knapsack problem is: first, remove the items with a higher cost than V, and then use a similar counting sorting method, calculate which of the items with the same cost has the highest value. The optimization can be completed by O (V + n. This unimportant process does not provide pseudocode. I hope you can think about writing pseudocode or programs independently.
Convert to 01 solve the Knapsack Problem
Since the question 01 is the most basic question about a backpack, we can consider converting the question of a full backpack into a question 01 about a backpack. The simplest idea is to select at most V/C [I] items for the I-th item, therefore, you can convert the I-th item into an item with the same cost and value as the V/C [I] item, and then solve this 01 backpack problem. In this way, there is no time complexity to improve the basic idea, but this gives us the idea of converting a full backpack problem into a 01 backpack problem: Splitting an item into multiple items.
A more efficient conversion method is to split the I-th item into several items with a cost of C [I] * 2 ^ K and a value of W [I] * 2 ^ K, k meets C [I] * 2 ^ k <= v. This is a binary idea, because no matter how many items of the I-th item are selected in the optimal strategy, they can always be expressed as the sum of several 2 ^ K items. In this way, splitting each item into an O (log (V/C [I]) item is a great improvement.
O (VN) Algorithm
This algorithm uses a one-dimensional array. first look at the pseudo code:
For I = 1. n
For V = 0 .. v
F [v] = max {f [v], F [V-cost] + weight}
You will find that this pseudo code is different from the pseudo code p01 in the loop order of v. Why is this change feasible? First, let's think about why the V = V .. 0 in p01 should be reversed. This is because we need to ensure that the State f [I] [v] in the I-th loop is recursive from the state f [I-1] [V-C [I. In other words, this is to ensure that each item is selected only once, and to ensure that the policy of "selecting the I-item" is considered, it is based on a sub-result f [I-1] [V-C [I] That has never been selected for item I. Now, the unique feature of a backpack is that each type of item can be an unlimited number of items. Therefore, when considering the policy of "adding a first item, however, you need a sub-result f [I] [V-C [I] that may have been selected for Type I. Therefore, you can use V = 0 .. v. This is why this simple program is established.
This algorithm can also be derived from other ideas. For example, the state transition equation in the basic idea can be equivalent to this form:
F [I] [v] = max {f [I-1] [v], F [I] [V-C [I] + W [I]}
This equation is implemented using a one-dimensional array and the above pseudo code is obtained.
At last, the process of abstracting the source to manage a completely backpack-type item is pseudocode, which will be used in the future:
Procedure completepack (cost, weight)
For v = Cost .. v
F [v] = max {f [v], F [V-C [I] + W [I]}
Summary
The complete knapsack problem is also a very basic knapsack problem. It has two state transfer equations, which are given in the section "basic idea" and "O (VN) algorithm" respectively. I hope that you can carefully understand these two state transition equations, not only remember, but also understand how they come out. It is best to think of a method to obtain these equations on your own. In fact, it is a good way to deepen understanding of dynamic planning and improve the skill of dynamic planning to think about the significance of its equation and how to obtain it.
--------------------------------------------------------------------------------
P03:Multiple knapsack problems
Question
There are n items and a backpack with 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.
Basic Algorithms
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]}
The complexity is O (V * Σ N [I]).
Converting to 01 backpack Problems
Another basic way to write well is to convert it to 01 backpack solution: Replace the I-th item with the item in the N [I] Piece 01 backpack, then, the 01 backpack problem with the number of items is obtained. The problem is solved directly, and the complexity is still O (V * Σ N [I]).
But we expect that it will reduce complexity as if it were a full backpack after converting it into a 01 backpack problem. We still consider the binary idea. We want to replace the I-th item with several items, so that in the original question, the I-th item can be used in every strategy -- 0 .. N [I] parts-can be equivalent to several replacement items. In addition, a policy that exceeds N [I] parts cannot appear.
The method is to divide the I-th item into several items, with each item having a coefficient. The cost and value of this item are the original cost and value multiplied by this coefficient. Make these coefficients respectively 1, 2, 4 ,..., 2 ^ (k-1), N [I]-2 ^ k + 1, and K is the maximum integer that satisfies N [I]-2 ^ k + 1> 0. For example, if n [I] is 13, the item is divided into four items with a coefficient of 1, 2, 4, and 6.
The coefficient of the divided items is n [I], indicating that the I-th item cannot be obtained more than N [I] items. In addition, this method can ensure that 0 .. each integer between N [I] can be expressed by the sum of several coefficients. This proof can be divided into 0 .. 2 ^ K-1 and 2 ^ K .. N [I] is not difficult to discuss separately. I hope you can think about it yourself.
In this way, the I-th item is divided into the O (log n [I]) item, and the original problem is converted to the complexity of O (V * Σ log n [I]). the 0th backpack problem is a great improvement.
The following describes how o (log amount) processes items in multiple backpacks. amount indicates the number of items:
Procedure multiplepack (cost, weight, amount)
If cost * Amount> = V
Completepack (cost, weight)
Return
Integer k = 1
While k <num
Zeroonepack (K * cost, K * weight)
Amount = amount-K
K = K * 2
Zeroonepack (Amount * cost, amount * weight)
I hope you will understand this pseudocode carefully. If you do not understand it, you may translate it into program code, and then execute it several times in a single step, or you may understand it slowly by adding a pen or paper in your mind.
O (VN) Algorithm
Multiple knapsack problems also involve O (VN) algorithms. This algorithm is based on the state transition equation of the basic algorithm, but the monotonic queue method is used to make the values of each State be solved at an average O (1) time. Since the DP optimized with monotonous queue is beyond the scope of noip, this article will not explain it further. I first learned that this method was on Lou Tiancheng's "Man's eight questions" slide.
Summary
Here we see the process of improving the complexity of an algorithm from O (V * Σ N [I]) to O (V * Σ log n [I, we also know that there is an O (VN) algorithm that applies knowledge beyond the noip range. I hope you will pay special attention to the idea and method of "splitting items", prove its correctness, and write the complete program code.
--------------------------------------------------------------------------------
P04:Mixed three backpack Problems
Problem
If you mix p01, p02, and P03. That is to say, some items can be taken only once (01 backpack), some items can be taken unlimited times (full backpack), and some items can be taken up to a maximum of times (multiple backpacks ). How should we solve it?
01 mixing a backpack with a full backpack
Because the pseudocode given in p01 and p02 is only one different, if there are only two types of items: one type of item can only be retrieved once, and the other type of items can be retrieved infinitely, when you apply the transfer equation to each item, you only need to select an order or backward Loop Based on the item category. The complexity is O (VN ). The pseudocode is as follows:
For I = 1. n
If the I-th item is a 01 backpack
For v = V .. 0
F [v] = max {f [v], F [V-C [I] + W [I]};
Else if the I-th item is a full backpack
For V = 0 .. v
F [v] = max {f [v], F [V-C [I] + W [I]};
Plus multiple backpacks
If you can add up to a limited number of items, you can also give an O (VN) solution in principle: You can solve multiple types of items in a monotonous queue. However, if you do not consider the algorithm that exceeds the noip range, you can use P03 to divide each of these items into items in the O (log n [I]) 001 backpack.
Of course, a clearer way of writing is to call the three relevant processes we mentioned earlier.
For I = 1. n
If the I-th item is a 01 backpack
Zeroonepack (C [I], W [I])
Else if the I-th item is a full backpack
Completepack (C [I], W [I])
Else if the I-th item is multiple backpacks
Multiplepack (C [I], W [I], n [I])
When we first wrote these three processes, we probably did not think they would be mixed here. I think this shows the power of abstraction in programming. If you have been writing each type of backpack problem in this "abstract process" way, you are also very aware of the differences in their implementations, so when we encounter the problem of mixing three backpacks, we will be able to quickly think of the simple solution above, right?
Summary
Some people say that difficult questions are all superimposed by simple questions. Whether or not this sentence is still true or not, but it has been fully reflected in this lecture. Originally 01 backpacks, full backpacks, and multiple backpacks are not difficult, but after they are simply Combined, they will have such a question that will surely scare many people. However, as long as you have a solid foundation and understand the ideas of the three basic knapsack problems, you can split the difficult questions into simple ones to solve them.
--------------------------------------------------------------------------------
P05:Two-dimensional knapsack problems
Problem
The two-dimensional bag problem refers to: for each item, there are two different charges; the two costs must be paid at the same time when this item is selected; there is a maximum charge (Backpack capacity) for each price ). Ask how to select an item to maximize the value. Set these two costs to price 1 and price 2, respectively. The two costs required for item I are a [I] and B [I]. The maximum value (two types of backpack capacity) can be paid at two costs: V and U. The value of an item is W [I].
Algorithm
The fee is added to one dimension. You only need to add one dimension to the status. If f [I] [V] [u] is set, it indicates the maximum value that can be obtained when the cost of the first I item is V or U. The state transition equation is:
F [I] [V] [u] = max {f [I-1] [V] [u], f [I-1] [V-A [I] [U-B [I] + W [I]}
As mentioned above, only two-dimensional arrays can be used: when each item can only be retrieved once, the V and U variables adopt a backward loop, when an item is like a full backpack, use a sequential loop. Split an item when there are multiple backpack problems. I believe that with the Foundation above, you can implement the Program for this problem on your own.
Limit on the total number of items
Sometimes, the "two-dimensional fee" condition is given in an implicit way: a maximum of m items can be obtained. In fact, this is equivalent to an additional "number of items" for each item. The cost of each item is 1, and the maximum number of items that can be paid is M. In other words, if f [v] [m] is set, it indicates the maximum value that can be obtained when the cost is paid V and the maximum number of M items is selected. Then, according to the item type (01, complete, multiple) update cyclically using different methods, and then in F [0 .. v] [0 .. m.
Summary
When a problem is found to be deformed by a familiar dynamic planning question, adding a latitude to the original state to meet new restrictions is a common method. I hope you will first understand this method.
--------------------------------------------------------------------------------
P06:Group knapsack problems
Problem
There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. These items are divided into several groups. items in each group conflict with each other and you can select at most one item. 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.
Algorithm
This problem becomes that there are several strategies for each group of items: whether to select one item in the group or not. That is to say, if f [k] [v] is set, it indicates the maximum weight value that can be obtained for the first K items in the Group. The options are:
F [k] [v] = max {f [k-1] [v], f [k-1] [V-C [I] + W [I] | item I belongs to group k}
The pseudocode for using a one-dimensional array is as follows:
For all group K
For v = V .. 0
For all I belong to group K
F [v] = max {f [v], F [V-C [I] + W [I]}
Pay attention to the order of the three-tier loop here, and I have written an error in the beta version of this article. The loop "for V = V .. 0" must be outside of "for all I belong to group K. In this way, only one item in each group can be added to the backpack.
In addition, it is obvious that "a simple and effective optimization" can be applied to items in each group in p02 ".
Summary
The grouping of backpacks refers to several items mutually exclusive to each other as a group, which establishes a good model. The deformation of many knapsack problems can be converted into a group's backpack problem (for example, p07). The concept of "Generalized items" can be further defined by the group's backpack problem, which is very helpful for solving the problem.
--------------------------------------------------------------------------------
P07:Dependent backpack Problems
Simplified question
There is a "dependency" between items with this type of backpack problem. That is to say, I depends on J, indicating that if I is selected, item j must be selected. For the sake of simplification, we first set that no item is dependent on other items and other items. In addition, no item depends on multiple items at the same time.
Algorithm
This problem was extended by noip2006 kingming's Budget Scheme. According to the reference of this question, an item that does not depend on other items is called the "main item", and an item that relies on a main item is called the "attachment ". The simplified condition of this problem shows that all items are composed of several main parts and an attachment set dependent on each main component.
According to the general idea of the knapsack problem, only one main component and its attachment set are considered. However, there are a lot of available policies, including: one is not selected, only the master is selected, select the master and then select an attachment, select the master and then select two attachments ...... The state transition equation cannot be used to represent so many policies. (In fact, if there are n attachments, there are 2 ^ n + 1 in the policy, which is exponential .)
Considering that all these policies are mutually exclusive (that is, you can only select one policy), a master and its attachment set actually correspond to an item group in p06, the cost and value of an item in this item group correspond to the sum of the values of the items in this policy. However, this step alone does not provide a good algorithm, because there are as many items in the item group as the policy of the original problem.
Consider one sentence in p06: "a simple and effective optimization" can be applied to items in each group in p02 ". This reminds us that for items in an item group, only one item with the same cost has the greatest value and does not affect the result. Therefore, we can perform a backpack first on the "attachment set" of the main component I, and the cost is 0 in turn .. v-C [I] the maximum value of all these values F' [0 .. v-C [I]. The collection of the main component and its accessories is equivalent to the item group of V-C [I] + 1 item, the value of C [I] + k is F' [k] + W [I]. That is to say, many of the original exponential policies are redundant. After a 01 backpack is used, the main component I is converted into an item group of V-C [I] + 1, you can directly apply the p06 algorithm to solve the problem.
General problems
The more general problem is that the dependency is given in the form of "Forest" in graph theory (Forest is the set of multi-tree trees). That is to say, the attachment of the main component can still have its own attachment set, the restriction is that each item depends on only one item (only one main item) and does not have circular dependency.
To solve this problem, you can still convert each master and its attachment set into an item group. The only difference is that each attachment may not be considered as an item in a general 01 backpack because there may be attachments. If this attachment also has an attachment set, it must be first converted into an item group, then, we use the grouping backpack to solve the problem and find out the value of the accessories for each expense in the attachment group corresponding to the main component and its attachment set.
In fact, this is a tree-like DP, which features that each parent node needs to perform a DP for each of its son's attributes to obtain its own relevant attributes. This has touched on the idea of "Generalized items. After reading p08, you will find that every subtree of this "dependency tree" is equivalent to a generalized item, finding the generalized item corresponding to the subtree with a node as the root is equivalent to finding the sum of the generalized items corresponding to all of its sons.
Summary
I did a very bad job with that backpack problem in noip2006. I wrote hundreds of lines of code, but I did not get a point. Later, I thought and found that by introducing the concepts of "item group" and "dependency", I could deepen my understanding of this question and solve its promotion problems. Use the idea of item groups to consider the extremely special dependency in the question: the item cannot be both a master and an attachment, and each master has a maximum of two attachments, it can be found that a main component and its two attachments are equivalent to an item group composed of four items, which reveals the essence of the problem.
--------------------------------------------------------------------------------
P08:Generalized item
Definition
Considering such an item, it does not have a fixed cost and value, but its value changes with the cost you allocate to it. This is the concept of generalized items.
More strictly defined. In a backpack with a capacity of V, the generalized item is a fixed domain of 0 .. the integer function H in V. When the cost assigned to it is V, the value obtained is H (V ).
This definition is a little abstract. Another kind of understanding is that a generalized item is an array H [0. V], which charges V to obtain the value H [v].
An item with a C value of W. If it is an item in the 01 backpack, think of it as a generalized item, except for H (c) = W a function whose other function values are 0. If it is an item in a full backpack, it can be considered as a function, only when V is divisible by C, H (v) = V/C * W, and other function values are 0. If it is an item with a maximum of N duplicates in multiple backpacks, H (v) is a generalized item function) = V/C * W is only used when V is divisible by C and V/C <= n. In other cases, the function value is 0.
An item group can be considered as a generalized item H. For a 0 .. V in V. If there is no item in the item group whose cost is V, H (v) = 0; otherwise, H (v) is the maximum value of all items whose cost is v. In p07, each primary component and its attachment set are equivalent to an item group, and can naturally be considered as a generalized item.
Sum of generalized items
If two generalized items h and l are used, how can we obtain the greatest value from these two generalized items with the given cost? In fact, for a given charge V, you only need to enumerate how to allocate the charge to two generalized items. Similarly, for each integer v of 0. V, the maximum value of F (v) allocated to H and l can be obtained ). That is, F (v) = max {H (k) + L (V-k) | 0 <= k <= V }. As you can see, F is also a function with the defined domain 0 .. v determined by generalized item H and L. That is to say, F is a generalized item determined by generalized item H and L.
The sum of generalized items and H and l can be defined as generalized items. If generalized item F meets F (v) = max {H (k) + L (V-k) | 0 <= k <= V}, that is, F is the sum of H and l, that is, F = H + L. The time complexity of this operation depends on the size of the backpack, Which is O (V ^ 2 ).
The definition of generalized items indicates that if two generalized items are replaced by their sum in a backpack question, the answer to the question is not affected. As a matter of fact, all the items in the list are general items. The process of finding the answer is the process of finding the sum of all these general items. If this is set to S, the answer is the maximum value in S [0. V.
General items of backpack Problems
Many conditions may be provided for a backpack, including the charges, values, and other attributes of each item, as well as the group and dependency among items. However, the problem must correspond to a generalized item. That is to say, given all the conditions, we can calculate for each non-negative integer v: If the backpack capacity is V, what is the maximum value to attach an item to the backpack, this can be considered as a generalized item defined in a non-negative integer set. This generalized item-or a function with a non-negative integer in its definition field-contains highly concentrated information about the problem itself. Generally, after obtaining the value of a subdomain (for example, 0. V) of this generalized item, you can obtain the final answer to the question of the backpack based on the value of this function.
To sum up, in general, solving a knapsack problem is a function corresponding to solving the problem, that is, the generalized item of the problem. One way to solve a generalized item is to represent it as the sum of several generalized items and then seek for it.
Summary
This is my own original idea. Specifically, when I learned the scheme language of functional programming, I used functional programming to examine the theory of various backpack problems. This is really abstract. Maybe the degree of model abstraction has exceeded the requirements of noip, so it doesn't matter if you cannot understand it for the moment. I believe that as your oi journey expands, you will understand it one day.
--------------------------------------------------------------------------------
P09:Question changes in backpacks
All the above problems involve the greatest value that can be obtained under the limitation of the capacity (cost) of the backpack. However, there are still many flexible questions about the backpack. It is worth mentioning here. However, I believe that as long as I thoroughly understand how to find the greatest value of the knapsack problem, even if the question method changes, it is not difficult to come up with an algorithm.
For example, you can find out how many items can be placed at most or how much space can be filled with backpacks. You can obtain the values of all States (array f) based on the preceding equation.
Also, if you require "minimum total value" and "Minimum Total number of items", simply change the max value in the above state transition equation to Min.
The following describes some questions with greater changes.
Output Scheme
Generally, a knapsack problem requires an optimal value. If you want to output the optimal value scheme, you can refer to the general method of dynamically planning the problem output scheme: the optimal value of each State is recorded by which of the state transition equation is introduced. In other words, it is recorded by which policy it is introduced. You can find the previous status based on this policy, and push it forward from the previous status.
Take 01 backpack as an example, the equation is f [I] [v] = max {f [I-1] [v], f [I-1] [V-C [I] + W [I]}. Use an array G [I] [v], if G [I] [v] = 0 indicates that the value of F [I] [v] is introduced, the first item of the equation is used (that is, F [I] [v] = f [I-1] [v]), G [I] [v] indicates that the last item of the equation is used. Note that the two items indicate two strategies: the I-th item is not selected and the I-th item is selected. The pseudo code of the output scheme can be written as follows (set the final state to f [N] [v]):
I = N
V = V
While (I> 0)
If (G [I] [v] = 0)
Print "item I not selected"
Else if (G [I] [v] = 1)
Print "select item I"
V = V-C [I]
In addition, the first or last item of the equation can also be obtained in real time based on the value of F [I] [v] in the Process of the output scheme, that is, the G array is not required to be recorded, change G [I] [v] = 0 in the above Code to f [I] [v] = f [I-1] [v], G [I] [v] = 1 to f [I] [v] = f [I-1] [V-C [I] + W [I.
Optimal Solution with minimum output Lexicographic Order
Here, the "minimum Lexicographic Order" means that the selection scheme of the No. 1. N item is sorted out and the Lexicographic Order is the smallest. The following uses the solution of outputting the smallest Lexicographic Order of the 01 backpack as an example.
In general, to find the optimal solution with the smallest Lexicographic Order, you only need to pay attention to the policy during the transfer. First, the definition of the subproblem should be slightly changed. We have noticed that if there is an optimal solution for Selecting item 1, the answer must include item 1. The original problem is converted into a backpack with a capacity of V-C [1]. the item is 2 .. n. Otherwise, if the answer does not include item 1, it is converted into a sub-question with the backpack capacity still V and item 2 .. n. Regardless of the answer, the sub-question items are defined in the form of I. N instead of 1. I, so the state definition and the transfer equation must be changed. However, it may be easier to sort items in reverse order first, and the following items are described in reverse order.
In this case, you can calculate the value according to the previous classical state transition equation, but note the following when outputting the scheme: From n to 1, if f [I] [v] = f [I-v] and f [I] [v] = f [I-1] [f-C [I] + W [I] was established at the same time, the latter (that is, the item I is selected) should be used to output the scheme.
Total number of solutions
In addition to the maximum value that can be obtained after the value of each item is given, the problem of a backpack with a given capacity, item cost, and item relationship (grouping, dependency, etc, you can also get the total number of solutions that are filled with backpacks or packed into a specific capacity.
For this type of question changing, you only need to change Max in the state transition equation to sum. For example, if each item is an item in a full backpack, the transfer equation is
F [I] [v] = sum {f [I-1] [v], F [I] [V-C [I]}
Initial Condition f [0] [0] = 1.
In fact, the reason for this is that the state transition equation has examined all possible backpack composition solutions.
Total number of Optimal Solutions
The optimal solution here refers to the solution with the largest total item value. Take the 01 backpack as an example.
Combined with the idea of finding the maximum total value and the total number of solutions, the total number of optimal solutions can be calculated as follows: F [I] [v] means the same as above, G [I] [v] indicates the total number of optimal solutions for this subproblem, the pseudo code of G [I] [v] while obtaining f [I] [v] is as follows:
For I = 1. n
For V = 0 .. v
F [I] [v] = max {f [I-1] [v], F [I-1] [V-C [I] + W [I]}
G [I] [v] = 0
If (F [I] [v] = f [I-1] [v])
INC (G [I] [v], G [I-1] [v]
If (F [I] [v] = f [I-1] [V-C [I] + W [I])
INC (G [I] [v], G [I-1] [V-C [I])
If this is the first time you see such a problem, Please carefully understand the above pseudo code.
Obtain the sub-optimal solution and the K-Optimal Solution
For the problem of the sub-optimal solution and the K-optimal solution, if the corresponding optimal solution can write the state transition equation and solve it with dynamic planning, the sub-optimal solution can be solved with the same complexity, the k-th optimal solution is a factor k more than the complexity of the optimal solution.
The basic idea is to convert each State into an ordered queue and convert the max/min in the state transition equation into a merge of ordered queues. Here, we will continue to explain the 0th backpack as an example.
First, look at 01 backpack for the optimal solution state transition equation: F [I] [v] = max {f [I-1] [v], f [I-1] [V-C [I] + W [I]}. If the k-th optimization is required, the state f [I] [v] should be a K-sized array f [I] [V] [1. K]. F [I] [V] [k] indicates the value of the K-optimal solution when the size of the first item and backpack is v. "F [I] [v] is an array of K sizes", which may be easier to understand if you are familiar with C, alternatively, you can simply consider adding one dimension to the original equation. Obviously, F [I] [V] [1 .. k] is arranged in ascending order, so we regard it as an ordered queue.
Then the original equation can be interpreted: f [I] [v] This ordered queue is ordered by F [I-1] [v] and f [I-1] [V-C [I] + W [I] the queue is merged. Ordered queue f [I-1] [v] That f [I-1] [V] [1 .. k], F [I-1] [V-C [I] + W [I] is understood as in F [I-1] [V-C [I] [1 .. k. Merge the two ordered queues and store the results (the first K items) to f [I] [V] [1 .. k]. The complexity is O (k ). The final answer is f [N] [V] [K]. The total complexity is O (nvk ).
Why is this method correct? In fact, the process of solving a correct state transition equation traverses all available policies, which overwrites all solutions to the problem. However, the optimal solution is ignored because it is the optimal solution. If each State is represented as a K-sized array, and the first K Optimal Values of the State can be saved in an orderly manner in this array. Then, the max operation for any two States is equivalent to the merge of two ordered queues from large to small.
In addition, pay attention to the definition of "k-optimal solution". The two schemes with different policies but the same weights are considered as the same solution or different solutions. If it is the former, it is necessary to ensure that the number in the queue is not repeated during the maintenance of the ordered queue.
Summary
Obviously, it is impossible to exhaust all questions about Dynamic Planning of backpacks. There is even a kind of problem of Combining dynamic planning of backpacks with other fields (such as number theory and graph theory), which will not be discussed in this article. However, it is not difficult to come up with an algorithm as long as you have a profound understanding of the ideas and state transfer equations of all the aforementioned categories and encounter other deformation questions, as long as the question difficulty is still noip.