K. J. Somaiya College of Engineering

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

K. J.

Somaiya College of Engineering


(A Constituent College of Somaiya Vidyavihar University)

Batch: B3 Roll No.: 16010120126

Experiment No.6

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

Title: Implementation of Knapsack Problem using Greedy strategy

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.

Books/ Journals/ Websites referred:


1. Ellis horowitz, Sarataj Sahni, S.Rajsekaran,” Fundamentals of computer
algorithm”, University Press
2. T.H.Cormen ,C.E.Leiserson,R.L.Rivest and C.Stein,” Introduction to
algortihtms”,2nd Edition ,MIT press/McGraw Hill,2001
3. http://lcm.csa.iisc.ernet.in/dsa/node184.htm
4. http://students.ceid.upatras.gr/~papagel/project/kruskal.htm
5. http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/
GraphAlgor/kruskalAlgor.html
6. http://lcm.csa.iisc.ernet.in/dsa/node183.html
7. http://students.ceid.upatras.gr/~papagel/project/prim.htm
8. http://www.cse.ust.hk/~dekai/271/notes/L07/L07.pdf

Pre Lab/ Prior Concepts:


Data structures, Concepts of algorithm analysis
K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

Historical Profile:

The knapsack problem represents constraint satisfaction optimization problems’ family.


Based on nature of constraints, the knapsack problem can be solved with various problem
saolving strategies. Typically, these problems represent resource optimization solution.

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.

New Concepts to be learned:


Application of algorithmic design strategy to any problem, Greedy method of problem
solving Vs other methods of problem solving, optimality of the solution, knapsack problem
and their applications

Knapsack Problem Algorithm


K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

Example: Knapsack Problem

Analysis of Knapsack Problem algorithm:


K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

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

– If a fraction xi of object i placed into knapsack, a profit p ixi is earned

The objective is to obtain a filling of knapsack maximizing the total profit

• Problem formulation (Formula 4.1-4.3)

• A feasible solution is any set satisfying (4.2) and (4.3)

• An optimal solution is a feasible solution for which (4.1) is maximized

· Greedy selection policy: three natural possibilities

· 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

• Greedy Knapsack: O(n)

• So, total time is O(n log n)

Code for Knapsack


K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)

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.

You might also like