Answer:
Check the explanation
Step-by-step explanation:
weights = [2,3,4,5,9] #assign the weights here
value = [3,4,8,8,10] #assign the values here
#recursive function to get the maximum weight for a given knapsack
def getMaxWeight(weights,value,W,n):
#check if any elements are there or check if the knapsack can hold any more weights
if(n==0 or W==0):
return 0
elif(weights[n]>W):
#if the current value of the weight is greater than what a knapsack can hold then discard that
return getMaxWeight(weights,value,W,n-1)
else:
#either choose the current weight or do not choose based on the maximum values
return max(value[n]+getMaxWeight(weights,value,W-weights[n],n-1),getMaxWeight(weights,value,W,n-1))
if __name__ == '__main__':
W=20
n=len(weights)-1
print("Weights are:",weights)
print("Values are:",value)
print("Maximum value for given knapsack is:",getMaxWeight(weights,value,W,n))
Kindly check the attached output images below.