-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack_dynamic.py
64 lines (52 loc) · 1.35 KB
/
knapsack_dynamic.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
def matzero(rows,cols):
row=[]
data=[]
for i in range(cols):
row.append(0)
for i in range(rows):
data.append(row[:])
return data
ans=matzero(10,10)
def itemUsed(w,c):
i=len(c)-1
currentW = len(c[0])-1
tick=[]
for i in range(i+1):
tick.append(0)
while(i>=0 and currentW>=0):
if (i == 0 and c[i][currentW] > 0) or c[i][currentW] != c[i-1][currentW]:
tick[i]=1
currentW=currentW-w[i]
i-1
return tick
def knapsack(v,w,W):
c=[]
n=len(v)
c=matzero(n,W+1)
for i in range(0,n):
for j in range(0,W+1):
if(w[i]>1):
c[i][j]=c[i-1][j]
else:
c[i][j] = max(c[i-1][j],v[i] +c[i-1][j-w[i]])
print(c)
return [c[n-1][W],itemUsed(w,c)]
if (len(sys.argv)!=3):
print("Please provide input in following manner for knapsack dynamic programming.")
print("knapsack.py 1-2,5-5,7-5 10 // second argument is maximum cost W ")
quit()
items=sys.argv[1].split(',')
w=[]
v=[]
total=0
for item in items :
num=item.split('-')
w.append(int(num[0]))
v.append(int(num[1]))
maxc=int(sys.argv[2])
answer = knapsack(v,w,maxc)
print("If knapsack can hold %d pounds ,i can get %d profit"% (maxc,answer[0]))
for i in range(len(answer)):
if(answer[1][i]!=0):
print(i+1)