0% found this document useful (0 votes)
7 views3 pages

22bce7873 Asg6

The document presents a Java program that solves the Knapsack problem by calculating the optimal solution based on item profits and weights. It allows user input for the number of items and their respective profit and weight values, then outputs the solution and maximum profit. The example provided demonstrates the program's functionality with three items and a knapsack capacity of 50.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

22bce7873 Asg6

The document presents a Java program that solves the Knapsack problem by calculating the optimal solution based on item profits and weights. It allows user input for the number of items and their respective profit and weight values, then outputs the solution and maximum profit. The example provided demonstrates the program's functionality with three items and a knapsack capacity of 50.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

NAME VEMURI PRAVEENA

REGISTRATION NUMBER 22BCE7873


LAB SLOT L51+52
ASSIGNMENT NUMBER 06

>> Java program to display optimal solution of Knapsack problem.


CODE:

import java.util.*;

class Item {

int weight, profit;

double ratio;

Item(int profit, int weight) {

this.profit = profit;

this.weight = weight;

this.ratio = (double) profit / weight;

public class Knapsack {

static void knapsack(int n, int m, int[] p, int[] w) {

Item[] items = new Item[n];

for (int i = 0; i < n; i++)

items[i] = new Item(p[i], w[i]);

Arrays.sort(items, (a, b) -> Double.compare(b.ratio, a.ratio));

double[] x = new double[n];


double profit = 0;
for (int i = 0; i < n; i++) {

if (items[i].weight > m) {

x[i] = (double) m / items[i].weight;


profit += items[i].profit * x[i];

break;

} else {

x[i] = 1;

profit += items[i].profit;

m -= items[i].weight;

}
}

System.out.println("Solution: " + Arrays.toString(x));

System.out.println("Maximum profit: " + profit);

public static void main(String args[]) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter number of items: ");

int n = scanner.nextInt();

int[] p = new int[n];

int[] w = new int[n];

System.out.print("Enter knapsack capacity: ");

int m = scanner.nextInt();

for (int i = 0; i < n; i++) {


System.out.print("Enter profit and weight of item " + (i + 1) + ": ");
p[i] = scanner.nextInt();

w[i] = scanner.nextInt();

knapsack(n, m, p, w);

scanner.close();

OUTPUT:

Enter number of items: 3

Enter knapsack capacity: 50


Enter profit and weight of item 1: 60 10
Enter profit and weight of item 2: 100 20

Enter profit and weight of item 3: 120 30

Solution: [1.0, 1.0, 0.6666666666666666]

Maximum profit: 240.0

-------------------------------------------------END------------------------------------------------------

You might also like