Sie sind auf Seite 1von 5

APPLICATION OF KNAPSACK ALGORITHM

Author : S Ramarajan

Year : III BE CSE

College : Shanmuganathan Engineering


College, Arasampatti.

Introduction:
ABSTRACT
The knapsack problem can be
Using Knapsack Algorithm (Greedy used to find the optimal solution to fill
Method) we are giving an innovative the knapsack with maximum profit.
thinking on how to get maximum energy Likewise, we can apply the same
from a meal. Usually, if we take the technique to obtain the optimal solution
energetic foods daily, we can gain more for eating a meal. So that, we can get
calories. But if you are given only a maximum calories by using the optimal
limited number of recipes, we not sure to solution.
get maximum energy. So in this paper
Before entering into the problem,
we are giving a way to reach maximum
it becomes must to know about the
energy from a meals using fractional
Greedy method and knapsack problem,
knapsack algorithm. The measures of
That is why, here, a brief introduction on
foods are taken using the weights and
Greedy method and knapsack problem
total calories are taken as the weights
using Greedy method are given.
and total calories are taken as the profits
for the knapsack algorithm. The optimal Greedy Method:
solution found out gives the ratio of
foods to take to eat. Application-Knapsack Problem:

• Input: A weight capacity C, and


n items of weights W[1:n] and
monetary value P[1:n].

• Problem: Determine which items


to take and how much of each item,
so that

 The total weight is <= C,


and
 The total value(Profit) is way to earn maximum energy per
maximized. a time. Here Human’s stomach is
• Formulation of the problem: considered as a knapsack or bag.
Let x[i] be the fraction taken from item i.
The capacity of
0 <= x[i] <= 1.
stomach is taken as a constraint
The weight of the part taken from for this problem. The capacity of
item i is x[i]*w[i]
stomach may vary according to
The corresponding profit is x[i]*P[i] the person who takes the food.
The problem is then to find the Because Gents, ladies, a person
values of the array x[1:n] So that who affected by disease, etc. can
X[1]P[1]+x[2]P[2]+….+x[n]P[n] is eat differently. Even though, in
maximized subject to the constraint this paper we are taken 1.5 liters
that
is the capacity that is constraint
x[1]w[1] +X[2]w[2] +……
for this problem.
+x[n]w[n] <= C

• Greedy Selection policy: Three This condition suits


natural possibilities for this problem, since a healthy

• Policy 1: Choose the lightest young person’s stomach can


remaining item, and take as much consume 1.5 liters without any
of it as can fit. troubles. i.e, m = 1.5 liters. We
• Policy 2: Choose the most give six numbers of objects. i.e.,
profitable remaining item, and
n=6
take as much of it as can fit.

• Policy 3: Choose the item with The objects are listed


the highest price per unit weight below.
(P[i]/W[i]) and take as much of it
as can fit. Rice (cooked) (2 ½ cups)

According to knapsack problem 1) Drumstick sambar (1 ½ cups)


using greedy Technique we can
2) Cabbage Curry (1/3 cup)
calculate or find which is the best
3) Baked eggs (2/3 cup) Then we have to set profits. As
this point of view, total can set as profit.
4) Butter milk gravy (1 cup)
So that nutritional information is
5) Payasam (2 cups) collected for each recipe.

The measures of the objects are The calories per each recipe are
given in cups. Using the standard kitchen given below:
conversion units we can calculate the
1) Rice (Cooked) = 800 calories
measures in milliliters.
2) Drumstick Sambar = 8224
The objects measure in milliliters.
calories
1) Rice (cooked) (2 ½ cups) = 590 ml
3) Cabbage curry = 36 calories
2) Drumstick sambar (1 ½ cups)= 354 ml
4) Baked eggs = 211 calories
3) Cabbage Curry (1/3 cup)= 79 ml
5) Butter milk gravy =98 calories
4) Baked eggs (2/3 cup) = 158 ml
Payasam (Sugar, Cream,
5) Butter milk gravy (1 cup)= 237 ml Vermicelli) = 300 calories

6) Payasam (2 cups) = 473 ml Profits are

These objects are to be loaded Pi = (P1, P2, P3, P4, P5, P6)
into the stomach while lunch or dinner.
Pi = (800, 224, 36, 211, 98, 300)
So, these are taken as objects similar to
weights in knapsack problem. Where 1 ≤ i ≤ n and n=6

i.e., wi = (w1 , w2 , w3 , w4 , w5 ,w6 )


= (Rice, Sambar, Cabbage curry, Baked
eggs, Buttermilk gravy, Payasam )

(w1, w2, w3, w4, w5, w6) = (590, 354,


79, 158, 237, 473) units are in ml.
The possible feasible and solution should have the maximum profit
objective solutions are calculated using in calories.
the expressions ∑WiXi and ∑PiXi
Wi = (590, 354, 79, 158, 237, 473)
respectively. Where Xi is the possible
assumptions we made based on which Pi = (800, 224, 36, 211, 98, 300)

recipe are taken by the person.

The following table gives the


feasible and objective solutions in which
we can choose the optimal solution that

∑WiXi ≤m ∑PiXi
Xi
(Feasible Solutions) (Objective Solutions)
590+354+79+158+237+473 800+224+36+211+98
1) (1,1 ,1,1,1,0)
⇒ 1418 ⇒1369
590+200+79+158+0+473 800+126.5+36+211+300
2) 1,200/354,1,1,0,1)
⇒1500 ⇒ 1429.7
590+0+79+158+200+473 800+36+211+82.7+300
3) 1,0,1,1,,200/237,1)
⇒ 1500 ⇒ 1429.7
4) 590+100+79+158+100+473 800+63.276+36+211+41.35+300
(1,100/354,1,1,100/237) ⇒ 1500 ⇒1451.266
590+354+158+237+161 800+224+211+98+102.114
5) 1,1,0,1,1,161/473)
⇒ 1500 ⇒ 1435.114
590+354+79+158+237+82
800+224+36+211+98+300x82/473
6) 1,1,1,1,1, 82/473 ⇒
⇒1421
1500
590+354+0+83+0+473 800+224+0+110.84+0+300
7) (1,1,0, 83/158,0,1)
⇒ 1500 ⇒ 1434.841
590+354+79+0+4+473 800+224+36+0+1.654+300
8) (1,1,1,0,4/237,1)
⇒ 1500 ⇒ 1361.654

590+354+0+0+83+473 800+224+0+0+83/237x98+300
9) (1,1,0,0,83/237,1)
⇒ 1500 ⇒ 1358.32
10) 590+354+61+158+237+100 800+224+61/79x36+211+98+10/473x300
(1,1,61/79,1,1,100/473) ⇒ 150 ⇒ 1424.22

When we compare all the


objective solutions, solution - 2 has the
high profit in calories (1473.55 calories).
So we can take this solution on optimal
solution. If we take the foods based on
this solution we can attain the maximum
energy.

The ten solutions of possible solutions


are in the table.

The first solution is calculated by


straight forward technique. This solution
gives us the profit 1369 calories only. Conclusion:
Also, it doesn’t satisfy the constraint
If we eat foods that have high
exactly.
nutrition we can attain the good profit.
Then in another objects are But, if you have only 6 items from that
arranged in knapsack in the form of only you take nutrition. When this
weights have maximum profit than situation arises, we can use this method
others. We are tried to get optimal and we can reach maximum profit we
solution if the ratio Pi / Xi has high value should remember a fact that is 1200
this trick yields us the solution similar to calories is the calorie limit for a good
the above solution said. healthy person per a time.

*********