About backpack 9

Source: Internet
Author: User

P01: 0-1 backpack 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 make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.

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 "before the I-1 items into the capacity of V backpack"; if I items, then the problem is converted to "pre-i-1 items into the remaining capacity V-C [I] in the backpack ", 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.

Note that f [I] [v] makes sense when and only when there is a subset of the first I items, the total cost is v. Therefore, the final answer is not necessarily the maximum value of F [N] [v], but of F [N] [0. V. If you remove the "exactly" word in the state definition, you need to add another f [I] [V-1] to the transfer equation, this ensures that f [N] [v] is the final answer. It's up to you to understand why.

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] saves the value of status f [I-1] [V-C [I] when pushing f [v. 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.

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 not 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, as shown in the following code: if two items I and j meet the requirements of C [I] <= C [J] and w [I]> = 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.

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. But we have a better O (VN) algorithm. * O (VN) algorithm this algorithm uses a one-dimensional array, first look at the pseudo code: <PRE class "example"> for I = 1 .. N for V = 0 .. V f [v] = max {f [v], F [V-C [I] + W [I]};

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]}. After this equation is implemented using a one-dimensional array, the above pseudo code is obtained.

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, then: 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 * Sigma log n [I]). the 0th backpack problem is a great improvement.

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 * Sigma N [I]) to O (V * Sigma 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 ideas and methods of "splitting items", prove its correctness, and use a program as concise as possible.

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
Considering that the last 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.

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 variable V and U adopt the sequential loop, when an item is like a full backpack, a reverse loop is used. Split an item when there are multiple backpack problems.

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.

In addition, if you want to "retrieve m items", find the answer in the range of F [0. V] [M.

Summary
As a matter of fact, it is a common method to add a latitude in the original state to satisfy new restrictions when you find a question that is transformed from a familiar dynamic planning question. I hope you will first understand this method.

P06: group backpack 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 that can be obtained for the first K items in the group, 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 all I belong to group K
For v = V .. 0
F [v] = max {f [v], F [V-C [I] + W [I]}

In addition, "simple and effective optimization" in p02 can be applied to items in each group ".

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.

More 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
The introduction of the concepts of "item group" and "dependency" can deepen the 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 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: questions about 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 the 01 backpack, the transfer equation is f [I] [v] = sum {f [I-1] [v], f [I-1] [V-C [I] + W [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.

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.