0-1 knapsack problem

Source: Internet
Author: User

0-1 knapsack problem:

There are n items and a backpack with a capacity of V. The cost of article I 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.

The problem is characterized by the following: Each item has only one piece, can choose to put or not to put.

Basic idea of the algorithm:

Using the dynamic programming idea, the sub-problem is: F[i][v] indicates the maximum value that the first I item can get in a backpack with a capacity of V.

Its 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 knapsack-related problems of the equation is derived from it.

F[I-1][V]: Do not put the article I item

F[i-1][v-c[i]]+w[i]: Make room for article I items to put in

Explain the above equation: "Put the first I item into a backpack with a capacity of V" This sub-problem, if only consider the article I items put or not put, then can be converted to only the first I-1 items, that is, 1, if I do not put items, then the problem translates to "the first I-1 items into a backpack with a capacity of V" 2, if the article I item, then the problem translates into "The first I-1 items into the remaining capacity of v-c[i] backpack" (the maximum value can be obtained at this time is F [i-1][v-c[i]] plus the value obtained by placing the article I items w[i]). The value of F[i][v] is the largest value in 1, 2.

(Note: F[i][v] is meaningful when and only if there is a subset of the first I item, its cost sum is v. So after the recursive completion of this equation, the final answer is not necessarily f[n] [V], but the maximum value of f[n][0..v]. )

Optimization of spatial Complexity:

The time and space complexity of the above methods are O (N*v), in which the time complexity can no longer be optimized, but the spatial complexity is optimized to O (V).

The above f[i][v] is stored using a two-dimensional array, can be optimized to one-dimensional array f[v], change the main loop to:

For I=1..N

For V=v. 0

F[v]=max{f[v],f[v-c[i]]+w[i]};

The second loop is going to be changed from V. 0, reverse.

Explain:

Assume maximum capacity m=10, number of items n=3, item size w{3,4,5}, item value p{4,5,6}.

When the first cycle is carried out, F[V] is the result of the last cycle, that is, the result of the i-1 cycle (i>=1). So F[v]=max{f[v],f[v-c[i]]+w[i]} in this equation, the right side of the equal sign (F[v] and f[v-c[i]]+w[i) are the values generated by the previous loop.

When I=1, the f[0..10] initial value is 0. So

f[10]=max{f[10],f[10-c[1]]+w[1]}=max{0,f[7]+4}=max{0,0+4}=4;

f[9]=max{f[9],f[9-c[1]]+w[1]}=max{0,f[6]+4}=max{0,0+4}=4;

......

f[3]=max{f[3],f[3-c[1]]+w[1]}=max{0,f[3]+4}=max{0,0+4}=4;

f[2]=max{f[2],f[2-c[1]]+w[1]}=max{0,f[2-3]+4}=0;//array out of bounds?

f[1]=0;

f[0]=0;

When i=2, this time f[0..10] has been re-assigned after the last cycle, that is, f[0..2]=0,f[3..10]=4. Use F[v]=max{f[v],f[v-c[i]]+w[i]} This formula calculates the value of f[0..10] at i=2.

When I=3 is the same.

The specific values are shown in the following table:

Therefore, the inverse loop can be used to guarantee that the values of f[i-1][v] and F[i-1][v-c[i]] are saved by the f[v] and F[v-c[i]]+w[i [] to the right of the equals sign in the formula F[v]=max{f[v],f[v-c[i]]+w[i]} When F[v] is calculated.

When I=n, the resulting f[v] is the optimal value required.

F[j]=max{f[j],f[j-c[i]]+w[i]};

When not assigned to F[J], F[j] stores a variety of j-1 in 0~v

So F[j] indicates the total value of the time when J is not put

F[i-c[i]]+w[i] Represents the total value of freeing up a place to put J (there may be no place, or 0)

Therefore Max{f[j],f[j-c[i]]+w[i]}; The maximum value that is calculated is the J-and-A-max.

Details of the initialization problem:

In the knapsack problem of finding the optimal solution, there are two kinds of different questions: 1, the optimal solution of "just fill the backpack", 2, the optimal solution of less than equal to the knapsack capacity, that is not necessarily full pack.

These two types of questioning are different at the time of initialization.

1. The optimal solution for the "pack Full Pack" is required:

At initialization, except for F[0] 0 other F[1..V] are set to-∞, so that the resulting f[n is guaranteed to be a very full pack of the best solution. If the knapsack capacity cannot be satisfied, that is, f[v] is not the optimal value, then f[v]=-∞, so that it can be said to not find the best value of the knapsack capacity.

2, the optimal solution is less than equal to the capacity of the backpack, that is not necessarily full pack:

If it is not required to fill the backpack, but only want the value as large as possible, the initialization should be F[0..V] all set to 0.

Summarize

01 knapsack problem is the most basic knapsack problem, it contains the knapsack problem design state, the basic idea of the equation, in addition, other types of knapsack problem can also be converted to 01 knapsack problem solving. Therefore, we must carefully understand the above basic idea of the method, the significance of the state transfer equation, and finally how to optimize the space complexity.

0-1 knapsack problem

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.