0% found this document useful (0 votes)
29 views3 pages

Org 6 B

This document contains a C++ program that implements the fractional knapsack problem using a greedy algorithm. It defines functions to swap integers and doubles, sort items by their profit-to-weight ratio, and calculate the maximum profit that can be obtained within a given weight capacity. The main function collects user input for item profits and weights, then calls the knapsack function to display the selected items and the maximum profit.
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)
29 views3 pages

Org 6 B

This document contains a C++ program that implements the fractional knapsack problem using a greedy algorithm. It defines functions to swap integers and doubles, sort items by their profit-to-weight ratio, and calculate the maximum profit that can be obtained within a given weight capacity. The main function collects user input for item profits and weights, then calls the knapsack function to display the selected items and the maximum profit.
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

#include <iostream>

using namespace std;

void swapInt(int &a, int &b) {

int temp = a;

a = b;

b = temp;

void swapDouble(double &a, double &b) {

double temp = a;

a = b;

b = temp;

void sortByRatio(int p[], int w[], double ratio[], int n) {

// Bubble sort by decreasing ratio

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

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

if (ratio[j] < ratio[j + 1]) {

swapDouble(ratio[j], ratio[j + 1]);

swapInt(p[j], p[j + 1]);

swapInt(w[j], w[j + 1]);

}
double knapsack(int p[], int w[], int n, int W) {

double ratio[10];

// Calculate profit/weight ratio for each item

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

ratio[i] = (double)p[i] / w[i];

// Sort items by ratio decreasing

sortByRatio(p, w, ratio, n);

double maxProfit = 0.0;

int currCap = W;

cout<<"the knapsack with following items are:"<<endl;

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

if (w[i] <= currCap) {

maxProfit += p[i];

currCap -= w[i];

cout<<"item with weight:"<<w[i]<<" and the profit:"<<p[i]<<" and is selected


full:1"<<endl;

} else {

// Take fraction of item

double fraction = (double)currCap / w[i];

maxProfit += p[i] * fraction;

cout<<"item with weight:"<<w[i]<<" and the profit:"<<p[i]<<" and is selected


fractionally:"<<fraction<<endl;

break;

}
cout << "Max profit: " << maxProfit << endl;

int main() {

int n, W;

int p[10], w[10];

cout << "Enter number of items: ";

cin >> n;

cout << "Enter knapsack capacity: ";

cin >> W;

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

cout << "Item " << i + 1 << endl;

cout << "Profit: ";

cin >> p[i];

cout << "Weight: ";

cin >> w[i];

knapsack(p, w, n, W);

return 0;

You might also like