Open In App

Implementation of 0/1 Knapsack using Branch and Bound

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given two arrays v[] and w[] that represent values and weights associated with n items respectively. Find out the maximum value subset(Maximum Profit) of v[] such that the sum of the weights of this subset is smaller than or equal to Knapsack capacity Cap(W).

Note: The constraint here is we can either put an item completely into the bag or cannot put it at all. It is not possible to put a part of an item into the bag.

Example:

Input: N = 3, W = 4, v[] = {1, 2, 3}, w[] = {4, 5, 1}
Output: 3
Explanation: There are two items which have weight less than or equal to 4. If we select the item with weight 4, the possible profit is 1. And if we select the item with weight 1, the possible profit is 3. So the maximum possible profit is 3. Note that we cannot put both the items with weight 4 and 1 together as the capacity of the bag is 4.

Input: N = 3, W = 4, v[] = {2, 3.14, 1.98, 5, 3}, w[] = {40, 50, 100, 95, 30}
Output: 235

Implementation of 0/1 Knapsack using Branch and Bound:

We strongly recommend to refer below post as a prerequisite for this. Branch and Bound | Set 1 (Introduction with 0/1 Knapsack) We discussed different approaches to solve above problem and saw that the Branch and Bound solution is the best suited method when item weights are not integers. In this post implementation of Branch and Bound method for 0/1 knapsack problem is discussed. 

0-1-Knapsack-using-Branch-and-Bound3

Implementation of 0/1 Knapsack using Branch and Bound

How to find bound for every node for 0/1 Knapsack? 

The idea is to use the fact that the Greedy approach provides the best solution for Fractional Knapsack problem. To check if a particular node can give us a better solution or not, we compute the optimal solution (through the node) using Greedy approach. If the solution computed by Greedy approach itself is more than the best so far, then we can’t get a better solution through the node. 

Algorithm:

  • Sort all items in decreasing order of ratio of value per unit weight so that an upper bound can be computed using Greedy Approach.
  • Initialize maximum profit, maxProfit = 0
  • Create an empty queue, Q.
  • Create a dummy node of decision tree and enqueue it to Q. Profit and weight of dummy node are 0.
  • Do following while Q is not empty.
    • Extract an item from Q. Let the extracted item be u.
    • Compute profit of next level node. If the profit is more than maxProfit, then update maxProfit.
    • Compute bound of next level node. If bound is more than maxProfit, then add next level node to Q.
    • Consider the case when next level node is not considered as part of solution and add a node to queue with level as next, but weight and profit without considering next level nodes.

Following is implementation of above idea. 

Cpp




// C++ program to solve knapsack problem using
// branch and bound
#include <bits/stdc++.h>
using namespace std;
 
// Structure for Item which store weight and corresponding
// value of Item
struct Item
{
    float weight;
    int value;
};
 
// Node structure to store information of decision
// tree
struct Node
{
    // level --> Level of node in decision tree (or index
    //             in arr[]
    // profit --> Profit of nodes on path from root to this
    //         node (including this node)
    // bound ---> Upper bound of maximum profit in subtree
    //         of this node/
    int level, profit, bound;
    float weight;
};
 
// Comparison function to sort Item according to
// val/weight ratio
bool cmp(Item a, Item b)
{
    double r1 = (double)a.value / a.weight;
    double r2 = (double)b.value / b.weight;
    return r1 > r2;
}
 
// Returns bound of profit in subtree rooted with u.
// This function mainly uses Greedy solution to find
// an upper bound on maximum profit.
int bound(Node u, int n, int W, Item arr[])
{
    // if weight overcomes the knapsack capacity, return
    // 0 as expected bound
    if (u.weight >= W)
        return 0;
 
    // initialize bound on profit by current profit
    int profit_bound = u.profit;
 
    // start including items from index 1 more to current
    // item index
    int j = u.level + 1;
    int totweight = u.weight;
 
    // checking index condition and knapsack capacity
    // condition
    while ((j < n) && (totweight + arr[j].weight <= W))
    {
        totweight += arr[j].weight;
        profit_bound += arr[j].value;
        j++;
    }
 
    // If k is not n, include last item partially for
    // upper bound on profit
    if (j < n)
        profit_bound += (W - totweight) * arr[j].value /
                                        arr[j].weight;
 
    return profit_bound;
}
 
