-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfractional_knapsack.c
More file actions
85 lines (64 loc) · 1.67 KB
/
Copy pathfractional_knapsack.c
File metadata and controls
85 lines (64 loc) · 1.67 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
struct Item {
char id[5];
float weight;
float value;
float density;
};
void fractionalKnapsack(struct Item items[], int n, int W);
int main(void) {
//variables
int i, j;
//list items
struct Item items[6] = {
{"i1", 6, 6, 0},
{"i2", 10, 2, 0},
{"i3", 3, 1, 0},
{"i4", 5, 8, 0},
{"i5", 1, 3, 0},
{"i6", 3, 5, 0}
};
//temp item
struct Item temp;
//number of items
int n = 6;
//max weight limit of knapsack
int W = 16;
//compute desity = (value/weight)
for(i = 0; i < n; i++) {
items[i].density = (items[i].value) / items[i].weight;
}
//sort by density in descending order
for(i = 1; i < n; i++) {
for(j = 0; j < n - i; j++) {
if(items[j+1].density > items[j].density) {
temp = items[j+1];
items[j+1] = items[j];
items[j] = temp;
}
}
}
fractionalKnapsack(items, n, W);
return 0;
}
void fractionalKnapsack(struct Item items[], int n, int W) {
int i, wt;
float value;
float totalWeight = 0, totalBenefit = 0;
for(i = 0; i < n; i++) {
if(items[i].weight + totalWeight <= W) {
totalWeight += items[i].weight;
totalBenefit += items[i].value;
printf("Selected Item: %s\tWeight: %f\tValue: %f\tTotal Weight: %f\tTotal Benefit: %f\n", items[i].id, items[i].weight, items[i].value, totalWeight, totalBenefit);
} else {
wt = (W - totalWeight);
value = wt * (items[i].value) / (items[i].weight);
totalWeight += wt;
totalBenefit += value;
printf("Selected Item: %s\tWeight: %d\tValue: %f\tTotal Weight: %f\tTotal Benefit: %f\n", items[i].id, wt, value, totalWeight, totalBenefit);
break;
}
}
printf("Total Weight: %f\n", totalWeight);
printf("Total Benefit: %f\n", totalBenefit);
}