- Related Questions & Answers
- 0/1 Knapsack using Branch and Bound in C/C++?
- 0-1 Knapsack Problem in C?
- Printing Items in 0/1 Knapsack in C++
- Python Program for 0-1 Knapsack Problem
- C++ Program to Solve the 0-1 Knapsack Problem
- Forming string using 0 and 1 in JavaScript
- Upper bound and Lower bound for non increasing vector in C++
- Difference between while(1) and while(0) in C/C++
- Difference between while(1) and while(0) in C language
- Lower bound in C++
- Difference Between exit(0) and exit(1)
- Contiguous subarray with 0 and 1 in JavaScript
- Sort an arrays of 0’s, 1’s and 2’s using C++
- Segregate 0’s and 1’s in an array list using Python?
- C++ Program to Solve Knapsack Problem Using Dynamic Programming

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

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.

Illustrationis given below −

// 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

The maximum possible profit = 235

Advertisements