P01: 01 backpack Problems

Source: Internet
Author: User
P01: 01 question about backpack

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 "before the I-1 items into the capacity of V in the 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 above method are O (VN). The time complexity should no longer be optimized, but the space complexity can be optimized to O.

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 to push f [v] in order to ensure that f [V-C [I] is saved as a status when f [v] is pushed.
F [I-1] [V-C [I] value. 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.

Constant Optimization

In the preceding pseudo code, for V = V .. 1 can be used to improve the lower limit of the loop.

Because we only need the final value of F [v], we only need to know f [V-W [N. Similarly, for the J-ID backpack, you only need to know f [V-sum {W [J .. n]}], that is, in the code

for i=1..N
    for v=V..0

Can be changed

for i=1..n
    bound=max{V-sum{w[i..n]},c[i]}
    for v=V..bound

This is useful when V is large.

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.

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.