// Returns maximum profit we can get with capacity W
int knapsack(int W, Item arr[], int n)
{
    // sorting Item on basis of value per unit
    // weight.
    sort(arr, arr + n, cmp);
 
    // make a queue for traversing the node
    queue<Node> Q;
    Node u, v;
 
    // dummy node at starting
    u.level = -1;
    u.profit = u.weight = 0;
    Q.push(u);
 
    // One by one extract an item from decision tree
    // compute profit of all children of extracted item
    // and keep saving maxProfit
    int maxProfit = 0;
    while (!Q.empty())
    {
        // Dequeue a node
        u = Q.front();
        Q.pop();
 
        // If it is starting node, assign level 0
        if (u.level == -1)
            v.level = 0;
 
        // If there is nothing on next level
        if (u.level == n-1)
            continue;
 
        // Else if not last node, then increment level,
        // and compute profit of children nodes.
        v.level = u.level + 1;
 
        // Taking current level's item add current
        // level's weight and value to node u's
        // weight and value
        v.weight = u.weight + arr[v.level].weight;
        v.profit = u.profit + arr[v.level].value;
 
        // If cumulated weight is less than W and
        // profit is greater than previous profit,
        // update maxprofit
        if (v.weight <= W && v.profit > maxProfit)
            maxProfit = v.profit;
 
        // Get the upper bound on profit to decide
        // whether to add v to Q or not.
        v.bound = bound(v, n, W, arr);
 
        // If bound value is greater than profit,
        // then only push into queue for further
        // consideration
        if (v.bound > maxProfit)
            Q.push(v);
 
        // Do the same thing, but Without taking
        // the item in knapsack
        v.weight = u.weight;
        v.profit = u.profit;
        v.bound = bound(v, n, W, arr);
        if (v.bound > maxProfit)
            Q.push(v);
    }
 
    return maxProfit;
}
 
// driver program to test above function
int main()
{
    int W = 10; // Weight of knapsack
    Item arr[] = {{2, 40}, {3.14, 50}, {1.98, 100},
                {5, 95}, {3, 30}};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Maximum possible profit = "
        << knapsack(W, arr, n);
 
    return 0;
}


Java




import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
 
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) {
        int W = 10;
        Item[] arr = {
            new Item(2, 40),
            new Item(3.14f, 50),
            new Item(1.98f, 100),
            new Item(5, 95),
            new Item(3, 30)
        };
        int n = arr.length;
 
        int maxProfit = knapsack(W, arr, n);
        System.out.println("Maximum possible profit = " + maxProfit);
    }
}


Python




from Queue import Queue
 
# Define an Item class to represent
# each item in the knapsack
class Item:
    def __init__(self, weight, value):
        self.weight = weight
        self.value = value
 
# Define a Node class to represent each
# node in the branch and bound tree
class Node:
    def __init__(self, level, profit, bound, weight):
        self.level = level
        self.profit = profit
        self.bound = bound
        self.weight = weight
 
# Define a compare function to sort items
# by their value-to-weight ratio in
# descending order
def compare(a, b):
    r1 = float(a.value) / a.weight
    r2 = float(b.value) / b.weight
    return r1 > r2
 
# Define a function to calculate the
# maximum possible profit for a given node
def bound(u, n, W, arr):
    # If the node exceeds the knapsack's
    # capacity, its profit bound is 0
    if u.weight >= W:
        return 0
 
    # Calculate the profit bound by adding
    # the profits of all remaining items
    # that can fit into the knapsack
    profitBound = u.profit
    j = u.level + 1
    totWeight = int(u.weight)
 
    while j < n and totWeight + int(arr[j].weight) <= W:
        totWeight += int(arr[j].weight)
        profitBound += arr[j].value
        j += 1
 
    # If there are still items remaining,
    # add a fraction of the next item's
    # profit proportional to the remaining
    # space in the knapsack
    if j < n:
        profitBound += int((W - totWeight) * arr[j].value / arr[j].weight)
 
    return profitBound
 
