Dynamic programming solves the 0-1 knapsack problem (super detailed understanding)

Foreword:

It’s been a long time since I wrote about the 0-1 knapsack problem, and I don’t remember much. I wrote this article for my own reference in the future. It would be a great honor if I could help readers at the same time.

text

The 0-1 knapsack problem is such a problem, assuming there is a knapsack whose capacity is capacity. There is a pile of items on the ground, the number of which is n, and each item has two properties: weight w and value v.

The requirement is to find a combination of items such that their weight is less than or equal to the maximum capacity and their value is the largest.

The idea of ​​​​dynamic programming solves the 0-1 knapsack problem:

First create a two-dimensional array dp, where dp[i][j] represents the maximum value that can be obtained when the knapsack capacity is j when only the first i items are used. That is to say: select some items from the first i items, the total weight of these items is less than or equal to j, but the sum of their values ​​is the largest, and this maximum value sum is recorded as dp[i][j].

The row width of dp is n, indicating that there are n items in total, and the column width is capacity, indicating that the maximum capacity of the backpack is capacity.

Roughly like this: 

Suppose there are n items, and each item is numbered with 1-n, wi, vi represent the weight and value of the i-th item respectively.

Then the jth column of the first row indicates the maximum value that can be put into the backpack when only item 1 is used and the backpack capacity is j.

So, in the first line:

(1) If w1 > j, then the knapsack capacity j cannot accommodate the weight of the first item, and 0 should be filled in at this time

(2) If w1 <= j, then the backpack capacity can accommodate the weight of the first item, and v1 should be filled in at this time.

Therefore, the elements in the first row can only be filled with 0 or v1, and the first half is 0, and the second half is v1

Assuming n==10, the knapsack capacity is 3 (final capacity)

1,2,3 (the number of each item)

2,7,1 (the weight of each item)

1,2,3 (the value of each item)

Assuming w1==7, then the first line is as follows:

Let i>1, then for the j-th column of the i-th row, it should be filled like this:

(1) When wi > j, even if all the things that have been loaded in the backpack are emptied, it is not enough to hold the i-th item.

At this point dp[i][j] = dp[i-1][j]. That is, considering the first i items is the same result as the first i-1 items.

(2) When wi <= j, you can consider putting the i-th item in

        (2-1) If the i-th item is to be put in, then the i-th item will occupy the capacity of wi, how much value can the remaining capacity hold? There is no doubt that the value of dp[i-1][j-wi] should be able to be loaded at the maximum, because dp[m][n] means that only the first m items can be loaded when the capacity is n the maximum value of . That is, dp[i][j] = dp[i-1][j-wi] + vi.

        (2-2) If the i-th item is not put in, then the total value remains unchanged, that is: dp[i][j] = dp[i-1][j].

          (2-3)

          So do you want to put the i-th item in it? Some people may say, since it can be put in, why not put it in?

          Wouldn't it be more valuable if you put it in? In fact, not necessarily, because what is said here can be put in means, put it in the backpack

          After the things that have been put in are vacated, the i-th item can be put in. But after forcibly putting the i-th item in, it is possible

          As a result, some items that have been put in are squeezed out and there is no room to put them, which may lead to a decrease in the total value.

          So, when wi <= j, dp[i][j] = max{dp[i-1][j-wi] + vi , dp[i-1][j]}

So the final form is:

 Therefore, it can be seen from the table that when the backpack capacity is 3 and the number of items is 3,

Each item number is: 1,2,3

The weight of each item is: 2,7,1

When the value of each item is: 1, 2, 3,

The maximum value that can be put into the backpack is 4 (the number in the lower right corner of the form)

practise:

This picture is the topic on Likou, and it is also the 0-1 knapsack problem

 Written at the end: If there is any mistake, please correct me, communicate politely, thank you very much.

Guess you like

Origin blog.csdn.net/fly_view/article/details/132218691