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

Program7 Knapsack C D Greedy

Knapsack greedy method

Uploaded by

disha434.ansar
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)
95 views4 pages

Program7 Knapsack C D Greedy

Knapsack greedy method

Uploaded by

disha434.ansar
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

Program-7: Design and implement C/C++ Program to solve discrete

Knapsack and continuous Knapsack problems using greedy


approximation method.
#include <stdio.h>

#include <stdlib.h>

typedef struct

{ int p; // Profit

int w; // Weight

float r; // Profit-to-weight ratio

float f; // Fraction of the item to be taken

} I; // Define a structure for items

I *a; // Pointer to an array of items

float k, r, t; // Knapsack capacity, remaining capacity, total profit

int n, i, j, p; // Number of items, loop variables

/* main function */

int main()

printf("Items: ");

scanf("%d", &n); // Read the number of items

printf("Capacity: ");

scanf("%f", &k); // Read the knapsack capacity

a = calloc(n, sizeof(I)); // Allocate memory for n items

// Input profits and weights for each item

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

printf("Profit: ");

scanf("%d", &a[i].p);

printf("Weight: ");

scanf("%d", &a[i].w);

a[i].r = (float)a[i].p / a[i].w; // Calculate profit-to-weight ratio

}
// Sort items by profit-to-weight ratio in descending order using bubble sort

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

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

if (a[j].r < a[j + 1].r) {

I t = a[j];

a[j] = a[j + 1];

a[j + 1] = t;

//Discrete Knapsack Solution

r = k; // Initialize remaining capacity

t = 0; // Initialize total profit

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

if (a[i].w <= r) { // If the item can fit in the remaining capacity

a[i].f = 1; // Take the whole item

r -= a[i].w; // Decrease remaining capacity

t += a[i].p; // Increase total profit

} else {

break; // Stop if the item cannot fit

printf("\nDiscrete Knapsack Solution\n");

printf("Item\tWeight\tProfit\tFraction\n");

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

printf("%d\t%d\t%d\t%.2f\n", i + 1, a[i].w, a[i].p, a[i].f);

printf("\nTotal Profit: %.2f\n", t);

// Resetting for Continuous Knapsack Solution

r = k; // Reset remaining capacity


t = 0; // Reset total profit

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

a[i].f = 0; // Reset fractions

// Continuous Knapsack Solution

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

if (a[i].w <= r) { // If the item can fit in the remaining capacity

a[i].f = 1; // Take the whole item

r -= a[i].w; // Decrease remaining capacity

t += a[i].p; // Increase total profit

} else {

a[i].f = r / a[i].w; // Take the fraction of the item that fits

t += a[i].p * a[i].f; // Increase total profit by the fraction of profit

break; // Stop as the knapsack is full

if (r == 0) break; // Stop if no remaining capacity

printf("\n\nContinuous Knapsack Solution\n");

printf("Item\tWeight\tProfit\tFraction\n");

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

printf("%d\t%d\t%d\t%.2f\n", i + 1, a[i].w, a[i].p, a[i].f);

printf("\nTotal Profit: %.2f\n", t);

free(a); // Free allocated memory

return 0;
}

You might also like