You are on page 1of 6

Greedy approach builds the solution to the Problem in the Steps.

At each iteration it adds a part of the solution.

Problem:
Given a Knapsack(a Small Bag that can be carried on a shoulder) with
the limited Weight Capacity ’W’ .
There are ‘n’ items available having weights ‘w’ and with each item
certain ‘Profit or Value’ is associated.

Objective: To fill the Knapsack with those items such that we have
maximum Profit and the selected items weight ‘w’ should not exceed
the Knapsack Capacity Weight ‘W’.

Item_Weight Profit or Value


3 100
2 20
4 60
1 40

Or THE OTHER WAY THE PROBLEM CAN BE DEFINE AS:


Thief enters in the House with the purpose of robbing. He can carry
maximum weight of 5 kg in his bag.There are 4 items in the house with
the following weight and value
Item_Name Item_Weight(Kg) Profit or Value
Mobile Phone 2 25000/-
Gold Platted Clock 3 1,50,000/-
Silver Plated Flower Vass 4 15000/-
Necklace 5 10000/-

5 kg Limit of Bag
Objective: Maximum Profit and the weight of the items should not
exceed the bag capacity.
Option I :If he goes for the Necklace: weight of necklace is 5 equal to bag
capacity and the Profit he earns is Rs 10000/-
Option 2: But if he goes for Mobile Phone and Gold Platted Clock the
weight of the items is equal to the bag capacity and the Profit
he earns is Rs 1,75,000/-
So He goes for option 2 Maximum Profit with in the Bag Capacity.

There are two variants of the Knapsack problem


A) 0/1 Knapsack
B) Fractional Knapsack

0/1 Knapsack Fractional Knapsack


Dynamic Programming approach Greedy approach is used to
is used to solve the problem solve the Problem.
Items are indivisible i.e you can take Items are divisible i.e you can
the complete item or you cannot take fraction of the item
take it
.Fractional KnapSack Problem: In this Greedy approach is used to solve
the Problem.Items are divisible i.e you can take fraction of the item.

Steps to solve the Problem


A) Calculate the ratio of Value(Profit) per Weight for each item.
B) Sort the Items in the Descending Order on the basis of above
calculated ratio.
C) Start Filling the Knapsack with items arranged in above sorted order.
D) Take as much Item as possible not already taken in the Knapsack.
Example:
Let us consider that the capacity of the knapsack’ W ’= 60 kg and the list
of provided items are shown in the following table −
Item Weight Profit
A 40 280
B 10 100
C 20 120
D 24 120

1) Calculate the ratio Profit(Value)per Weight for each item.

Item Weight Profit/Value Ratio =Profit/Weight


A 40 280 7
B 10 100 10
C 20 120 6
D 24 120 5

2) Sort the Items in the Descending Order on the basis of above


calculated ratio.
Item Weight Profit/Value Ratio =Value/Weight
B 10 100 10
A 40 280 7
C 20 120 6
D 24 120 5
3) Start Filling the Knapsack with items arranged in above sorted order

Capacity of the Knapsack-Weight of the Item Item Added Profit in


added Knapsack
60 -0 = 60 0 0
60 - 10 = 50 B 100
50-40 = 10 B,A 100 + 280 = 380
10 -( Fraction of C * Weight of Added Item) B,A,C
10 -(1/2 * 20) = 10 - 10 =0

Now here the Remaining capacity of the Knapsack after adding item B
and item A is 10.
Next Item to be added according to the sorted order in the Knapsack is ‘C’
having weight 20 with Profit 120.

The Item Weight is greater than the remaining Capacity of the Knapsack
so we cannot take the C as a complete item but we consider only the
fraction of ‘C’ .

Question: How much part or Fraction of C should be considered for


adding in the Knapsack such that the profit will be maximum with in
limit of the Knapsack Capacity.

Fraction of an Item can be calculated as:


Remaining Capacity of the Knapsack/ Weight of the chosen Item
= 10/20= 1/2

Profit in the Knapsack = 100 + 280 + Profit of C * Fraction of C


= 100 +280 + 120 * (1/2)
= 100 +280 +60 = 440
Total Weight of the Selected Items = 10 + 40 + 20 (1/2) = 60

You might also like