-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKNAPSACK.C
209 lines (161 loc) · 3.29 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Knapsack Problem using Greedy Method */
#include<stdio.h>
#include<conio.h>
void Knapsack(int n, float w[], float p[], float m);
void LargestProfit(int n, float w[], float p[], float m);
void SmallestWeight(int n, float w[], float p[], float m);
int main()
{
float w[20], p[20], ratio[20], m;
int n; //p-profit, w-weights, x-ratio, m-capacity, n-num
int i, j;
float temp;
clrscr();
printf("\nEnter the number of objects: ");
scanf("%d", &n);
printf("\nEnter the details: ");
for(i=0;i<n;i++)
{
printf("\nItem %d:", i+1);
printf("\n\tWeight: ");
scanf("%f", &w[i]);
printf("\tProfit: ");
scanf("%f", &p[i]);
}
printf("\nEnter the capacity of Knapsack: ");
scanf("%f", &m);
for(i=0;i<n;i++)
{
ratio[i]=p[i]/w[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(ratio[i] < ratio[j])
{
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = w[j];
w[j] = w[i];
w[i] = temp;
temp = p[j];
p[j] = p[i];
p[i] = temp;
}
}
}
printf("\nLargest Ratio of Profit by Weight Strategy:\n");
Knapsack(n, w, p, m);
printf("\n\nLargest Profit Strategy:\n");
LargestProfit(n, w, p, m);
printf("\n\nSmallest Weight Strategy:\n");
SmallestWeight(n, w, p, m);
getch();
return 0;
}
//-------CALCULATE FRACTION OF WEIGHTS AND PROFIT-------
void Knapsack(int n, float w[], float p[], float m)
{
float x[20], profit;
int i, j;
float u;
u=m;
profit=0.0;
for(i=0;i<n;i++)
x[i]=0.0;
for(i=0;i<n;i++)
{
if(w[i] > u)
break;
else
{
x[i]=1.0;
u=u-w[i];
profit=profit+p[i];
}
}
if(i<n)
x[i]=u/w[i];
profit=profit + ( x[i] * p[i] );
printf("\n\tThe resultant vector is ");
for(i=0;i<n;i++)
printf("%.2f\t", x[i]);
printf("\n\n\tThe total profit is %.3f", profit);
}
//-------LARGEST PROFIT STRATEGY-------
void LargestProfit(int n, float w[], float p[], float m)
{
int i, j;
float temp;
float ratio[20];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i] < p[j])
{
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = w[j];
w[j] = w[i];
w[i] = temp;
temp = p[j];
p[j] = p[i];
p[i] = temp;
}
}
}
Knapsack(n, w, p, m);
}
//-------SMALLEST WEIGHT STRATEGY-------
void SmallestWeight(int n, float w[], float p[], float m)
{
int i, j;
float temp;
float ratio[20];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(w[i] > w[j])
{
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = w[j];
w[j] = w[i];
w[i] = temp;
temp = p[j];
p[j] = p[i];
p[i] = temp;
}
}
}
Knapsack(n, w, p, m);
}
/* OUTPUT
Enter the number of objects: 3
Enter the details:
Item 1:
Weight: 18
Profit: 25
Item 2:
Weight: 10
Profit: 24
Item 3:
Weight: 10
Profit: 15
Enter the capacity of Knapsack: 20
Largest Ratio of Profit by Weight Strategy:
The resultant vector is 1.00 0.50 0.00
The total profit is 31.500
Largest Profit Strategy:
The resultant vector is 1.00 0.13 0.00
The total profit is 28.200
Smallest Weight Strategy:
The resultant vector is 1.00 0.67 0.00
The total profit is 31.000
*/