01- backpack full backpack, backpack and related multi-applications

This article describes the series knapsack problem, including:

[1] 01- backpack and its application
[2] completely backpack and its application
[3] Multiple backpack


[1] 01- backpack and its application:

1.1,01- knapsack problem description:

There are N items and the capacity C of a backpack. By weight of the i-th items that w [i], the value is v [i]. Solving the article into the backpack which allows the sum of the weight of these items does not exceed the capacity of the backpack, and the sum of the maximum value. Such as:

I items Weight w Value v
1 2 3
2 3 4
3 4 5
4 5 6

Where N = 4, C = 8, the maximum value of 10 (i.e., the items 2 and 4 into the backpack).

1.2,01- knapsack problem-solving ideas:

01- suitable for solving knapsack problem dynamic programming, with dp [i] [j] represent the i-th before items into the capacity of the largest value j of the backpack , so this issue becomes a problem filling. As the above examples, dp [4] [8] is the final answer.

The key is to find state transition equation. Suppose now calculate dp [i] [j], then the two cases:

  • If j is less than the current capacity of the weight of the items i (j <w [i]) , then the i-th items certainly not into the backpack, the backpack or just before this i-1 th items, i.e. dp[i][j] = dp[i-1][j].
  • If j is greater than the current capacity is equal to the weight of the items i (j ≥ w [i]) , then the i-th items of basic conditions into the backpack. So in the end can not be added to the first place depends on whether i items of the total value of the maximum. If not maximize the total value, it is still dp[i][j] = dp[i-1][j], i represents not into the first items; if you can maximize the overall value, then dp[i][j] = dp[i-1][j-w[i]] + v[i], where dp[i-1][j-w[i]]is the i-th items before the charged state.

As the above example, as follows from top to bottom sheet (the first row and first column is initialized boundary conditions):
4905573-13f7c4037db31e6d.png

1.3,01- backpack Python3 achieve:

class Solution:
    def knapsack01(self, N, w, v, C):
        '''
        @param N: int, 物品总数
        @param w: List, 物品重量
        @param v: List, 物品价值
        @param C: int, 背包容量
        '''
        dp = [[0] * (C+1) for _ in range(N+1)]
        for i in range(1, N + 1):
            for j in range(1, C+1):
                if j < w[i-1]:
                    dp[i][j] = dp[i-1][j]
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i-1]] + v[i-1])
        return dp[-1][-1]

N = 4
w = [2,3,4,5]
v = [3,4,5,6]
C = 8
print(Solution().knapsack01(N, w, v, C))  # 10

Further found that, if j <w [i-1], to update dp [i] [j] = dp [i-1] [j] is actually useless, because the update of the next line is certainly less than j <w [i -1] where when. Thus, the intermediate code can be simplified:

for i in range(1, N + 1):
    for j in range(w[i-1], C+1):
        dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i-1]] + v[i-1])

1.5,01- bag space optimization:

Still further found that every time dp [i] [j] is changed only to dp [i-1] [x] {x: 1 ... j} related, dp [i-1] [x] is the last cycle saved value;

Thus, DP may be compressed into one-dimensional arrays, to achieve the purpose of optimizing space, state transition equation is converted to:
dp[j] = max(dp[j], dp[j-w[i]] + v[i]).

Note: state transition equation, each derived V (i) (j) by V (i-1) (jw (i)) is derived, so that one-dimensional array to be scanned in descending order of j (Capacity to 0), one cycle before preserved whether those values will be modified, resulting in an error.

The intermediate code is simplified as follows:

for i in range(1, N+1):
    for j in range(C, w[i-1]-1, -1):
        dp[j] = max(dp[j], dp[j-w[i-1]] + v[i-1])

1.4 associated with 01- backpack Leetcode topics:

416: determining whether an array may be divided into two equal subsets and


[2] completely backpack and its application

2.1, complete knapsack problem description:

There are N kinds of goods and a backpack capacity is C, each item has infinite element is available. The weight of the i-th article of wi, the value is vi. Which solving the items into the bag, the total volume of these items can not exceed the capacity of the backpack, and the total value of the maximum. The maximum output value.

Input formats:

The first line of two integers, N, C, separated by spaces, each represent a kind of goods and a backpack volume.
Then there are N rows, each row two integers wi, vi, separated by spaces, respectively, and the weight of the i-th value of the article.

Output formats:

Output An integer representing the maximum value.

data range:

  • 0 < N, C ≤ 1000
  • 0 <wi, we ≤ 1000
Sample input:
4 5
1 2
2 4
3 4
4 5
Sample output:
10

2.2, full backpack problem-solving ideas:

The difference is that with the 01- backpack full knapsack problem means that each item has an infinite number.

2.3, C ++ implementation:


Reproduced in: https: //www.jianshu.com/p/7b60dfc8d1bd

Guess you like

Origin blog.csdn.net/weixin_34072159/article/details/91203969