# Define the knapsack_solution function
# that uses the Branch and Bound algorithm
# to solve the 0-1 Knapsack problem
def knapsack_solution(W, arr, n):
 
    # Sort the items in descending order
    # of their value-to-weight ratio
    arr.sort(cmp=compare, reverse=True)
 
    # Initialize a queue with a root node
    # of the branch and bound tree
    q = Queue()
    u = Node(-1, 0, 0, 0)
    q.put(u)
 
    # Initialize a variable to keep track
    # of the maximum profit found so far
    maxProfit = 0
 
    # Loop through each node in the
    # branch and bound tree
    while not q.empty():
        u = q.get()
 
        # If the node is the root node,
        # add its child nodes to the queue
        if u.level == -1:
            v = Node(0, 0, 0, 0)
 
        # If the node is a leaf node, skip it
        if u.level == n - 1:
            continue
 
        # Calculate the child node that
        # includes the next item in knapsack
        v = Node(u.level + 1, u.profit +
                 arr[u.level + 1].value, 0, u.weight + arr[u.level + 1].weight)
 
        # If the child node's weight is
        # less than or equal to the knapsack's
        # capacity and its profit is greater
        # than the maximum profit found so far,
        # update the maximum profit
        if v.weight <= W and v.profit > maxProfit:
            maxProfit = v.profit
 
        # Calculate the profit bound for the
        # child node and add it to the queue
        # if its profit bound is greater than
        # the maximum profit found so far
        v.bound = bound(v, n, W, arr)
 
        if v.bound > maxProfit:
            q.put(v)
 
        v = Node(u.level + 1, u.profit, 0, u.weight)
 
        v.bound = bound(v, n, W, arr)
 
        if v.bound > maxProfit:
            q.put(v)
 
    return maxProfit
 
 
# Driver Code
if __name__ == '__main__':
    W = 10
    arr = [Item(2, 40), Item(3.14, 50), Item(
        1.98, 100), Item(5, 95), Item(3, 30)]
    n = len(arr)
 
    print 'Maximum possible profit =', knapsack_solution(W, arr, n)


C#




using System;
using System.Collections.Generic;
 
public struct Item
{
    public float weight;
    public int value;
}
 
public struct Node
{
    public int level;
    public int profit;
    public int bound;
    public float weight;
}
 
public class Knapsack
{
    public static bool Compare(Item a, Item b)
    {
        double r1 = (double)a.value / a.weight;
        double r2 = (double)b.value / b.weight;
        return r1 > r2;
    }
 
    public 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;
        int totWeight = (int)u.weight;
 
        while (j < n && totWeight + (int)arr[j].weight <= W)
        {
            totWeight += (int)arr[j].weight;
            profitBound += arr[j].value;
            j++;
        }
 
        if (j < n)
            profitBound += (int)((W - totWeight) * arr[j].value / arr[j].weight);
 
        return profitBound;
    }
 
    public static int KnapsackSolution(int W, Item[] arr, int n)
    {
        Array.Sort<Item>(arr, Compare);
 
        Queue<Node> q = new Queue<Node>();
        Node u, v;
        int maxProfit = 0;
 
        u.level = -1;
        u.profit = 0;
        u.weight = 0;
        q.Enqueue(u);
 
        while (q.Count > 0)
        {
            u = q.Dequeue();
 
            if (u.level == -1)
                v.level = 0;
 
            if (u.level == n - 1)
                continue;
 
            v.level = u.level + 1;
            v.weight = u.weight + arr[v.level].weight;
            v.profit = u.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)
                q.Enqueue(v);
 
            v.weight = u.weight;
            v.profit = u.profit;
            v.bound = Bound(v, n, W, arr);
 
            if (v.bound > maxProfit)
                q.Enqueue(v);
        }
 
        return maxProfit;
    }
 
    public static void Main(string[] args)
    {
        int W = 10;
        Item[] arr = new Item[5] { new Item { weight = 2, value = 40 },
                                   new Item { weight = 3.14f, value = 50 },
                                   new Item { weight = 1.98f, value = 100 },
                                   new Item { weight = 5, value = 95 },
                                   new Item { weight = 3, value = 30 } };
        int n = arr.Length;
 
        Console.WriteLine("Maximum possible profit = {0}", KnapsackSolution(W, arr, n));
    }
}


Javascript




// JavaScript program to solve knapsack problem using
// branch and bound
 
