0% found this document useful (0 votes)
13 views4 pages

Knapsack problem (Anurag Verma) v1.0

The document outlines an experiment to implement the Knapsack problem using a greedy approach, focusing on maximizing profit based on a profit-to-weight ratio. It details the algorithm steps, provides C++ code for the implementation, and concludes that the greedy strategy effectively yields optimal solutions. The experiment demonstrates that fractional items can be included when full items do not fit in the knapsack.

Uploaded by

Anurag Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Knapsack problem (Anurag Verma) v1.0

The document outlines an experiment to implement the Knapsack problem using a greedy approach, focusing on maximizing profit based on a profit-to-weight ratio. It details the algorithm steps, provides C++ code for the implementation, and concludes that the greedy strategy effectively yields optimal solutions. The experiment demonstrates that fractional items can be included when full items do not fit in the knapsack.

Uploaded by

Anurag Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Anurag Verma EN22CS301175

Experiment 6
Aim
To implement the Knapsack problem using a greedy approach.

Theory

Knapsack Problem
Given the weights and profits of N items, in the form of {profit, weight} put
these items in a knapsack of capacity W to get the maximum total profit in
the knapsack.

Naive Approach:
Try all possible subsets with all different fractions.

Greedy Approach:
The greedy approach to the Knapsack is an algorithm for making decisions
that have to make a locally optimal choice at each stage in the hope that
this will eventually lead to the best overall decision.

In other words, it chooses items that have high value-to-weight ratios by


iteratively selecting them based on increasing cost-benefit ratio whereby
those items whose price can be paid less per unit utility derived from them
are always preferred over others. Therefore, at any point in time, it just
picks the item with a higher value/weight ratio without considering future
consequences.

Algorithm:
1.​ Calculate the ratio profit/weight for each item.
2.​ Sort the items on the basis of this ratio.
3.​ Take the item with the highest ratio and add them as much as we can
(can be the whole element or a fraction of it).

47
Anurag Verma EN22CS301175

This will always give the maximum profit because, in each step it adds an
element such that this is the maximum possible profit for that much weight

Code
#include <bits/stdc++.h>
using namespace std;

struct Item {
int value, weight;

// Constructor
Item(int item_value, int item_weight)
: value(item_value), weight(item_weight) {
}
};

// Comparison function to sort Item according to


// value/weight ratio
bool cmp(struct Item a, struct Item b) {
double r1 = (double)a.value / a.weight;
double r2 = (double)b.value / b.weight;
return r1 > r2;
}

double fractionalKnapsack(int W, struct Item arr[], int


n) {
// sorting Item on basis of ratio
sort(arr, arr + n, cmp);

/*

48
Anurag Verma EN22CS301175

for (int i = 0; i < n; i++) {


cout << arr[i].value << " " << arr[i].weight
<< " :
" << ((double)arr[i].value / arr[i].weight) <<
endl;
}
*/

int curWeight = 0;
double finalvalue = 0.0; // Result (value in
Knapsack)

// Looping through all Items


for (int i = 0; i < n; i++) {
// If adding Item won't overflow, add it
completely
if (curWeight + arr[i].weight <= W) {
curWeight += arr[i].weight;
finalvalue += arr[i].value;
}

// If we can't add current Item, add fractional


part of it
else {
int remain = W - curWeight;
finalvalue += arr[i].value *
((double)remain / arr[i].weight);
break;
}
}

49
Anurag Verma EN22CS301175

return finalvalue;
}

// Driver code
int main() {
int W = 40; // Weight of knapsack
Item arr[] = {{500, 30}, {100, 20}};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "\nMaximum value we can obtain = "


<< fractionalKnapsack(W, arr, n) << "\n";
return 0;
}

Output

Conclusion
The experiment successfully implemented the Fractional Knapsack
Problem using a greedy approach. By selecting items based on their
profit-to-weight ratio, the algorithm efficiently maximizes the total profit. If
a full item cannot fit, a fractional part is included. The results confirm that
the greedy strategy provides an optimal solution for this problem.

50

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy