-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.py
More file actions
24 lines (18 loc) · 714 Bytes
/
knapsack.py
File metadata and controls
24 lines (18 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
0/1 Knapsack Problem
We are given N items where each item has some weight and profit associated with it. We are also given a bag with capacity W, [i.e., the bag can hold at most W weight in it]. The target is to put the items into the bag such that the sum of profits associated with them is the maximum possible.
"""
def knapsack(self,W,wt,val,N):
def solve(n,cap):
if n==0 or cap==0:
return 0
else:
cwt=wt[n-1]
cv=val[n-1]
if cwt<=cap:
c1=cv+solve(n-1,cap-cwt)
c2=solve(n-1,cap)
return max(c1,c2)
else:
return 0+solve(n-1,cap)
return solve(N,W)