// Structure for Item which store weight and corresponding value of Item
class Item {
    constructor(weight, value) {
        this.weight = weight;
        this.value = value;
    }
}
 
// Node structure to store information of decision tree
class Node {
    constructor(level, profit, weight, bound) {
        this.level = level; // Level of node in decision tree (or index in arr[])
        this.profit = profit; // Profit of nodes on path from root to this node (including this node)
        this.weight = weight; // Weight of nodes on path from root to this node (including this node)
        this.bound = bound; // Upper bound of maximum profit in subtree of this node
    }
}
 
// Comparison function to sort Item according to val/weight ratio
function cmp(a, b) {
    let r1 = a.value / a.weight;
    let r2 = b.value / b.weight;
    return r1 < r2;
}
 
// Returns bound of profit in subtree rooted with u.
// This function mainly uses Greedy solution to find
// an upper bound on maximum profit.
function bound(u, n, W, arr) {
    // if weight overcomes the knapsack capacity, return 0 as expected bound
    if (u.weight >= W) {
        return 0;
    }
 
    // initialize bound on profit by current profit
    let profit_bound = u.profit;
 
    // start including items from index 1 more to current item index
    let j = u.level + 1;
    let totweight = u.weight;
 
    // checking index condition and knapsack capacity condition
    while (j < n && totweight + arr[j].weight <= W) {
        totweight += arr[j].weight;
        profit_bound += arr[j].value;
        j++;
    }
 
    // If k is not n, include last item partially for upper bound on profit
    if (j < n) {
        profit_bound += (W - totweight) * arr[j].value / arr[j].weight;
    }
 
    return profit_bound;
}
 
// Returns maximum profit we can get with capacity W
function knapsack(W, arr, n) {
    // sorting Item on basis of value per unit weight.
    arr.sort(cmp);
 
    // make a queue for traversing the node
    let Q = [];
    let u = new Node(-1, 0, 0, 0);
    let v;
 
    // dummy node at starting
    Q.push(u);
 
    // One by one extract an item from decision tree
    // compute profit of all children of extracted item
    // and keep saving maxProfit
    let maxProfit = 0;
    while (Q.length > 0) {
        // Dequeue a node
        u = Q.shift();
 
        // If it is starting node, assign level 0
        if (u.level == -1) {
            v = new Node(0, 0, 0, 0);
        }
 
        // If there is nothing on next level
        if (u.level == n - 1) {
            continue;
        }
 
        // Else if not last node, then increment level,
        // and compute profit of children nodes.
        v = new Node(u.level + 1, u.profit, u.weight, 0);
 
        // Taking current level's item add current
        // level's weight
        v.weight = u.weight + arr[v.level].weight;
        v.profit = u.profit + arr[v.level].value;
            // If cumulated weight is less than W and
    // profit is greater than previous profit,
    // update maxprofit
    if (v.weight <= W && v.profit > maxProfit) {
        maxProfit = v.profit;
    }
 
    // Get the upper bound on profit to decide
    // whether to add v to Q or not.
    v.bound = bound(v, n, W, arr);
 
    // If bound value is greater than profit,
    // then only push into queue for further
    // consideration
    if (v.bound > maxProfit) {
        Q.push(v);
    }
 
    // Do the same thing, but Without taking
    // the item in knapsack
    v = new Node(u.level + 1, u.profit, u.weight, 0);
    v.bound = bound(v, n, W, arr);
    if (v.bound > maxProfit) {
        Q.push(v);
    }
}
 
return maxProfit;
}
 
// driver program to test above function
function main() {
const W = 10; // Weight of knapsack
const arr = [
{ weight: 2, value: 40 },
{ weight: 3.14, value: 50 },
{ weight: 1.98, value: 100 },
{ weight: 5, value: 95 },
{ weight: 3, value: 30 },
];
const n = arr.length;
 
console.log(`Maximum possible profit = ${knapsack(W, arr, n)}`);
}
main();


Output

Maximum possible profit = 235

Time complexity: O(2n) because the while loop runs n times, and inside the while loop, there is another loop that also runs n times in the worst case.
Auxiliary Space: O(n) because the queue stores the nodes, and in the worst case, all nodes are stored, so the size of the queue is proportional to the number of items, which is n.



Last Updated : 23 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads