Professional Documents
Culture Documents
K. J. Somaiya College of Engineering
K. J. Somaiya College of Engineering
K. J. Somaiya College of Engineering
Experiment No.6
Grade: AA / AB / BB / BC / CC / CD /DD
Objective: To learn the Greedy strategy of solving the problems for different types of
problems
CO to be achieved:
CO 2 Describe various algorithm design strategies to solve different problems and analyze
Complexity.
Historical Profile:
Given a set of n inputs. · Find a subset, called feasible solution, of the n inputs subject to
some constraints, and satisfying a given objective function. · If the objective function is
maximized or minimized, the feasible solution is optimal. · It is a locally optimal method.
The basic idea of the greedy approach is to calculate the ratio Profit/weight for each item and sort the
item on basis of this ratio. Then take the item with the highest ratio and add them until we can’t add
the next item as a whole and at the end add the next item as much as we can. Which will always be
the optimal solution to this problem.
Problem definition
– Given n objects and a knapsack where object i has a weight w i and the knapsack has a capacity m
· Policy 1: Choose the lightest remaining item and take as much of it as can fit.
· Policy 2: Choose the most profitable remaining item and take as much of it as can fit.
· Policy 3: Choose the item with the highest price per unit weight (P[i]/W[i]) and take as much of it as
can fit. Policy 3 always gives an optimal solution.
Time complexity
• Sorting: O(n log n) using fast sorting algorithm like merge sort
import java.util.*;
public class KnapSack
{
double sortedArr[];
public void sort(double inputArr[],int lb,int ub)
{
if(lb<ub)
{
int mid=(lb+ub)/2;
sort(inputArr,lb,mid);
sort(inputArr,mid+1,ub);
merge(inputArr,lb,mid,ub);
}
}
public void merge(double inputArr[],int lb,int mid,int ub)
{
int i=lb;
int j=mid+1;
int k=lb;
while(i<=mid && j<=ub)
{
if(inputArr[i]<=inputArr[j])
{
sortedArr[k]=inputArr[i];
i++;k++;
}
else
{
sortedArr[k]=inputArr[j];
j++;k++;
}
}
while(j<=ub)
{
sortedArr[k]=inputArr[j];
k++;j++;
}
while(i<=mid)
{
sortedArr[k]=inputArr[i];
k++;i++;
}
for(i=lb; i<=ub; i++)
{
inputArr[i]=sortedArr[i];
}
}
public static void main(String[] args) {
KnapSack obj=new KnapSack();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n=sc.nextInt();
obj.sortedArr=new double[n];
System.out.println("Enter the bag capacity: ");
K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)
int b=sc.nextInt();
int p[]=new int[n];
int w[]=new int[n];
System.out.println("Enter the values of profit: ");
for(int i=0;i<n;i++)
p[i]=sc.nextInt();
System.out.println("Enter the values of weight: ");
for(int i=0;i<n;i++)
w[i]=sc.nextInt();
double pw[]=new double[n];
for(int i=0;i<n;i++)
pw[i]=p[i] / w[i];
obj.sort(pw,0,(n)-1);
double profit=0.0;
while(b>0)
{
double temp=obj.sortedArr[0];
int max=0;
for(int j=0;j<n;j++)
{
if(temp<obj.sortedArr[j]){
max=j;
temp=obj.sortedArr[j];
}
}
obj.sortedArr[max]=-1;
if(b>=w[max])
{
profit+=p[max];
b-=w[max];
}
else{
double temp2 =(double)w[max];
profit+=(p[max]*b) / temp2;
b=0;
}
}
System.out.println("Maximum profit = " + profit);
}
}
Conclusion:
In this experiment we have understood the greedy approach to solve knapsack problems .We
have also understood the algorithm and implemented the same on java .We have compared
the time complexity of the program for sorted and unsorted array .Additionally we have
understood the applications of knapsack problem in real life scenarios.