Knapsack Lect
Knapsack Lect
Knapsack
Intro
What is knapsack problem?
Solution to knapsack problem
0-1 knapsack
Fractional knapsack + Example
Dynamic programming + Example
Time complexity
Introduction
A knapsack is a bag with two straps that you wear over your shoulders, leaving
your arms free.
Don't forget your knapsack when you head out on that hiking trip!
Knapsack can also be name as "backpack" or a "rucksack."
The word knapsack is thought to be the oldest of these terms, and while
"backpack" is more often used today in the U.S, knapsack is more common in
Canada.
What is knapsack problem?
In the supermarket there are n packages (n ≤ 100) the package i has weight W[i]
≤ 100 and value V[i] ≤ 100.
A thief breaks into the supermarket, the thief cannot carry weight exceeding M
(M ≤ 100).
The problem to be solved here is: which packages the thief will take away to get
the highest value?
Input:
Maximum weight M and the number of packages n.
Array of weight W[i] and corresponding value V[i].
Output:
Maximize value and corresponding weight in capacity.
Which packages the thief will take away.
Solution to knapsack problem
A simple solution is to consider all subsets of items and calculate the total
weight and value of all subsets.
Consider the only subsets whose total weight is smaller than W. From all such
subsets, pick the maximum value subset.
Knapsack problem can be further divided into two types:
The 0/1 knapsack problem. in this type, each package can be taken or not
taken. besides, the thief cannot take a fractional amount of a taken package or
take a package more than once.
Fractional knapsack problem: This type can be solved by greedy strategy.
This type can be solved by dynamic programming approach.
0/1 Knapsack
In 0-1 knapsack, items cannot be broken which means the thief should take the
item as a whole or should leave it. this is reason behind calling it as 0-1
knapsack.
Hence, in case of 0-1 knapsack, the value can be either 0 or 1, where other
constraints remain the same.
0-1 knapsack cannot be solved by greedy approach. greedy approach does not
ensure an optimal solution. in many instances, greedy approach may give an
optimal solution.
Fractional knapsack
In Fractional Knapsack, we can break items for maximizing the total value
of knapsack.
This problem in which we can break an item is also called the fractional
knapsack problem.
Algorithm of Fractional Knapsack
for i 1 to size(v)
i 1;
{
if w[i] <= M
M M - w[i];
if
{
w[i]> M;
i i+1
}
Example of fractional knapsack
M=15
Objects 1 2 3 4 5 6 7
Profit 10 5 18 7 6 18 3
2 3 5 7 1 4 1
Weight
x1 x2 x3 x4 x5 x6 x7
1 2/3 1 0 1 1 1
The objective is that the
Profit 10 5 15 7 6 18 3
profit is greater than weight
Recursive backtracking starts with max capacity and makes choice for items.
Choices are:
▪ take the item if it fits
▪ don't take the item
Dynamic programming start with the simpler problems.
Reduce number of items available
AND Reduce weight limit on knapsack