Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Fractional Knapsack Problem

(Greedy approach)

Problem:
A person urgently need 40kg of rice. He has only one shop to buy from. There are four
qualities of rice at the shop: q1, q2, q3 and q4. The quantities in the stock along with their
total prices are 5kg (PKR 600), 10kg (PKR 1300), 15kg (PKR 1500), and 20kg (PKR 2100)
respectively. The person prefers low-cost over quality. Give a solution, i.e., the person buys
best possible combination of rice, cost-wise.

Solution
Given Capacity C= 40kg n=4

Objects Price Weight Remaining


Pi/Wi Xi
Oi Pi Wi weight
q1 600 5 120 5/5=1 30-5=25
q2 1300 10 130 10/10=1 40-10=30
q3 1500 15 100 5/15=0.3333 5-5=0
q4 2100 20 105 20/20=1 25-20=5

Xi will be calculate by greedy approach by selecting first maximum of 130,


the weight against 130 is 10 and if the weight Wi ≤C then divide by self as
10/10=1. Now in the remaining column of weight the weight is 40 – 10 = 30
If the weight Wi not ≤C the divide the remaining weight by given weight as
5/15= 0.3333.
Selection of maximum will be as 130 then 120 then 105 and lastly 100.

Now total weight = ∑ PiWi = (Pi/Wi).Xi= 1*5+1*10+0.3333*15+1*20


= 5+10+4.9995+20=39.9995 ≈ 40kg
Total price= ∑ XiWi=(Xi/Wi).Xi = 1*600+1*1300+0.3333*1500+1*2100
= 600+1300+499.95+2100= 4499.95 ≈ 4500
If you have confusion email me at ms160401398@vu.edu.pk

You might also like