- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 0/1 Knapsack using Branch and Bound in C/C++?

The idea is to implement the fact that the Greedy approach provides the best solution for Fractional Knapsack problem.

To check whether a particular node can give us a better solution or not, we calculate the optimal solution (through the node) implementing Greedy approach. If the solution calculated by Greedy approach itself is more than the best so far, then we can’t obtain a better solution through the node.

Complete Algorithm is given below −

Sort all items according to decreasing order of ratio of value per unit weight so that an upper bound can becalculated implementing Greedy Approach.

Initialize maximum profit, such as maxProfit = 0

An empty queue, Q, is created.

A dummy node of decision tree is created and insert or enqueue it to Q. Profit and weight of dummy node be 0.

Do following while Q is not vacant or empty.

An item from Q is created. Let the extracted item be u.

Calculate profit of next level node. If the profit is higher than maxProfit, then modify maxProfit.

Calculate bound of next level node. If bound is higher than maxProfit, then add next level node to Q.

Consider the case when next level node is not treated or considered as part of solution and add a node to queue with level as next, but weight and profit without treating or considering next level nodes.

Illustrations given below −

## Input

// First thing in every pair is treated as weight of item // and second thing is treated as value of item Item arr1[] = {{2, 40}, {3.14, 50}, {1.98, 100}, {5, 95}, {3, 30}}; Knapsack Capacity W1 = 10

## Output

The maximum possible profit = 235

- Related Articles
- 0/1 Knapsack using Branch and Bound in C++
- 0-1 Knapsack Problem in C?
- Printing Items in 0/1 Knapsack in C++
- C++ Program to Solve the 0-1 Knapsack Problem
- Python Program for 0-1 Knapsack Problem
- Upper bound and Lower bound for non increasing vector in C++
- Lower bound in C++
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- Difference between while(1) and while(0) in C/C++
- Sort an arrays of 0’s, 1’s and 2’s using C++
- Difference between while(1) and while(0) in C language
- C++ Program to Solve the Fractional Knapsack Problem
- Find the Pattern of 1’s inside 0’s using C++
- Forming string using 0 and 1 in JavaScript
- Count the number of 1’s and 0’s in a binary array using STL in C++