0% found this document useful (0 votes)
6 views6 pages

Week 72

This document provides a C program that implements the Greedy method for solving the Knapsack problem. It includes steps for reading item weights and profits, calculating the optimal solution based on value-to-weight ratios, and displaying the result vector and maximum profit. The program demonstrates its functionality with example inputs and outputs.

Uploaded by

madhumitha69m
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)
6 views6 pages

Week 72

This document provides a C program that implements the Greedy method for solving the Knapsack problem. It includes steps for reading item weights and profits, calculating the optimal solution based on value-to-weight ratios, and displaying the result vector and maximum profit. The program demonstrates its functionality with example inputs and outputs.

Uploaded by

madhumitha69m
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/ 6

Write a C program for calculating Knapscak using Greedy method

Algorithm:

Step1: Start the program.

Step2: Declare the variable.

Step3: Using the get function read the number of items, capacity of the
bag, Weight of the item and value of the items.

Step4: Find the small weight with high value using the find function.
Step5: Find the optimal solution using the function findop ().

Step6: Display the optimal solution for the items.

Step7: Stop the process


#include<stdio.h>

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

float x[20], tp = 0;

int i,j, u;

u = capacity;

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

x[i] = 0.0;

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

if (weight[i] > u)

break; else {

x[i] =1.0;

tp= tp+profit[i];
u = u - weight[i];

if(i<n)

x[i] =u / weight[i];

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

printf("\nThe result vector is:-"); for (i = 0; i < n; i++)

printf("%f\t", x[i]); printf("\nMaximumprofitis:-%f",tp);

int main()

float weight[20],profit[20],capacity; int num, i, j;

float ratio[20], temp;

printf("\nEnter the no. of objects:-");

scanf("%d", &num);

printf("\n Enter the wts and profits of each object:-");

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

{
scanf("%f%f",&weight[i],&profit[i]);

printf("\nEnter the capacity ofknapsack:-");

scanf("%f", &capacity);

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])

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;


}

knapsack(num,weight,profit,capacity);

return(0);

Enter the no. of objects:-

Enter the wts and profits of each object:-

2 12

3 10

1 20

4 15

Enter the capacity ofknapsack:-

15

The result vector is:-1.000000 1.000000 1.000000 1.000000


Maximumprofitis:-57.000000

Enter the no. of objects:-

Enter the wts and profits of each object:-

10 20

20 30

30 66

40 40

50 60

Enter the capacity ofknapsack:-100

The result vector is:-1.000000 1.000000 1.000000 0.800000


0.000000

Maximumprofitis:-164.000000

You might also like