Home » Interview coding problems/challenges

# 0-1 Knapsack Algorithm

Here, we are going to learn about the **0-1 Knapsack Algorithm along with the explanation, algorithm, and example**.

Submitted by Vivek Kothari, on December 02, 2018

**Problem statement:**

We have given items i_{1}, i_{2} , ..., i_{n} (the item we want to put in our bag) with associated weights w_{1}, w_{2}, ... w_{n} and profit values V_{1}, V_{2}, ... V_{n}. Now the problem is **how we can maximize the total benefit given a capacity of the bag is W and each item is allowed to be used for 0 or 1 time**?

Generally, there are two **Knapsack problems** first is **fractional knapsack** and second is **0-1 knapsack**. In this article, we are discussing **0-1 knapsack algorithm**. In **fractional knapsack**, you can cut a fraction of object and put in a bag but in 0-1 knapsack either you take it completely or you don’t take it.

In order to solve the **0-1 knapsack problem**, our greedy method fails which we used in the fractional knapsack problem. So the only method we have for this optimization problem is solved using Dynamic Programming, for applying Dynamic programming to this problem we have to do three things in this problem:

- Optimal substructure
- Writing the recursive equation for substructure
- Whether subproblems are repeating or not

Now assume we have **'n'** items **1 2 3 ... N**. I will take an item and observe that there are two ways to consider the item either

- it could be included in knapsack
- or you might not include it in knapsack

Likewise, every element has 2 choices. Therefore we have **2X2X2X2...** Upto **n** choices i.e **2^n choices**.

We have to consider the **2^n solution** to find out the optimal answer but now we have to find that is there any repeating substructure present in the problem so that exempt from examining **2^n solutions**.

The recursive equation for this problem is given below:

knapsack(i,w) = { max( Vi +knapsack(i-1,W-wi) , knapsack(i-1,W) ) 0,i=0 & W=0 Knapsack(i-1,W) , wi> W }

**Knapsack(i-1,W) :** is the case of not including the ith item. In this case we are not adding any size to knapsack.

**Vi + Knapsack(i-1,W-wi) :** indicates the case where we have selected the ith item. If we add ith item then we need to add the value Vito the optimal solution.

Number of unique subproblems in **0-1 knapsack problem** is **(n X W)**. We use tabular method using Bottom-up Dynamic programming to reduce the time from **O(2^n) to O(n X W)**.

### Algorithm using Bottom up Dynamic Programming:

Input : {w1,w2,.........wn }, W , {V1 ,V2 ,……..Vn} Output : T[n,W] 1. for i = 0 to W do T[0,i] = 0 2. For i = 1 to n { 3. B[I,o] = 0 4. For(j=1 to W) do 5. If(wk<= j) and T[i-1,j-wk] + Vk> T[i-1,j] 6. Then T[i,j] = T[i-1,j-wk] + Vk 7. Else T[I,j] = T[i-1,j] }

## C++ Implementation of Knapsack problem

**1) Using recursive method **

#include<stdio.h> // function that find maximum of two numbers int max(int a, int b) { return (a > b)? a : b; } int knapsack(int W, int w[], int V[], int n) { if (n == 0 || W == 0) return 0; // If weight of the nth item is larger than capacity(W) of Knapsack if (w[n-1] > W) return knapsack(W, w, V, n-1); // Return max(nth item included, nth item not included) else return max( V[n-1] + knapsack(W-w[n-1], w, V, n-1), knapsack(W, w, V, n-1) ); } // Driver program to test above function int main() { int w[] = {5, 4, 6, 3}; int V[] = {10, 40, 30, 50}; int W = 10; int n = 4; int Max; printf("details of all items : \n"); printf("Value\\Profit\tWeight\t\n"); for (int i = 0; i < n; i++) { printf("%d\t\t%d\n",V[i],w[i]); } printf("\n"); Max = knapsack(W, w, V, n); printf("Maximum profit we can obtain = %d", Max); return 0; }

**Output**

details of all items : Value\Profit Weight 10 5 40 4 30 6 50 3 Maximum profit we can obtain = 90

**2) Using Bottom-up Dynamic programming**

#include<stdio.h> int max(int a, int b) { return (a > b)? a : b; } int knapsack(int W, int w[], int V[], int n) { int i, j; int T[n+1][W+1]; for (i = 0; i <= n; i++) { for (j = 0; j <= W; j++) { if (i==0 || j==0) T[i][j] = 0; else if (w[i-1] <= j) T[i][j] = max(V[i-1] + T[i-1][j-w[i-1]], T[i-1][j]); else T[i][j] = T[i-1][j]; } } printf("DP table T[i,j] : \n"); for(i=0;i<=n;i++) { printf("i=%d\t",i); for(j=0;j<=W;j++) { printf("%d\t",T[i][j]); } printf("\n"); } printf("\n"); return T[n][W]; } int main() { int w[] = {5, 4, 6, 3}; int V[] = {10, 40, 30, 50}; int W = 10; int n = 4; int Max; printf("details of all items : \n"); printf("Value\\Profit\tWeight\t\n"); for (int i = 0; i < n; i++) { printf("%d\t\t%d\n",V[i],w[i]); } printf("\n"); Max = knapsack(W, w, V, n); printf("Maximum profit we can obtain = %d", Max); return 0; }

**Output**

details of all items : Value\Profit Weight 10 5 40 4 30 6 50 3 DP table T[i,j] : i=0 0 0 0 0 0 0 0 0 0 0 0 i=1 0 0 0 0 0 10 10 10 10 10 10 i=2 0 0 0 0 40 40 40 40 40 50 50 i=3 0 0 0 0 40 40 40 40 40 50 70 i=4 0 0 0 50 50 50 50 90 90 90 90 Maximum profit we can obtain = 90

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL