-
Notifications
You must be signed in to change notification settings - Fork 1
/
KnapSack.c
73 lines (60 loc) · 956 Bytes
/
KnapSack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<stdio.h>
int max(int x,int y)
{
return(x>y)?x:y;
}
void knapsack(int m,int n,int w[],int p[]){
int v[100][200],x[10],i,j,sum=0;
for(i=0;i<=n;i++) //filling first row and column with zeros
v[i][0]=0;
for(j=0;j<=m;j++)
v[0][j]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(j>=w[i])
v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
else
v[i][j]=v[i-1][j];
}
}
for(i=1;i<=n;i++)
x[i]=0;
i=n;
j=m;
while(i>0 && j>0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
}
i--;
}
printf("the optimal set of items is\n");
for(i=1;i<=n;i++)
{
if(x[i]==1)
{
printf("%d\t",i);
sum=sum+p[i];
}
}
printf("total profit is=%d",sum);
}
void main(){
int w[10],p[10],m;
int i,n,j;
printf("enter the number of elenmts\n");
scanf("%d",&n);
printf("enter the weights\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("enter the profit values of the items\n");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("enter the capacity\n");
scanf("%d",&m);
knapsack(m,n,w,p);
}