Skip to main content

Knapsack problem

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsackand must fill it with the most valuable items.


0/1 Knapsack Problem: Dynamic Programming Approach:

Knapsack Problem:

Knapsack is basically means bag. A bag of given capacity.
We want to pack n items in your luggage.
  • The ith item is worth vi dollars and weight wi pounds.
  • Take as valuable a load as possible, but cannot exceed W pounds.
  • vi wi W are integers.

0/1 Knapsack Problem: Dynamic Programming Approach:

Knapsack Problem:

Knapsack is basically means bag. A bag of given capacity.
We want to pack n items in your luggage.
  • The ith item is worth vi dollars and weight wi pounds.
  • Take as valuable a load as possible, but cannot exceed W pounds.
  • vi wi W are integers.

    0/1 Knapsack Problem: Dynamic Programming Approach:

    Knapsack Problem:

    Knapsack is basically means bag. A bag of given capacity.
    We want to pack n items in your luggage.
    • The ith item is worth vi dollars and weight wi pounds.
    • Take as valuable a load as possible, but cannot exceed W pounds.
    • vi wi W are integers.

      0/1 Knapsack Problem: Dynamic Programming Approach:

      Knapsack Problem:

      Knapsack is basically means bag. A bag of given capacity.
      We want to pack n items in your luggage.
      • The ith item is worth vi dollars and weight wi pounds.
      • Take as valuable a load as possible, but cannot exceed W pounds.
      • vi wi W are integers.
  • Knapsack of capacity
  • List (Array) of weight and their corresponding value.
Output: To maximize profit and minimize weight in capacity.
The knapsack problem where we have to pack the knapsack with maximum value in such a manner that the total weight of the items should not be greater than the capacity of the knapsack.
Knapsack problem can be further divided into two parts:
1. Fractional Knapsack: Fractional knapsack problem can be solved by Greedy Strategy where as 0 /1 problem is not.
It cannot be solved by Dynamic Programming Approach.

0/1 Knapsack Problem:

In this item cannot be broken which means thief should take the item as a whole or should leave it. That's why it is called 0/1 knapsack Problem.
  • Each item is taken or not taken.
  • Cannot take a fractional amount of an item taken or take an item more than once.
  • It cannot be solved by the Greedy Approach because it is enable to fill the knapsack to capacity.
  • Greedy Approach doesn't ensure an Optimal Solution.

Example of 0/1 Knapsack Problem:

Example: The maximum weight the knapsack can hold is W is 11. There are five items to choose from. Their weights and values are presented in the following table:
Example of 0/1 Knapsack Problem:
The [i, j] entry here will be V [i, j], the best value obtainable using the first "i" rows of items if the maximum capacity were j. We begin by initialization and first row.
Example of 0/1 Knapsack Problem:
V [i, j] = max {V [i - 1, j], vi + V [i - 1, j -wi]
Example of 0/1 Knapsack Problem 
Example of 0/1 Knapsack Problem
The value of V [3, 7] was computed as follows:
V [3, 7] = max {V [3 - 1, 7], v3 + V [3 - 1, 7 - w3]
         = max {V [2, 7], 18 + V [2, 7 - 5]} 
         = max {7, 18 + 6}
         = 24
Example of 0/1 Knapsack Problem
Finally, the output is:
Example of 0/1 Knapsack Problem
The maximum value of items in the knapsack is 40, the bottom-right entry). The dynamic programming approach can now be coded as the following algorithm:

Algorithm of Knapsack Problem

KNAPSACK (n, W)
 1. for w = 0, W
 2. do V [0,w] ← 0
 3. for i=0, n
 4. do V [i, 0] ← 0  
 5. for w = 0, W
 6. do if (wi≤ w & vi + V [i-1, w -  wi]> V [i -1,W])
 7. then V [i, W] ← vi + V [i - 1, w - wi]
 8. else V [i, W] ← V [i - 1, w]

Comments

Popular posts from this blog

Eight queens puzzle

Eight queens puzzle Eight Queens Puzzle  The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general n queens problem of placing n non-attacking queens on an n×n chessboard, for which solutions exist for all natural numbers n with the exception of n=2 and n=3. The eight queens puzzle has 92 distinct solutions. If solutions that differ only by the symmetry operations of rotation and reflection of the board are counted as one, the puzzle has 12 solutions. These are called fundamental solutions; representatives of each are shown below. A fundamental solution usually has eight variants (including its original form) obtained by rotating 90, 180, or 270° and then reflecting each of the four rotational variants in a mirror in a fixed position. However, should a soluti

Sorting Operations in Design Analysis and Algorithm

/* Implementing  sorting in a C Program  * Written by: Riddhi Lalakiya.   */ Sorting Algorithms in C 1.BubbleSort : 1. Algorithm : begin BubbleSort(list)    for all elements of list       if list[i] > list[i+1]          swap(list[i], list[i+1])       end if    end for        return list     end BubbleSort 2. Pseudocode : procedure bubbleSort( list : array of items )    loop = list.count;        for i = 0 to loop-1 do:       swapped = false       for j = 0 to loop-1 do:                 /* compare the adjacent elements */             if list[j] > list[j+1] then             /* swap them */             swap( list[j], list[j+1] )               swapped = true          end if                 end for              /*if no number was swapped that means        array is sorted now, break the loop.*/              if(not swapped) then          break       end if           end for