Last Updated : 09 Nov, 2023

Summarize

Comments

Improve

Write a Python program for a given** N**items where each item has some weight and profit associated with it and also given a bag with capacity

**, [i.e., the bag can hold at most**

**W****weight in it]. The task is to put the items into the bag such that the sum of profits associated with them is the maximum possible.**

**W****Examples:**

N = 3, W = 4, profit[] = {1, 2, 3}, weight[] = {4, 5, 1}Input:3Output:There are two items which have weight less than or equal to 4. If we select the item with weight 4, the possible profit is 1. And if we select the item with weight 1, the possible profit is 3. So the maximum possible profit is 3. Note that we cannot put both the items with weight 4 and 1 together as the capacity of the bag is 4.Explanation:

N = 3, W = 3, profit[] = {1, 2, 3}, weight[] = {4, 5, 6}Input:0Output:

## Python Program for 0-1 Knapsack Problem using Recursion:

A simple solution is to consider all subsets of items and calculate the total weight and profit of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the subset with maximum profit.

Optimal SubstructureTo consider all subsets of items, there can be two cases for every item.:

The item is included in the optimal subset.Case 1:The item is not included in the optimal set.Case 2:

**Step-by-step approach:**

The maximum value obtained from ‘N’ items is the max of the following two values.

- Case 1 (include the N
^{th}item): Value of the N^{th}item plus maximum value obtained by remaining N-1 items and remaining weight i.e. (W-weight of the N^{th}item). - Case 2 (exclude the N
^{th}item): Maximum value obtained by N-1 items and W weight. - If the weight of the ‘N
^{th}‘ item is greater than ‘W’, then the Nth item cannot be included andis the only possibility.**Case 2**

Below is the implementation of the above approach:

## Python

`# A naive recursive implementation `

`# of 0-1 Knapsack Problem `

`# Returns the maximum value that `

`# can be put in a knapsack of `

`# capacity W `

`def`

`knapSack(W, wt, val, n): `

`# Base Case `

`if`

`n `

`=`

`=`

`0`

`or`

`W `

`=`

`=`

`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[n`

`-`

`1`

`] > W): `

`return`

`knapSack(W, wt, val, n`

`-`

`1`

`) `

`# return the maximum of two cases: `

`# (1) nth item included `

`# (2) not included `

`else`

`: `

`return`

`max`

`( `

`val[n`

`-`

`1`

`] `

`+`

`knapSack( `

`W`

`-`

`wt[n`

`-`

`1`

`], wt, val, n`

`-`

`1`

`), `

`knapSack(W, wt, val, n`

`-`

`1`

`)) `

`# end of function knapSack `

`# Driver Code `

`if`

`__name__ `

`=`

`=`

`'__main__'`

`: `

`profit `

`=`

`[`

`60`

`, `

`100`

`, `

`120`

`] `

`weight `

`=`

`[`

`10`

`, `

`20`

`, `

`30`

`] `

`W `

`=`

`50`

`n `

`=`

`len`

`(profit) `

`print`

`knapSack(W, weight, profit, n) `

`# This code is contributed by Nikhil Kumar Singh `

**Output**

220

** Time Complexity:**O(2

^{N})

**O(N), Stack space required for recursion**

**Auxiliary Space:**## Python Program for 0-1 Knapsack Problem using Dynamic Programming:

__Memoization Approach for 0/1 Knapsack Problem:__

If we get a subproblem the first time, we can solve this problem by creating a 2-D array that can store a particular state (n, w). Now if we come across the same state (n, w) again instead of calculating it in exponential complexity we can directly return its result stored in the table in constant time.

Below is the implementation of the above approach:

## Python3

`# This is the memoization approach of `

`# 0 / 1 Knapsack in Python in simple `

`# we can say recursion + memoization = DP `

`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] `

`# Driver code `

`if`

`__name__ `

`=`

`=`

`'__main__'`

`: `

`profit `

`=`

`[`

`60`

`, `

`100`

`, `

`120`

`] `

`weight `

`=`

`[`

`10`

`, `

`20`

`, `

`30`

`] `

`W `

`=`

`50`

`n `

`=`

`len`

`(profit) `

`# We initialize the matrix with -1 at first. `

`t `

`=`

`[[`

`-`

`1`

`for`

`i `

`in`

`range`

`(W `

`+`

`1`

`)] `

`for`

`j `

`in`

`range`

`(n `

`+`

`1`

`)] `

`print`

`(knapsack(weight, profit, W, n)) `

`# This code is contributed by Prosun Kumar Sarkar `

**Output**

220

** Time Complexity:**O(N * W).As redundant calculations of states are avoided.

