|
| 1 | +# Fractional Knapsack Problem using Brute Force Method |
| 2 | +Language used : **Python 3** |
| 3 | + |
| 4 | +## 🎯 Aim |
| 5 | +The aim of this script is to find out the maximum total value in the knapsack. |
| 6 | + |
| 7 | +## 👉 Purpose |
| 8 | +The main purpose of this script is to show the implementation of Brute Force Method to find out the maximum total value in the knapsack. |
| 9 | + |
| 10 | +## 📄 Description |
| 11 | +Given weights and values of n items, we need to put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In the 0-1 Knapsack problem, we are not allowed to break items. We either take the whole item or don’t take it. |
| 12 | +``` |
| 13 | +Input: |
| 14 | +Items as (value, weight) pairs |
| 15 | +arr[] = {{60, 10}, {100, 20}, {120, 30}} |
| 16 | +Knapsack Capacity, W = 50; |
| 17 | +
|
| 18 | +Output: |
| 19 | +Maximum possible value = 240 |
| 20 | +by taking items of weight 10 and 20 kg and 2/3 fraction |
| 21 | +of 30 kg. Hence total price will be 60+100+(2/3)(120) = 240 |
| 22 | +``` |
| 23 | + |
| 24 | +## 📈 Workflow of the script |
| 25 | +- `ItemValue` - This function returns the item values of the data class. |
| 26 | +- `FractionalKnapSack` - This function is the main architecture to solve the program. |
| 27 | +- `__main__` - This is the driver code of the script. |
| 28 | + |
| 29 | +## 📃 Explanation |
| 30 | +In Fractional Knapsack, we can break items for maximizing the total value of knapsack. This problem in which we can break an item is also called the fractional knapsack problem. |
| 31 | +``` |
| 32 | +Input : Same as above |
| 33 | +Output : Maximum possible value = 240 By taking full items of 10 kg, 20 kg and 2/3rd of last item of 30 kg |
| 34 | +``` |
| 35 | +A brute-force solution would be to try all possible subset with all different fraction but that will be too much time taking. |
| 36 | + |
| 37 | +## 🧮 Algorithm |
| 38 | +- An efficient solution is to use Greedy approach. The basic idea of the greedy approach is to calculate the ratio value/weight for each item and sort the item on basis of this ratio. |
| 39 | +- Then take the item with the highest ratio and add them until we can’t add the next item as a whole and at the end add the next item as much as we can. Which will always be the optimal solution to this problem. |
| 40 | +- A simple code with our own comparison function can be written as follows, please see sort function more closely, the third argument to sort function is our comparison function which sorts the item according to value/weight ratio in non-decreasing order. |
| 41 | +- After sorting we need to loop over these items and add them in our knapsack satisfying above-mentioned criteria. |
| 42 | + |
| 43 | +## 💻 Input and Output |
| 44 | +``` |
| 45 | +- Fractional Knapsack Problem using Brute Force Method - |
| 46 | +----------------------------------------------------- |
| 47 | +
|
| 48 | +The values given : |
| 49 | +[60, 40, 100, 120] |
| 50 | +----------------------------------------------------- |
| 51 | +The corresponding weights are : |
| 52 | +[10, 40, 20, 30] |
| 53 | +----------------------------------------------------- |
| 54 | +The maximum capacity can be : |
| 55 | +50 |
| 56 | +
|
| 57 | +----------------------------------------------------- |
| 58 | +
|
| 59 | +Output : |
| 60 | +Maximum value in Knapsack = 240.0 |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## ⏰ Time and Space complexity |
| 67 | +- **Time Complexity:** `O(n log n)`. |
| 68 | + |
| 69 | +--------------------------------------------------------------- |
| 70 | +## 🖋️ Author |
| 71 | +**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)** |
| 72 | + |
| 73 | +[](https://www.python.org/) |
0 commit comments