×

String Coding Problems

Arrays Coding Problems

Sorting Coding Problems

Searching Coding Problems

Coding Algorithms

Tree Coding Problems

Stack Coding Problems

Linked list Coding Problems

Graph Coding Problems

Greedy Algorithms Coding Problems

Dynamic Programming Coding Problems

Matrix Coding Problems

Recursion Coding Problems

Number Theory Coding Problems

Backtracking Coding Problems

Heap Coding Problems

Brute Force Coding Problems

Implementation Coding Problems

Google Contests

Competitive Programming Coding

Miscellaneous

0-1 Knapsack Algorithm

Here, we are going to learn about the 0-1 Knapsack Algorithm along with the explanation, algorithm, and example. By Vivek Kothari Last updated : April 21, 2024

Problem statement

We have given items i1, i2 , ..., in (the item we want to put in our bag) with associated weights w1, w2, ... wn and profit values V1, V2, ... Vn. 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.

Solution of 0-1 Knapsack Algorithm

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:

  1. Optimal substructure
  2. Writing the recursive equation for substructure
  3. 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

  1. it could be included in knapsack
  2. 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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.