AOA-Lab_Exp3
AOA-Lab_Exp3
/Div: 60/D6AD
EXPERIMENT-3
AIM:
Write a C program to implement Fractional Knapsack Problem
THEORY:
Fractional Knapsack is an algorithmic problem in combinational optimization in which the goal is to fill a
container-i.e., knapsack with fractional amounts of different materials chosen to maximize the value of the
selected materials.
Brute-force approach: The brute-force approach tries all the possible solutions with all the different fractions
but it is a time-consuming approach.
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.
Complexity: Time Complexity: O(N*logN) here, N= size of the array
Space Complexity: O(1)
ALGORITHM:
Greedy Knapsack(m, n)
// p[1: n] and w[1: n] contain the profits and weights respectively
// of the n objects ordered such that p[i]/w[i] ≥ pli+1]/w[i+1]
//m is the knapsack size and z[1:n) is the solution vector.
{
for i:=1 to n do r[i]=0.0; // Initialize x.
U := m;
for i:=1 to n do
{
if (w[i] > U) then break;
x[i] = 1.0; U:- U - w[i];
}
if (i <n) then r[i]:= U/w[i];
}
PROGRAM CODE:
OUTPUT:
CONCLUSION:
Thus, we have implemented Fractional Knapsack which is a greedy algorithm used in order to fill the
knapsack of the given weight with different things to maximize the profit.