Python implements dynamic programming 01 knapsack problem

Code: https://blog.csdn.net/qq_34178562/article/details/79959380

Theory explanation:

https://www.bilibili.com/video/BV1K4411X766?from=search&seid=13724711054890720316

https://www.bilibili.com/video/BV1XD4y1S7nv?from=search&seid=13724711054890720316

 

First assume a data input: six things, thing A weighs 2kg worth 2 cents, thing B weighs 2kg worth 3 cents, thing C weighs 3kg worth 1 cent, thing D weighs 1kg worth 5 cents, thing E weighs 5kg It's worth 40 cents, and stuff F weighs 2kg worth 30 cents. Asked about a few cents worth of stuff that a 10kg schoolbag can hold at most.

This problem requires the establishment of a dynamic programming table. If you can understand the creation process of this table, you can know how to write the code.

Value (gross) 0kg 1kg 2kg 3kg 4kg 5kg 6kg 7kg 8kg 9kg 10kg
Don't put things 0 0 0 0 0 0 0 0 0 0 0
A w=2 v=2 0 0 2 2 2 2 2 2 2 2 2
B w=2 v=3 0 0 3 3 5 5 5 5 5 5 5
C w=3 v=1 0 0 3 3 5 5 5 6 6 6 6
D w=1 v=5 0 5 5 8 8 10 10 10 11 11 11
E w = 5 v = 4 0 5 5 8 8 10 10 10 12 12 14
F w=2 v=3 0 5 5 8 8 11 11 13 13 13 15

First of all, if we don't put things, the backpack is definitely worthless, so the row of things left is 0. The backpack capacity is 0, you can't put things in, and the backpack has no value, so the 0kg column is 0.

Then, we look at the corresponding formula: V ( i , j ) means that the first i items are put into the backpack with the capacity j .

1) Let's look at the row of A, (A,1kg) is equal to 0, why is it equal to 0, 1kg is less than the weight of A, 2kg, conforms to j < wi , so (A,1kg) is equal to (A,0kg), which is equal to 0.

2) Let's see (A, 2kg) is equal to 2, why is it equal to 2, 2kg is equal to the weight of A 2kg, conforms to j >= wi , so (A, 2kg) is equal to max( (don't put things, 2kg), (don't put things ,2kg-2kg)+A's value), that is, max(0, 0+2)=2.

3) Let’s look at the back of the row A. Since i is unchanged, the previous item of max will always be (don’t put things, *)=0, and the latter item of max will always be (don’t put things, *)+A. Value=2, so there are all 2s behind the row A.

Fine product

4) Let us see why (B,1kg) is equal to 0, why is it equal to 0, 1kg is less than the weight of B, which is 2kg, conforms to j < wi , so (B,1kg) is equal to (B,0kg), which is equal to 0.

5) Let's see (B,2kg) is equal to 3, why is it equal to 3, 2kg is equal to the weight of B 2kg, conforms to j >= wi , so (B,2kg) is equal to max( (A,2kg), (A,2kg-2kg )+B value), that is, max(2, 0+3)=3.

6) Let's see (B,4kg) is equal to 5, why is it equal to 5, 4kg is greater than the weight of B 2kg, in line with j >= wi , so (B,4kg) is equal to max( (A,4kg), (A,4kg-2kg )+B value), that is, max(2, 2+3)=5.

Contrasting works

7) Let's see (D,1kg) is equal to 5, why is it equal to 5, 1kg is equal to the weight of D 1kg, and j >= wi , so (D,1kg) is equal to max( (C,1kg), (C,1kg-1kg )+D value), that is, max(0, 0+5)=5.

8) Let’s look at (F,10kg), 10kg is greater than F, and the weight is 2kg, in line with j >= wi , so (F,10kg) is equal to max((E,10kg), (E,10kg-2kg)+F value) , That is, max(14, 12+3)=15.

Finally, the answer to the question is that a 10kg backpack can hold up to 1 yuan and 5 cents.

 

Save a copy of the code, keep the formula in mind, and distinguish the and in the code .

def bag(n, c, w, v):
    """
    测试数据:
    n = 6  物品的数量,
    c = 10 书包能承受的重量,
    w = [2, 2, 3, 1, 5, 2] 每个物品的重量,
    v = [2, 3, 1, 5, 4, 3] 每个物品的价值
    """
    #置零,表示初始状态
    value = [[0 for j in range(c + 1)] for i in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(1, c + 1):

            #
            value[i][j] = value[i - 1][j]

            #
            if j >= w[i - 1] and value[i][j] < value[i - 1][j - w[i - 1]] + v[i - 1]:
                value[i][j] = value[i - 1][j - w[i - 1]] + v[i - 1]

    #打印动态规划表
    for x in value:
        print(x)

    return value

#w = list(map(int,input().split(' ')))
#v = list(map(int,input().split(' ')))
#c = eval(input())

w = [2, 2, 3, 1, 5, 2]
v = [2, 3, 1, 5, 4, 3]
c = 10
n = len(v)
print('{}'.format(bag(n,c,w,v)[n][c]))

 

Guess you like

Origin blog.csdn.net/XLcaoyi/article/details/115034607