0 - 1 Knapsack Problem Python
PROGRAM TO SOLVE 0-1 KNAPSACK PROBLEM
OUTPUT:
220
# driver codeval = [60, 100, 120 ]wt = [10, 20, 30 ]W = 50n = len(val)# We initialize the matrix with -1 at first.t = [[-1 for i in range(W + 1)] for j in range(n + 1)]def knapsack(wt, val, W, n): # base conditions if n == 0 or W == 0: return 0 if t[n][W] != -1: return t[n][W] # choice diagram code if wt[n-1] <= W: t[n][W] = max( val[n-1] + knapsack( wt, val, W-wt[n-1], n-1), knapsack(wt, val, W, n-1)) return t[n][W] elif wt[n-1] > W: t[n][W] = knapsack(wt, val, W, n-1) return t[n][W]print(knapsack(wt, val, W, n))
220
Comments
Post a Comment