From e02f35b1bbc7bfe0f44451989ec8b23c6aecbb87 Mon Sep 17 00:00:00 2001 From: s-mriddhi Date: Sun, 29 Jun 2025 14:19:30 +0530 Subject: [PATCH] Commented the algorithm logic --- greedy_algorithms/knapsack.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/greedy_algorithms/knapsack.cpp b/greedy_algorithms/knapsack.cpp index b5e9f6374ec..7c6414718eb 100644 --- a/greedy_algorithms/knapsack.cpp +++ b/greedy_algorithms/knapsack.cpp @@ -1,12 +1,13 @@ #include using namespace std; -struct Item { +struct Item { int weight; int profit; }; -float profitPerUnit(Item x) { return (float)x.profit / (float)x.weight; } +//function to calculate input data for algo which is profit/wt of each item +float profitPerUnit(Item x) { return (float)x.profit / (float)x.weight; } int partition(Item arr[], int low, int high) { Item pivot = arr[high]; // pivot @@ -39,11 +40,12 @@ void quickSort(Item arr[], int low, int high) { int main() { cout << "\nEnter the capacity of the knapsack : "; - float capacity; + float capacity; //constraint cin >> capacity; cout << "\n Enter the number of Items : "; int n; cin >> n; + //create item data structure, array here of size n Item *itemArray = new Item[n]; for (int i = 0; i < n; i++) { cout << "\nEnter the weight and profit of item " << i + 1 << " : "; @@ -51,20 +53,25 @@ int main() { cin >> itemArray[i].profit; } - quickSort(itemArray, 0, n - 1); + quickSort(itemArray, 0, n - 1); // sorts by profitperunit parameter in the partition function // show(itemArray, n); - + //Apply algorithm -> float maxProfit = 0; int i = n; while (capacity > 0 && --i >= 0) { + //start from item with largest profitperunit if (capacity >= itemArray[i].weight) { + //Add to the profit and reduce the capacity maxProfit += itemArray[i].profit; capacity -= itemArray[i].weight; + //Print the respective wt taken and profit of that item cout << "\n\t" << itemArray[i].weight << "\t" << itemArray[i].profit; } else { + // take the fraction of item wt such that capacity is exhausted but not overloaded maxProfit += profitPerUnit(itemArray[i]) * capacity; + //print the fractional wt taken and profit obtained on this wt of item cout << "\n\t" << capacity << "\t" << profitPerUnit(itemArray[i]) * capacity; capacity = 0;