0% found this document useful (0 votes)
57 views

Knapsack Algorithm Java Implementation

This Java code implements the greedy algorithm to solve the knapsack problem. It defines a KnapsackObject class to store item weights, profits, and profit-to-weight ratios. It reads in knapsack capacity and item data, sorts items by profit-to-weight ratio, and fills the knapsack greedily by taking highest ratio items until it is full, tracking total profit. It outputs the solution vector showing items taken and the total profit.

Uploaded by

Akash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Knapsack Algorithm Java Implementation

This Java code implements the greedy algorithm to solve the knapsack problem. It defines a KnapsackObject class to store item weights, profits, and profit-to-weight ratios. It reads in knapsack capacity and item data, sorts items by profit-to-weight ratio, and fills the knapsack greedily by taking highest ratio items until it is full, tracking total profit. It outputs the solution vector showing items taken and the total profit.

Uploaded by

Akash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DAA I.

eh Man11ol
/
(h) G,·udy Method j
import java.util Scanner
class KObject '
{ // Knapsllck oh_ject details
flnst w,
float p,
float r,

public class KnapsackGrcedv2


{ .
static tinftl int MAX ,., , 20, II mnx no of objects
static int n. II 110 . of objects
static float M; // capacity of Knapsack
public static void main(String args[])
{
Scanner scanner= new Scanner(System.in);
System.out println("Enter number of objects: ");
n = scanner.nextlnt( );
KObject[] obj= new KObject[n] ;
for(int i = 0; i<n;i++)
obj[i] = new KObject( );// allocate memory for members
Read Objects(obj);
Knapsack( obj);
scanner.close();
}
static void ReadObjects(KObject obj[ ])
{
KObject temp = new KObject( );
Scanner scanner= new Scanner(System.in);
System.out.println("Enter the max capacity of knapsack: ");
M = scanner.nextFloat( );
System.out.println("Enter Weights: ");
for (inti= 0; i < n; i++)
obj[i].w = scanner.nextFloat( );
System.out.println("Enter Profits: ");
for (inti= 0; i < n; i++)
obj[i].p = scanner.nextFloat( );
for (inti= O; i < n; i++)
obj[i].r = obj[i].p / obj[i].w;
II sort objects in descending order, based on p/w ratio
for(int i = 0; i<n-1; i++)
for(int j=0; j<n-1-i; j++)
ift obj[j].r < obj[j+ 1].r)
{
temp = obj Li];

Dept of CSE, BIT 31


(J AA Lnb Manu al

obj(j] :-::: obj(j + I J;


objlj + l} = temp ;
}
scanner.close( ):
}
~tatic void Knapsack(KObject kobj[])

t1oat x[ ] == new float[MAX];


float totalprofit;
int i;
float U ; II U place holder for M
U==M·,
totalprofit = o·
for (i = O; i < ~; i++)
x[i] = O;
for (i = O; i < n; i++)
{
if (kobj[i].w > U)
break;
else
{
x[i] = l ;
totalprofit = totalprofit + kobj[i].p ;
U = U - kobj[i].w;
}
}
System.out.println( i = + i) ;
11 11

if (i < n)
{
x[i] = U / kobj[i].w;
totalprofit = totalprofit + (x[i] * kobj[i].p);
}
System.out.println( The Solution vector, x[]:
11 11
) ;

for (i = O; i < n; i++)


System.out.print(x[i] + 11 11
);
11
System.out.println( \nTotal profit is= "+ totalprofit);
}
}

Dept of CSE, BIT 32

You might also like