Skip to content
Open
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
39 changes: 39 additions & 0 deletions knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# KnapSack Problem

# In this problem, we will be given an array of values with its respective weights.
# We will have a knapsack capacity. We will have to find the maximum value which is
# less than the capacity .

# Note : We cannot break an item. Either we pick a whole item or we do not pick that.


def knapsack(capacity, weights, values, size):

nums = [0 for i in range(capacity+1)]

for i in range(1, size+1):
# starting backwards to store the value of the previous computation.
for j in range(capacity, 0, -1):

# calculating the maximum value
if weights[i-1] <= j:
nums[j] = max(nums[j], nums[j-weights[i-1]]+values[i-1])

return nums[capacity]


values = [] # array of the values
weights = [] # array of the weighs

size = int(input("Enter the number of items to be inserted "))

for i in range(0, size):
val = int(input("Enter value of item "))
weight = int(input("Enter weight of item "))

values.append(val)
weights.append(weight)

capacity = int(input("Enter the knapsack capacity "))

print("The maximum value is ", knapsack(capacity, weights, values, size))