knapsack problem
What is a knapsack problem?
Suppose
you have given a knapsack or bag with a limited weight capacity, and each item
has some weight and value. The problem here is that "Which item is to be
placed in the knapsack such that the weight limit does not exceed and the total
value of the items is as large as possible?".
Consider
the real-life example. Suppose there is a thief and he enters the museum. The
thief contains the knapsack, or we can say bag that has limited weight
capacity. The museum contains various items of different values. The thief
decides what items are should he keep in the bag so that profit would become
maximum.
Some
important points related to the knapsack problem are:
- It
is a combinatorial optimization-related problem.
- Given
a set of N items - usually numbered from 1 to N; each of these items has a
mass wi and a value vi.
- It
determines the number of each item to be included in a collection so that
the total weight M is less than or equal to a given limit and the total
value is as large as possible.
- The
problem often arises in resource allocation where there are financial
constraints.
Suppose we
have a knapsack of 15 kg, so M = 15 kg. It means that the total weight of the
knapsack is 15 kg. The total weight of the items to be included in the knapsack
should have the weight less than or equal to the M. Some items are given which
have some key features, i.e., weight of the item, and the value of the item. As
we can observe below that the first item has the weight as 12kg and the value
as $4, second item has the weight as 2kg and the value as $2, third item has
the weight as 1 kg and the value as $1, fourth item has the weight as 4 kg and
the value as $10, and the fifth item has the weight as 1 kg and the value as
the $2. Here, we have to decide whether we have to include the item or not.
Knapsack problem variants
There are two variants of knapsack problem:
- 0/1 knapsack problem: In the case of 0/1 knapsack problem, items are
indivisible. Here, indivisible means that we cannot break an item. In
this, we can either take an item or not. We either take the item
completely and keep in the knapsack or we leave the item completely. There
is no possibility that we keep some fraction of the item in the knapsack.
Here, '0'
means that we are not taking that item and '1' means that we are taking the
item completely. This type of problem is solved by using the dynamic
programming approach.
Some important points related to the 0/1 Knapsack problem
are:
- In
this problem, we cannot take the fraction of the items. Here, we have to
decide whether we have to take the item, i.e., x = 1 or not, i.e., x = 0.
- The
greedy approach does not provide the optimal result in this problem.
- Another
approach is to sort the items by cost per unit weight and starts from the
highest until the knapsack is full. Still, it is not a good solution.
Suppose there are N items. We have two options either we select or exclude
the item. The brute force approach has O(2N)
exponential running time. Instead of using brute force approach, we use
the dynamic programming approach to obtain the optimal solution.
1.
Dynamic-0-1-knapsack (v, w, n, W)
2.
for w = 0 to W do
3.
c[0, w] = 0
4.
for i = 1 to n do
5.
c[i, 0] = 0
6.
for w = 1 to W do
7.
if wi ? w then
8.
if vi + c[i-1, w-wi] then
9.
c[i, w] = vi + c[i-1, w-wi]
10.
else c[i, w] = c[i-1, w]
11.
else
12. c[i, w] = c[i-1, w]
M.SarulathaAssistant Professor
M.
Comments
Post a Comment