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

Knapsack-N

This document contains a C program that implements the Knapsack problem using a greedy method. It prompts the user to enter the number of items, their weights, profits, and the capacity of the knapsack, then calculates the maximum profit and the result vector of item selections. The output shows the selected items and the maximum profit achieved.

Uploaded by

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

Knapsack-N

This document contains a C program that implements the Knapsack problem using a greedy method. It prompts the user to enter the number of items, their weights, profits, and the capacity of the knapsack, then calculates the maximum profit and the result vector of item selections. The output shows the selected items and the maximum profit achieved.

Uploaded by

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

//Program to implement KNAPSACK problem using Greedy method

#include<stdio.h>
#include<conio.h>

void knapsack(int n, float weight[], float profit[], float capacity)


{
float x[20],tp;
int i,j,rcap;

tp = 0;
rcap = capacity;

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


x[i] = 0.0;

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


{
if (weight[i] > rcap)
break;
else
{
x[i] = 1.0;
tp = tp + profit[i];
rcap = rcap - weight[i];
}
}

if (i < n)
x[i] = rcap / weight[i];

tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is = {");


for (i=0; i<n-1; i++)
printf("%0.2f, ",x[i]);
printf("%0.2f}\n",x[i]);
printf("\nMaximum profit is = %0.3f",tp);
}

void swap(int A[20], int x, int y)


{
int temp = A[x];
A[x] = A[y];
A[y] = temp;
}

void main()
{
float weight[20], profit[20], ratio[20], capacity;
int num,i,j;
float temp;

clrscr();
printf("Enter the no. of items : ");
scanf("%d",&num);
printf("\nEnter the capacity of knapsack : ");
scanf("%f",&capacity);

printf("\nEnter the wights and profits of each object.....\n");


for (i = 0; i < num; i++)
{
printf("Item no. %d : ",i+1);
scanf("%f%f",&weight[i],&profit[i]);
}

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


ratio[i] = profit[i] / weight[i];
for (i = 0; i < num; i++)
for (j = i + 1; j < num; j++)
if (ratio[i] < ratio[j])
{
swap(ratio,i,j);
swap(weight,i,j);
swap(profit,i,j);
}

knapsack(num, weight, profit, capacity);


getch();
}

OUTPUT :

Enter the no. of items : 7

Enter the capacity of knapsack : 15

Enter the wights and profits of each object.....


Item no. 1 : 2 10
Item no. 2 : 3 5
Item no. 3 : 5 15
Item no. 4 : 7 7
Item no. 5 : 1 6
Item no. 6 : 4 18
Item no. 7 : 1 3

The result vector is = {1.00, 1.00, 1.00, 1.00, 1.00, 0.67, 0.00}

Maximum profit is = 55.333

You might also like