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

DAA4

Code4

Uploaded by

Siddhesh Asati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DAA4

Code4

Uploaded by

Siddhesh Asati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Input:

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

class Item {
float weight;
int value;

Item(float weight, int value) {


this.weight = weight;
this.value = value;
}
}

class Node {
int level, profit, bound;
float weight;

Node(int level, int profit, float weight) {


this.level = level;
this.profit = profit;
this.weight = weight;
}
}

public class KnapsackBranchAndBound {


static Comparator<Item> itemComparator = (a, b) -> {
double ratio1 = (double) a.value / a.weight;
double ratio2 = (double) b.value / b.weight;
// Sorting in decreasing order of value per unit weight
return Double.compare(ratio2, ratio1);
};

static int bound(Node u, int n, int W, Item[] arr) {


if (u.weight >= W)
return 0;

int profitBound = u.profit;


int j = u.level + 1;
float totalWeight = u.weight;

while (j < n && totalWeight + arr[j].weight <= W) {


totalWeight += arr[j].weight;
profitBound += arr[j].value;
j++;
}

if (j < n)
profitBound += (int) ((W - totalWeight) * arr[j].value / arr[j].weight);
return profitBound;
}

static int knapsack(int W, Item[] arr, int n) {


Arrays.sort(arr, itemComparator);
PriorityQueue<Node> priorityQueue =
new PriorityQueue<>((a, b) -> Integer.compare(b.bound, a.bound));
Node u, v;

u = new Node(-1, 0, 0);


priorityQueue.offer(u);

int maxProfit = 0;

while (!priorityQueue.isEmpty()) {
u = priorityQueue.poll();

if (u.level == -1)
v = new Node(0, 0, 0);
else if (u.level == n - 1)
continue;
else
v = new Node(u.level + 1, u.profit, u.weight);

v.weight += arr[v.level].weight;
v.profit += arr[v.level].value;

if (v.weight <= W && v.profit > maxProfit)


maxProfit = v.profit;

v.bound = bound(v, n, W, arr);

if (v.bound > maxProfit)


priorityQueue.offer(v);

v = new Node(u.level + 1, u.profit, u.weight);


v.bound = bound(v, n, W, arr);

if (v.bound > maxProfit)


priorityQueue.offer(v);
}

return maxProfit;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

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


int n = scanner.nextInt();
System.out.print("Enter the weight limit: ");
int W = scanner.nextInt();

Item[] arr = new Item[n];

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


System.out.printf("Enter weight and value for item %d: ", i + 1);
float weight = scanner.nextFloat();
int value = scanner.nextInt();
arr[i] = new Item(weight, value);
}

int maxProfit = knapsack(W, arr, n);


System.out.println("Maximum possible profit = " + maxProfit);

scanner.close();
}
}
Output :

student@student:~$ javac KnapsackBranchAndBound.java


student@student:~$ java KnapsackBranchAndBound
Enter the number of items: 5
Enter the weight limit: 10
Enter weight and value for item 1: 2 40
Enter weight and value for item 2: 3.14 50
Enter weight and value for item 3: 1.98 100
Enter weight and value for item 4: 5 95
Enter weight and value for item 5: 3 30
Maximum possible profit = 235
student@student:~$

You might also like