0% found this document useful (0 votes)
46 views2 pages

Program 7 Design and implement C Program to solve discrete Knapsack and continuous Knapsack problems using greedy approximation method.

The document describes a C/C++ program that implements a greedy approximation method to solve both discrete and continuous Knapsack problems. It calculates the profit-to-weight ratio for items, sorts them, and fills the knapsack by selecting whole items and fractional parts as needed. The program outputs the optimal profit and the solution vector indicating which items were selected.

Uploaded by

vsai9986
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)
46 views2 pages

Program 7 Design and implement C Program to solve discrete Knapsack and continuous Knapsack problems using greedy approximation method.

The document describes a C/C++ program that implements a greedy approximation method to solve both discrete and continuous Knapsack problems. It calculates the profit-to-weight ratio for items, sorts them, and fills the knapsack by selecting whole items and fractional parts as needed. The program outputs the optimal profit and the solution vector indicating which items were selected.

Uploaded by

vsai9986
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/ 2

Program 7 Design and implement C/C++ Program to solve discrete Knapsack and

continuous Knapsack problems using greedy approximation method.

This program first calculates the profit-to-weight ratio for each item, then sorts the items based
on this ratio in non-increasing order. It then fills the knapsack greedily by selecting items with
the highest ratio until the knapsack is full. If there's space left in the knapsack after selecting
whole items, it adds fractional parts of the next item. Finally, it prints the optimal solution and
the solution vector.

Here's a simplified version of the C program to solve discrete Knapsack and continuous
Knapsack problems using the greedy approximation method:

#include <stdio.h>
#define MAX 50

int p[MAX], w[MAX], x[MAX];


double maxprofit;
int n, m, i, j;

void greedyKnapsack(int n, int w[], int p[], int m) {


double ratio[MAX];

// Calculate the ratio of profit to weight for each item


for (i = 0; i < n; i++) {
ratio[i] = (double)p[i] / w[i];
}

// Sort items based on the ratio in non-increasing order


for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
double temp = ratio[i];
ratio[i] = ratio[j];
ratio[j] = temp;

int temp2 = w[i];


w[i] = w[j];
w[j] = temp2;

temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
}
}
}

int currentWeight = 0;
maxprofit = 0.0;
// Fill the knapsack with items
for (i = 0; i < n; i++) {
if (currentWeight + w[i] <= m) {
x[i] = 1; // Item i is selected
currentWeight += w[i];
maxprofit += p[i];
} else {
// Fractional part of item i is selected
x[i] = (m - currentWeight) / (double)w[i];
maxprofit += x[i] * p[i];
break;
}
}

printf("Optimal solution for greedy method: %.1f\n", maxprofit);


printf("Solution vector for greedy method: ");
for (i = 0; i < n; i++)
printf("%d\t", x[i]);
}

int main() {
printf("Enter the number of objects: ");
scanf("%d", &n);

printf("Enter the objects' weights: ");


for (i = 0; i < n; i++)
scanf("%d", &w[i]);

printf("Enter the objects' profits: ");


for (i = 0; i < n; i++)
scanf("%d", &p[i]);

printf("Enter the maximum capacity: ");


scanf("%d", &m);

greedyKnapsack(n, w, p, m);

return 0;
}

You might also like