Knapsack
Knapsack
Knapsack
NO:41909112029
KNAPSACK PROBLEM
#include<stdio.h>
#include<conio.h>
float final_profit=-1.0;
int p[9]={0,11,21,31,33,43,53,55,65};
int w[9]={0,1,11,21,23,33,43,45,55};
int m=110,n=8;
int temp[9],x[9];
float final_wt=-1.0;
void main()
{
int i;
clrscr();
printf("\n \t Knapsack problem using back tracking algorithm");
printf("\nCapacity of knapsack:%d",m);
printf("\n\nProfit \t Weight");
printf("\n----------------------------");
for(i=1;i<=n;++i)
printf("\n%d\t%d",p[i],w[i]);
printf("\n----------------------------");
bk(1,0,0);
printf("\n Following items are selected from knapsack");
for(i=1;i<=n;++i)
{
if(x[i]==1)
printf("\n\t\titem%d",i);
}
printf("\n Final weight:%0.2f",final_wt);
printf("\n Final profit:%0.2f",final_profit);
getch();
}
OUTPUT:
Capacity of knapsack:110
Profit Weight
--------------------
11 1
21 11
31 21
33 23
43 33
53 43
55 45
65 55
----------------------------
Final weight:109.00
Final profit:159.00
RESULT: