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

12 DRR

The document presents a Java implementation of the 0-1 Knapsack problem using the Branch and Bound method. It defines a class 'KnapsackBB' with methods to calculate the maximum profit and the number of nodes processed. The example provided demonstrates the algorithm with specific values, resulting in a maximum profit of 220 and processing 7 nodes.

Uploaded by

arpiarpi1663
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)
28 views3 pages

12 DRR

The document presents a Java implementation of the 0-1 Knapsack problem using the Branch and Bound method. It defines a class 'KnapsackBB' with methods to calculate the maximum profit and the number of nodes processed. The example provided demonstrates the algorithm with specific values, resulting in a maximum profit of 220 and processing 7 nodes.

Uploaded by

arpiarpi1663
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-Vikash kumar Roll-11232947 Sec on-4D3

Program-12

Aim:- Implementa on of solu on of 0-1 Knapsack problem using Branch and Bound Method.

import java.u l.*;

class KnapsackBB {

sta c int count = 0;

sta c class Node {

int level, profit, weight;

double bound;

Node(int level, int profit, int weight, double bound) {

this.level = level;

this.profit = profit;

this.weight = weight;

this.bound = bound;

sta c double bound(Node u, int n, int W, int[] wt, int[] val) {

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

double profitBound = u.profit;

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

while (j < n && totalWeight + wt[j] <= W) {

totalWeight += wt[j];

profitBound += val[j];

j++;

if (j < n)

profitBound += (W - totalWeight) * ((double) val[j] / wt[j]);

return profitBound;

sta c int knapsack(int W, int[] wt, int[] val, int n) {

Arrays.sort(wt);

Queue<Node> pq = new PriorityQueue<>(Comparator.comparingDouble(o -> -o.bound));


Name-Vikash kumar Roll-11232947 Sec on-4D3

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

pq.add(u);

int maxProfit = 0;

while (!pq.isEmpty()) {

u = pq.poll();

count++;

if (u.level == n - 1) con nue;

Node v = new Node(u.level + 1, u.profit + val[u.level + 1], u.weight + wt[u.level + 1], 0);

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

maxProfit = v.profit;

v.bound = bound(v, n, W, wt, val);

if (v.bound > maxProfit)

pq.add(v);

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

v.bound = bound(v, n, W, wt, val);

if (v.bound > maxProfit)

pq.add(v);

return maxProfit;

public sta c void main(String[] args) {

int[] val = {60, 100, 120};

int[] wt = {10, 20, 30};

int W = 50;

int n = val.length;

int maxProfit = knapsack(W, wt, val, n);

System.out.println("Maximum Profit: " + maxProfit);

System.out.println("Total nodes processed: " + count);

}
Name-Vikash kumar Roll-11232947 Sec on-4D3

Output:-
Maximum Profit: 220
Total nodes processed: 7

Graph:-

You might also like