From 565baeb3eedb0f11747de60be15cbe9fa31eb3e1 Mon Sep 17 00:00:00 2001 From: Swastika Gupta Date: Fri, 12 Nov 2021 07:37:16 +0000 Subject: [PATCH 1/4] issue 110 --- en/Greedy Algorithms/Fractional Knapsack.md | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 en/Greedy Algorithms/Fractional Knapsack.md diff --git a/en/Greedy Algorithms/Fractional Knapsack.md b/en/Greedy Algorithms/Fractional Knapsack.md new file mode 100644 index 00000000..753bd263 --- /dev/null +++ b/en/Greedy Algorithms/Fractional Knapsack.md @@ -0,0 +1,52 @@ +# Knapsack Problem (Greedy algorithm) + +#### Problem Statement + +Given a set of items,each with weight and a value,determine the number of each item to include in a collection so that the total weight is less than or equal to given limit and the total value is as large as possible. + +##### Greedy method will always provide an optimal solution with fractional knapsack problem. + +#### Time Complexity + +O(log n) Worst Case +O(1) Best Case (If middle element of initial array is the target element) + +#### Space Complexity + +O(1) For iterative approach +O(1) For recursive approach *if tail call optimization is used*, O(log n) due to recursion call stack, otherwise + +#### Example + +``` +Lets assume the capacity of knapsack, W = 60 +value = [280, 100, 120, 120] +weight = [40, 10, 20, 24] + +Ratio(V/W) = 7,10,6,5 +Say those items as A,B,C,D +next the items should be sorted in descending order on the basis of ratio of value by weight in order to get maximum profit +First and foremost, B was picked since its weight is smaller than the knapsack's capacity. The next item, A, is chosen since the knapsack's available capacity is more than A's weight. C is now the next item on the list. However, the entire item cannot be chosen because the knapsack's remaining capacity is less than C's weight. +As a result, the C proportion (60–50)/20) +The knapsack's capacity is now equal to the specified items. +As a result, no more items can be chosen. + +10 + 40 + 20*(10/20) = 60 is the total weight of the chosen goods. + +100+280+120*(10/20)=380+60=440 is the total profit. + +This is the most suitable option. + +We won't be able to make more money by combining diverse things. + +``` + +#### Code Implementation Links + +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/greedy_algorithms/knapsack.cpp) +- [Python](https://github.com/TheAlgorithms/Python/tree/master/knapsack) +- [C-Sharp](https://github.com/TheAlgorithms/C-Sharp/tree/master/Algorithms/Knapsack) + +#### Video Explanation + +[A CS50 video explaining the Greedy Algorithm](https://www.youtube.com/watch?v=Ou9OA0yQCYA) From 95332f897727c17b38b0992027ad9c4dc938c8d6 Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Fri, 12 Nov 2021 21:37:36 +0530 Subject: [PATCH 2/4] Update en/Greedy Algorithms/Fractional Knapsack.md Co-authored-by: David Leal --- en/Greedy Algorithms/Fractional Knapsack.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/Greedy Algorithms/Fractional Knapsack.md b/en/Greedy Algorithms/Fractional Knapsack.md index 753bd263..b4b268a5 100644 --- a/en/Greedy Algorithms/Fractional Knapsack.md +++ b/en/Greedy Algorithms/Fractional Knapsack.md @@ -2,7 +2,7 @@ #### Problem Statement -Given a set of items,each with weight and a value,determine the number of each item to include in a collection so that the total weight is less than or equal to given limit and the total value is as large as possible. +Given a set of items, each with weight and a value, determine the number of each item included in a collection so that the total weight is less than or equal to the given limit and the total value is as large as possible. ##### Greedy method will always provide an optimal solution with fractional knapsack problem. From da25afd1f4e7e6e1c2dc3e7a636d9c57b75f4535 Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Fri, 12 Nov 2021 21:37:53 +0530 Subject: [PATCH 3/4] Update en/Greedy Algorithms/Fractional Knapsack.md Co-authored-by: David Leal --- en/Greedy Algorithms/Fractional Knapsack.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/Greedy Algorithms/Fractional Knapsack.md b/en/Greedy Algorithms/Fractional Knapsack.md index b4b268a5..67998e40 100644 --- a/en/Greedy Algorithms/Fractional Knapsack.md +++ b/en/Greedy Algorithms/Fractional Knapsack.md @@ -25,7 +25,7 @@ weight = [40, 10, 20, 24] Ratio(V/W) = 7,10,6,5 Say those items as A,B,C,D -next the items should be sorted in descending order on the basis of ratio of value by weight in order to get maximum profit +next, the items should be sorted in descending order based on the ratio of value by weight to get maximum profit First and foremost, B was picked since its weight is smaller than the knapsack's capacity. The next item, A, is chosen since the knapsack's available capacity is more than A's weight. C is now the next item on the list. However, the entire item cannot be chosen because the knapsack's remaining capacity is less than C's weight. As a result, the C proportion (60–50)/20) The knapsack's capacity is now equal to the specified items. From 8d90b23ee4ada4f1ccd7bc74e0a49753a846d92d Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Sat, 13 Nov 2021 08:38:40 +0530 Subject: [PATCH 4/4] Update Fractional Knapsack.md --- en/Greedy Algorithms/Fractional Knapsack.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/en/Greedy Algorithms/Fractional Knapsack.md b/en/Greedy Algorithms/Fractional Knapsack.md index 67998e40..cc9eb8c8 100644 --- a/en/Greedy Algorithms/Fractional Knapsack.md +++ b/en/Greedy Algorithms/Fractional Knapsack.md @@ -8,13 +8,7 @@ Given a set of items, each with weight and a value, determine the number of each #### Time Complexity -O(log n) Worst Case -O(1) Best Case (If middle element of initial array is the target element) - -#### Space Complexity - -O(1) For iterative approach -O(1) For recursive approach *if tail call optimization is used*, O(log n) due to recursion call stack, otherwise +O(nlog n) Worst Case #### Example