forked from rafi007akhtar/c-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.c
More file actions
61 lines (45 loc) · 1.22 KB
/
knapsack.c
File metadata and controls
61 lines (45 loc) · 1.22 KB
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
// Greedy technique to solve the knapsack problem
// Assumption: This program assumes the weights and profits are input in the order of p[i]/w[i]
#include <stdio.h>
#include <stdlib.h>
float *x, *w, *p; // solution array(x[]), weights (w[]), profits(p[])
int n, m; // number of elements in the arrays (n), knapsack size (m)
// inline functions
#define allo (float *)calloc(n,sizeof(float))
// user defined functions
void greedyKnapsack();
int main()
{
int i;
printf("Enter the number of elements: ");
scanf("%d", &n);
x = allo;
w = allo;
p = allo;
printf("Enter the weights: ");
for (i = 0; i < n; i++) scanf("%f", &w[i]);
printf("Enter the profits: ");
for (i = 0; i < n; i++) scanf("%f", &p[i]);
printf("Enter knapsack capacity: ");
scanf("%d", &m);
// assuming p[i]/w[i] > p[i+1]/w[i+1] for all i = 0 to n-1:
greedyKnapsack();
printf("\nThe solution array is:\n");
for (i = 0; i < n; i++) printf("%.3f ", x[i]);
printf("\nProfit yielded: ");
float s = 0;
for (i = 0; i < n; i++) s += p[i]*x[i];
printf("%.3f\n", s);
return 0;
}
void greedyKnapsack()
{
int i, temp = m;
for (i = 0; i < n; i++)
{
if (w[i] > temp) break;
x[i] = 1;
temp -= w[i];
}
if (i < n-1) x[i] = (temp*1.0) / (w[i]*1.0);
}