The clearest explanation of the 01 backpack problem

topic:

01 Knapsack problem description: There are N=5 items numbered a, b, c, d, e, their weights w are 2, 2, 6, 5, 4, and their values ​​v are 6, 3 ,5,4,6, the quantity of each item is only one, now give you a backpack with a load-bearing capacity of M=10, how to make the items loaded in the backpack have the maximum value sum_v?

 

In the DP (dynamic programming) problem, the 01 knapsack problem is relatively basic and simple, but many people's explanations on the Internet are either a long paragraph, long formula theory, or knowledge lists the state transition equations, and there is no Explain why the equation is written like this, let me try to talk about the simplest and most core concepts and ideas in the 01 knapsack problem :

1.  This 01 knapsack problem is essentially an exhaustive knapsack capacity and optional items (meaning that the items inside may or may not be put into the knapsack), and the optimal solution is obtained, but in the exhaustive In the process, according to the state transition equation, only the part of the possible optimal solution is calculated.

2.  Why can the state transition equation be listed? This is because the optimal solution of each state is obtained based on the optimal solution of the previous state . Specific to the backpack problem, there are the following points:

  a) When the item options are the same, the larger the backpack capacity M, the sum_v must be greater than or equal to the original value.

  b) When the backpack capacity M is determined, the more items N can be selected, the sum_v must be greater than or equal to the original value.

  c)  From a) and b), the maximum value of sum_v is the sum_v when M and N take the maximum value

  c) In terms of ideas, the 01 knapsack problem has two dimensions: the knapsack capacity M, and the number N of items to choose from. The essence of programming is to realize the idea of ​​human beings to solve real-world problems. Think about it, how would you solve this problem without a computer? The answer is, for example, when considering M=1, first consider whether a can be put into the backpack to get the maximum value, and then consider whether a and b can be put into the backpack (a and b are both options, and the one that can be put into the backpack in the end may be a , may be b, or ab), at this time, compared with the previous case where only a was considered, there is one more b, so:

    •   First, it is necessary to judge whether b can be put into the backpack alone. If not, then the maximum value that can be achieved by considering the cases of a and b is to consider only the case of a.
    •   If it can, that is, b can be put in, there are two possibilities. For these two possibilities, the maximum value should be taken:
        •   eventually actually puts b in; it is unknown whether it puts a in
        •   In the end, b was not put in (because there may be more suitable items than b in the back), it is unknown whether a is put in

  Describe the above paragraph mathematically: sum_v[i][j] represents the maximum value that can be obtained when the first i item is listed as an alternative and the backpack capacity is j; w[i] represents the weight of the i-th item, v[i] represents the value of the i-th item

if 1 >=w[2]:
    sum_v [2] [1] = max (sum_v [1] [jw [2]] + v [2], sum_v [1 ] [1])
 else :
    sum_v [2] [1] = sum_v [1] [1]

  Generalizing to any case, we get our state transition equation :

if j >=w[i]:
    sum_v[i][j] = max(sum_v[i-1][j-w[i]] + v[i], sum_v[i-1][j])
else:
    sum_v [i] [j] = sum_v [i-1] [j]

  The maximum value of sum_v is the last element of sum_v[i][j]

 

3. Perform simple data structure transformation during calculation. Because when i=1, that is, at the beginning of the calculation, it is also necessary to consider the situation that if the first item cannot be put in, there is no item in the backpack at this time, so the weight and value are both 0. Therefore, it is necessary to express the weight of the item and the value of Prepend a data 0 to the list of values.

In addition, when there is no item in the backpack, the value is 0. So the initial value of sum_v[i][j] needs to be all set to 0.

Below is the detailed code:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import copy

class ZOPACK(object):
    def __init__(self,n,m,w,v):
        self.num = n
        self.capacity = m
        self.weight_list = [0,] + w
        self.value_list = [0,] + v
        self.Sum_Value_Metrix = self.__CreateMetrix__(self.num+1,self.capacity+1,0)
        
    def __CreateMetrix__(self,x,y,init_value):
        d2_list = []
        for i in range(x):
            d1_list = []
            for j in range(y):
                d1_list.append(init_value)
            d2_list.append(d1_list)
        return d2_list
        
    def dp(self):
        sum_v = self.Sum_Value_Metrix
        num = self.num
        capacity = self.capacity
        w = self.weight_list
        v = self.value_list
        for i in range(1,num+1):
            for j in range(1,capacity+1):
                if j >=w[i]:
                    #print("i,j:%s,%s" % (i,j))
                    sum_v[i][j] = max(sum_v[i-1][j-w[i]] + v[i], sum_v[i-1][j])
                else:
                    sum_v[i][j] = sum_v[i-1][j]
        print("The max value we can get is: ", sum_v[-1][-1])
        print(sum_v)

if __name__ == "__main__":
    num = 5
    capacity = 10
    weight_list = [2, 2, 6, 5, 4]
    value_list = [6, 3, 5, 4, 6]
    q = ZOPACK(num,capacity,weight_list,value_list)
    q.dp()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324930471&siteId=291194637