-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem-22.py
48 lines (31 loc) · 891 Bytes
/
Problem-22.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
capacity = 50
weights = [10,20,30]
values = [60,100,120]
# Algorithm 1 : Brute Force
def knapsack(w,v,c,n):
if c == 0 or n == 0 :
res = 0
elif w[n-1] > c :
res = knapsack(w,v,c,n-1)
else:
skip_item = knapsack(w,v,c,n-1)
add_item = knapsack(w,v,c-w[n-1],n-1) + v[n-1]
res = max(skip_item,add_item)
return res
res = knapsack(weights,values,capacity,3)
print("Brute Force : ",res)
# Algorithm 2 : Dynamic Programming
dp = [[0]*50]*3
def knapsack(w,v,c,n):
if c == 0 or n == 0 :
res = 0
elif w[n-1] > c :
res = knapsack(w,v,c,n-1)
else:
skip_item = knapsack(w,v,c,n-1)
add_item = knapsack(w,v,c-w[n-1],n-1) + v[n-1]
res = max(skip_item,add_item)
dp[n-1][c-1] = res
return res
res = knapsack(weights,values,capacity,3)
print("Dynamic Programming : ",res)