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

Greedy Knapsack

Uploaded by

Shambhavi
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)
3 views3 pages

Greedy Knapsack

Uploaded by

Shambhavi
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/ 3

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

continuous Knapsack problems using greedy approximation method.

#include <stdio.h>
float Knapsack(float weight[], float profit[], float ratio[],
float capacity, int n, int isDiscrete)
{
int i;
float totalValue = 0;
if (isDiscrete)
{
for (i = 0; i < n; i++)
if (weight[i] <= capacity)
{
totalValue += profit[i];
capacity -= weight[i];
}
}
else
{
for (i = 0; i < n && weight[i] <= capacity; i++)
{
totalValue += profit[i];
capacity -= weight[i];
}

if (i < n)
totalValue += ratio[i] * capacity;
}

return totalValue;
}

int main()
{
float weight[50], profit[50], ratio[50], temp, capacity;
int n, i, j;

printf("Enter the number of items: ");


scanf("%d", &n);

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


printf("Enter Weight and Profit for item[%d]:\n", i + 1);
scanf("%f %f", &weight[i], &profit[i]);
}

printf("Enter the capacity of knapsack: ");


scanf("%f", &capacity);

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


ratio[i] = profit[i] / weight[i];

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


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

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}

printf("\nDiscrete Knapsack problems using Greedy Algorithm:\n");


printf("\tThe maximum value is[Discrete]: %f\n",
Knapsack(weight, profit, ratio, capacity, n, 1));

printf("\nContinuous Knapsack problems using Greedy Algorithm:\n");


printf("\tThe maximum value is[Continuous]: %f\n",
Knapsack(weight, profit, ratio, capacity, n, 0));

return 0;
}

OUTPUT:

Enter the number of items: 4


Enter Weight and Profit for item[1]:
7 42
Enter Weight and Profit for item[2]:
3 12
Enter Weight and Profit for item[3]:
4 40
Enter Weight and Profit for item[4]:
5 25
Enter the capacity of knapsack: 10

Discrete Knapsack problems using Greedy Algorithm:


​ The maximum value is[Discrete]: 65.000000

Continuous Knapsack problems using Greedy Algorithm:


​ The maximum value is[Continuous]: 76.000000

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