Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions dynamic_programming/knapsack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
"""
Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack.
"""
def MF_knapsack(i,wt,val,j):
'''
This code involves the concept of memory functions. Here we solve the subproblems which are needed
unlike the below example
F is a 2D array with -1s filled up
'''
global F # a global dp table for knapsack
if F[i][j] < 0:
if j < wt[i - 1]:
val = MF_knapsack(i - 1,wt,val,j)
else:
val = max(MF_knapsack(i - 1,wt,val,j),MF_knapsack(i - 1,wt,val,j - wt[i - 1]) + val[i - 1])
F[i][j] = val
return F[i][j]

def knapsack(W, wt, val, n):
dp = [[0 for i in range(W+1)]for j in range(n+1)]

Expand All @@ -12,13 +27,16 @@ def knapsack(W, wt, val, n):
dp[i][w] = dp[i-1][w]

return dp[n][w]
if __name__ == "__main__":

if __name__ == '__main__':
'''
Adding test case for knapsack
'''
val = [3,2,4,4]
wt = [4,3,2,3]
W = 6
n = 4
'''
Should give 8
'''
print(knapsack(W,wt,val,n))

w = 6
F = [[0]*(w + 1)] + [[0] + [-1 for i in range(w + 1)] for j in range(n + 1)]
print(knapsack(w,wt,val,n))
print(MF_knapsack(n,wt,val,w)) # switched the n and w