def'''Programming knapSack(WAssignment ,#4: wtQuestion 1'''
def knapsack(max_weight, valweights, values, nmax_value):
'''Recursive method for the knapsack problem (with memoization)'''
# Base Case
if nmax_value == 0 or Wmax_weight == 0 :
return 0
# If weight of the nth item is more than Knapsack of capacity
# W, then this item cannot be included in the optimal solution
if (wt[nweights[max_value-1] > W)max_weight:
return knapSackknapsack(W max_weight, wt weights, val values, nmax_value-1)
# Check for required value in lookup table first
if (lookup[n][W]LOOKUP[max_value][max_weight] != -1):
return lookup[n][W] LOOKUP[max_value][max_weight]
# Add to lookup table and return the maximum of two cases:
# (1) nth item included
# (2) not included
else:val1
= knapsack(max_weight-WEIGHTS[max_value-1], weights, values, max_value-1)
xval1 =+= max(val[nVALUES[max_value-1]
+ knapSack(W-wt[n-1] , wt# ,val1 valstores ,the n-1),maximum knapSack(Wpossible ,profit wtwhen the last item is included
val2 = knapsack(max_weight, valweights, values, nmax_value-1))
# val2 stores the lookup[n][W]maximum =possible x
profit when the last item is excluded
return x temp = max(val1, val2)
LOOKUP[max_value][max_weight] = temp
#End of function knapSack return temp
#End of function knapsack
#To test above function
valVALUES = []
wtWEIGHTS = []
#Reading the data file
with open("knapsack.txt") as f:
first_lineFIRST_LINE = f.readline().split()
WMAX_WEIGHT = int(first_line[0]FIRST_LINE[0])
nMAX_VALUE = int(first_line[1]FIRST_LINE[1])
for line in f:
words = line.split()
val VALUES.append(int(words[0]))
wt WEIGHTS.append(int(words[1]))
#Declaring and initializing the lookup table
LOOKUP = [[-1 for i in range(W+1MAX_WEIGHT+1)] for j in range(n+1MAX_VALUE+1)]
#Function knapsack is invoked
print knapSackknapsack(WMAX_WEIGHT, wtWEIGHTS, valVALUES, nMAX_VALUE)
def'''Programming knapSack(WAssignment ,#4: wtQuestion 2'''
def knapsack(max_weight, valweights, values, nmax_value):
'''Recursive method for the knapsack problem (with memoization)'''
# Base Case
if nmax_value == 0 or Wmax_weight == 0 :
return 0
# If weight of the nth item is more than Knapsack of capacity
# W, then this item cannot be included in the optimal solution
if (wt[nweights[max_value-1] > W)max_weight:
return knapSackknapsack(W max_weight, wt weights, val values, nmax_value-1)
# Check for required value in lookup table first
if (lookup[n][W]LOOKUP[max_value][max_weight] != -1):
return lookup[n][W] LOOKUP[max_value][max_weight]
# Add to lookup table and return the maximum of two cases:
# (1) nth item included
# (2) not included
else:val1
= knapsack(max_weight-WEIGHTS[max_value-1], weights, values, max_value-1)
xval1 =+= max(val[nVALUES[max_value-1]
+ knapSack(W-wt[n-1] , wt# ,val1 valstores ,the n-1),maximum knapSack(Wpossible ,profit wtwhen the last item is included
val2 = knapsack(max_weight, valweights, values, nmax_value-1))
# val2 stores the lookup[n][W]maximum =possible x
profit when the last item is excluded
return x temp = max(val1, val2)
LOOKUP[max_value][max_weight] = temp
#End of function knapSack return temp
#End of function knapsack
#To test above function
valVALUES = []
wtWEIGHTS = []
#Reading the data file
with open("knapsack_big.txt") as f:
first_lineFIRST_LINE = f.readline().split()
WMAX_WEIGHT = int(first_line[0]FIRST_LINE[0])
nMAX_VALUE = int(first_line[1]FIRST_LINE[1])
for line in f:
words = line.split()
val VALUES.append(int(words[0]))
wt WEIGHTS.append(int(words[1]))
#Declaring and initializing the lookup table
LOOKUP = [[-1 for i in range(W+1MAX_WEIGHT+1)] for j in range(n+1MAX_VALUE+1)]
#Function knapsack is invoked
print knapSackknapsack(WMAX_WEIGHT, wtWEIGHTS, valVALUES, nMAX_VALUE)