Assignment of Design and Analysis of Algorism
Assignment of Design and Analysis of Algorism
The fractional knapsack problem is also one of the techniques which are used to solve the
knapsack problem. In fractional knapsack, the items are broken in order to maximize the profit.
The problem in which we break the item is known as a Fractional knapsack problem.
This problem can be solved with the help of using two techniques:
o Brute-force approach: The brute-force approach tries all the possible solutions with all
the different fractions but it is a time-consuming approach.
o Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and
accordingly, we will select the item. The item with the highest ratio would be selected
first.
The fractional Knapsack problem using the Greedy Method is an efficient method to solve it,
where you need to sort the items according to their ratio of value/weight. In a fractional
knapsack, we can break items to maximize the knapsack’s total value. This problem in which we
can break an item is also called the Fractional knapsack problem. Here, we will see Knapsack
Problem using Greedy method in detail, along with its algorithm and examples.
1| P a g e
according to the descending order of the ratios.
Select an object with highest p/w ratio and check whether its height is lesser than
the capacity of the bag.
If so place 1 unit of the first object and decrement. the capacity of the bag by the
weight of the object you have placed.
Repeat the above steps until the capacity of the bag becomes less than the weight
of the object you have selected .in this case place a fraction of the object and come
out of the loop.
Whenever you selected.
The Profits and Weights are positive.
ALGORITHM:
Algorityhm Greedy knapsack (m,n)
//P[1:n] and the w[1:n]contain the profit
& weight res’.of the n object ordered.
//such that p[i]/w[i] >=p[i+1]/W[i+1]
//n is the Knapsack size and x[1:n] is the solution vertex.
{
for i=1 to n do a[i]=0.0;
U=n;
for i=1 to n do
{
if (w[i]>u)then break;
x[i]=1.0;U=U-w[i]
}if(i<=n)then x[i]=U/w[i];
}
Example: find optimal solution according to fractional knapsack problem
Object 1 2 3 4 5 6 7
W (Weight of the
knapsack): 15
Profit 5 10 15 7 8 9 4
n (no of items): 7
Weight 1 3 5 4 1 3 2
2| P a g e
a. According to Maximum profit
Profit 5 10 15 7 8 9 4
Weight 1 3 5 4 1 3 2
In this approach, we will select the objects based on the maximum profit/weight ratio. Since the
P/W of object 5 is maximum so we select object 5.
3| P a g e
Object Profit Weight Remaining weight
5 8 1 15 - 8 = 7
After object 5, object 1 has the maximum profit/weight ratio, i.e., 5. So, we select object 1 shown
in the below table:
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
After object 1, object 2 has the maximum profit/weight ratio, i.e., 3.3. So, we select object 2
having profit/weight ratio as 3.3.
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
After object 2, object 3 has the maximum profit/weight ratio, i.e., 3. So, we select object 3
having profit/weight ratio as 3.
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
After object 3, object 6 has the maximum profit/weight ratio, i.e., 3. So we select object 6 having
profit/weight ratio as 3.
4| P a g e
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5-3=2
After object 6, object 7 has the maximum profit/weight ratio, i.e., 2. So we select object 7 having
profit/weight ratio as 2.
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5-3=2
7 4 2 2-2=0
According to ratio of profit/weight total profit is (8 + 5 + 10 + 15 + 9 + 4) =51
According to the the above fractional knapsack problem the best solution for profit is maximum
ratio of profit/ weight . this solution is optimal for the given problem instance. In the first approach,
the maximum profit is 47.25. The maximum profit in the second approach is 46. The maximum profit in
the third approach is 51. Therefore, we can say that the third approach, i.e., maximum profit/weight
ratio is the best approach among all the approaches.
5| P a g e