diff --git a/algorithms/CPlusPlus/Arrays/fractional-knapsack.cpp b/algorithms/CPlusPlus/Arrays/fractional-knapsack.cpp new file mode 100644 index 00000000..74002754 --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/fractional-knapsack.cpp @@ -0,0 +1,69 @@ +// Description : Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. +// We can break items for maximizing the total value of knapsack/ + +// Algorithm Type: Divide & Conquer +// Time Complexity: O(n*log(n)) +#include +using namespace std; +struct Item +{ + int value, weight; +}; +bool comp(Item a, Item b) +{ + double r1 = (double)a.value / (double)a.weight; + double r2 = (double)b.value / (double)b.weight; + return r1 > r2; +} + +//Function to get the maximum total value in the knapsack. +double fractionalKnapsack(int W, vector &arr, int n) +{ + + sort(arr.begin(), arr.end(), comp); + + int curWeight = 0; + double finalvalue = 0.0; + + for (int i = 0; i < n; i++) + { + + if (curWeight + arr[i].weight <= W) + { + curWeight += arr[i].weight; + finalvalue += arr[i].value; + } + + else + { + int remain = W - curWeight; + finalvalue += (arr[i].value / (double)arr[i].weight) * (double)remain; + break; + } + } + + return finalvalue; +} +int main() +{ + int W; // Weight of knapsack + cout << "Enter the weight(W) of the knapsack" << endl; + cin >> W; + int n; + cout << "Enter the total number(n) of {Value-Weight}pairs" << endl; + cin >> n; + vector arr(n); + + for (int i = 0; i < n; i++) + { + + cout << "Enter value for pair " << i + 1 << endl; + cin >> arr[i].value; + cout << "Enter weight for pair " << i + 1 << endl; + cin >> arr[i].weight; + } + + // Function call + cout << "Maximum value we can obtain = " << fractionalKnapsack(W, arr, n); + return 0; +} diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 4b4b891d..718f4e0a 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -9,6 +9,7 @@ 6. [Boyer–Moore Voting Algorithm](Arrays/boyer_more.cpp) 7. [Reverse Array](Arrays/reverse-array.cpp) 8. [Sorted-Rotated Search Array](Arrays/search-sorted-rotated.cpp) +9. [Fractional Knapsack](Arrays/fractional-knapsack.cpp) ## Graphs 1. [Bellman Ford Algorithm](Graphs/bellmam-ford.cpp)