- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

Illustrations 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

- Related Questions & Answers
- 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
- Difference between while(1) and while(0) in C/C++
- Upper bound and Lower bound for non increasing vector in C++
- Lower bound in C++
- Forming string using 0 and 1 in JavaScript
- Difference between while(1) and while(0) in C language
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- Convert RE 1(0+1)*0 into equivalent DFA.
- Sort an arrays of 0’s, 1’s and 2’s using C++
- Implicit initialization of variables with 0 or 1 in C
- Difference Between exit(0) and exit(1)

Advertisements