Knapsack Problem - Greedy Method Code Implementation in C | Analysis of Algorithm

preview_player
Показать описание
#KnapsackProblemCode #FractionalKnapsackCode #EngineeringFrolic

Do like subscribe and share the video
Рекомендации по теме
Комментарии
Автор

Simple and Perfect Explanation.
This Was Really Helpful.

mdtahmid
Автор

You explained it in very simple way thank you brother❤

amanpandey
Автор

The best C code explanation video of Knapsack on YouTube.
Thank You Man❤‍🔥.

abhranilray
Автор

Very helpful explanation sir... Understood everything properly.. Thanks you

sohinibhattacharjee
Автор

Not working for (check the soln bottom)
Profit - 25, 24, 15
Weight - 18, 15, 10
Output should be 31.5.. but it gives something else

#include<stdio.h>

void swap(float *xp, float *yp)
{
float temp;
temp = *xp;
*xp = *yp;
*yp = temp;
}
void knapsack(float *p, float *w, int n, int m){
int i;
float tp=0, d[20];

for(i=0;i<n;i++)
d[i]=0.0;

for(i=0;i<n;i++){
if(w[i]<=m){
d[i]=1;
tp=tp+p[i];
m=m-w[i];
}
else{
d[i]=m/w[i];
tp=tp+(d[i]*p[i]);
break;
}
}

for(i=0;i<n;i++)
printf("%f\t", d[i]);
printf("\nMAX PROFIT IS : %f", tp);
}
main()
{
int i, m, n, j;
float profit[10], weight[10], ratio[10];
printf("\nEnter no of elements :");
scanf("%d", &n);
printf("\nEnter profit and weight of product respectively\n");
for(i=0;i<n;i++){
printf("\nElement %d(PROFIT, WEIGHT ):", i+1);
scanf("%f\t %f", &profit[i], &weight[i]);
printf("\nprofit =%f, weight= %f", profit[i], weight[i]);

}
printf("\nCapacity of the knapsack :");
scanf("%d", &m);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(ratio[j]>ratio[i]){
swap(&ratio[j], &ratio[i]);
swap(&profit[j], &profit[i]);
swap(&weight[j], &weight[i]);
}
}
}
knapsack(profit, weight, n, m);
}

capa
Автор

Loved the explanation ❤..I am going to code in my PC

subhashkr
Автор

this is the best explanation, thank you

anuragmeshram
Автор

Thank you vaiya .Aisa video banate raho❤❤❤❤❤❤

arnabpramanik
Автор

In void knapsack why do you take j Sir?

bhargobiroy
Автор

why dis it take 1 vector in the case of 10 p/w ratio when it can do 2

srijanpandey
Автор

Great video.
Continue posting videos.

kuberarale
Автор

Bro you need to do u=0 when weight is greater than capacity

ChillakuruMahan
Автор

Sir this works for both fractional and o/1

yashpatel
Автор

please don't post the code from next time because it's not working properly.

survipandey
welcome to shbcf.ru