**O(N * W) + O(N).The use of a 2D array data structure for storing intermediate states and O(N) auxiliary stack space(ASS) has been used for recursion stack**

**Auxiliary Space:****Bottom-up Approach for 0/1 Knapsack Problem:**

**Bottom-up Approach for 0/1 Knapsack Problem:**Since subproblems are evaluated again, this problem has Overlapping Sub-problems property. So the 0/1 Knapsack problem has both properties (seethisandthis) of a dynamic programming problem. Like other typicalDynamic Programming(DP) problems, re-computation of the same subproblems can be avoided by constructing a temporary array K[][] in a bottom-up manner.

Below is the implementation of the above approach:

## Python3

`# A Dynamic Programming based Python `

`# Program for 0-1 Knapsack problem `

`# Returns the maximum value that can `

`# be put in a knapsack of capacity W `

`def`

`knapSack(W, wt, val, n): `

`K `

`=`

`[[`

`0`

`for`

`x `

`in`

`range`

`(W `

`+`

`1`

`)] `

`for`

`x `

`in`

`range`

`(n `

`+`

`1`

`)] `

`# Build table K[][] in bottom up manner `

`for`

`i `

`in`

`range`

`(n `

`+`

`1`

`): `

`for`

`w `

`in`

`range`

`(W `

`+`

`1`

`): `

`if`

`i `

`=`

`=`

`0`

`or`

`w `

`=`

`=`

`0`

`: `

`K[i][w] `

`=`

`0`

`elif`

`wt[i`

`-`

`1`

`] <`

`=`

`w: `

`K[i][w] `

`=`

`max`

`(val[i`

`-`

`1`

`] `

`+`

`K[i`

`-`

`1`

`][w`

`-`

`wt[i`

`-`

`1`

`]], `

`K[i`

`-`

`1`

`][w]) `

`else`

`: `

`K[i][w] `

`=`

`K[i`

`-`

`1`

`][w] `

`return`

`K[n][W] `

`# Driver code `

`if`

`__name__ `

`=`

`=`

`'__main__'`

`: `

`profit `

`=`

`[`

`60`

`, `

`100`

`, `

`120`

`] `

`weight `

`=`

`[`

`10`

`, `

`20`

`, `

`30`

`] `

`W `

`=`

`50`

`n `

`=`

`len`

`(profit) `

`print`

`(knapSack(W, weight, profit, n)) `

`# This code is contributed by Bhavya Jain `

**Output**

220

** Time Complexity:**O(N * W).where ‘N’ is the number of elements and ‘W’ is capacity.

**O(N * W).The use of a 2-D array of size ‘N*W’.**

**Auxiliary Space:****Space optimized Approach for 0/1 Knapsack Problem using Dynamic Programming:**

**Space optimized Approach for 0/1 Knapsack Problem using Dynamic Programming:**For calculating the current row of the dp[] array we require only previous row, but if we start traversing the rows from right to left then it can be done with a single row only.

Below is the implementation of the above approach:

## Python3

`# Python code to implement the above approach `

`def`

`knapSack(W, wt, val, n): `

`# Making the dp array `

`dp `

`=`

`[`

`0`

`for`

`i `

`in`

`range`

`(W`

`+`

`1`

`)] `

`# Taking first i elements `

`for`

`i `

`in`

`range`

`(`

`1`

`, n`

`+`

`1`

`): `

`# Starting from back, `

`# so that we also have data of `

`# previous computation when taking i-1 items `

`for`

`w `

`in`

`range`

`(W, `

`0`

`, `

`-`

`1`

`): `

`if`

`wt[i`

`-`

`1`

`] <`

`=`

`w: `

`# Finding the maximum value `

`dp[w] `

`=`

`max`

`(dp[w], dp[w`

`-`

`wt[i`

`-`

`1`

`]]`

`+`

`val[i`

`-`

`1`

`]) `

`# Returning the maximum value of knapsack `

`return`

`dp[W] `

`# Driver code `

`if`

`__name__ `

`=`

`=`

`'__main__'`

`: `

`profit `

`=`

`[`

`60`

`, `

`100`

`, `

`120`

`] `

`weight `

`=`

`[`

`10`

`, `

`20`

`, `

`30`

`] `

`W `

`=`

`50`

`n `

`=`

`len`

`(profit) `

`print`

`(knapSack(W, weight, profit, n)) `

`# This code is contributed by Suyash Saxena`

**Output**

220

** Time Complexity**: O(N * W).As redundant calculations of states are avoided

**: O(W) As we are using a 1-D array instead of a 2-D array**

**Auxiliary Space**Please refer complete article on Dynamic Programming | Set 10 ( 0-1 Knapsack Problem) for more details!

Next Article

Python Program for Fractional Knapsack Problem