-
Notifications
You must be signed in to change notification settings - Fork 0
/
knapsack_O(n_sumap).py
44 lines (35 loc) · 1.06 KB
/
knapsack_O(n_sumap).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def knapsack(W, P, maxw):
n = len(W)
maxp = 0
for i in range(n):
maxp += P[i]
F = [[maxw+1]*(maxp+1) for i in range(n)]
Parent = [[maxw + 1] * (maxp + 1) for i in range(n)]
for i in range(n):
F[i][P[i]] = W[i]
for i in range(1, n):
for j in range(maxp+1):
F[i][j] = min(F[i-1][j], F[i][j])
if j - P[i] >= 0:
F[i][j] = min(F[i][j], (F[i-1][j-P[i]] + W[i]))
for i in range(maxp, -1, -1):
if F[n-1][i] != maxw+1:
return i, F, maxp
def getsolution(F, W, P, i,j, maxp):
if i == 0:
if F[i][j] == maxp+1:
return []
print(j)
return [i]
if F[i][j] == (F[i-1][j-P[i]] + W[i]):
return getsolution(F, W, P, i-1, j-P[i], maxp)+[i]
return getsolution(F, W, P, i-1, j, maxp)
W = [4, 3, 2, 2, 15, 1, 5]
P = [5, 7, 9, 3, 20, 3, 15]
maxw = 6
n = len(W)
res, F, maxp = knapsack(W, P, maxw)
print(res)
print(getsolution(F,W,P,n-1,res, maxp+1))
for i in range(n):
print(F[i])