typedef struct {
int value;
int weight;
} Item;
float fractionalKnapsack(int W, Item items[], int n) {
float totalValue = 0.0;
int i, curWeight = 0;
float ratio[n];
for (i = 0; i < n; i++) {
ratio[i] = (float)items[i].value / items[i].weight;
}
for (i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
float temp = ratio[i]; ratio[i] = ratio[j]; ratio[j] = temp;
Item tempItem = items[i]; items[i] = items[j]; items[j] = tempItem;
}
}
}
for (i = 0; i < n; i++) {
if (curWeight + items[i].weight <= W) {
curWeight += items[i].weight;
totalValue += items[i].value;
} else {
int remain = W - curWeight;
totalValue += ratio[i] * remain;
break;
}
}
return totalValue;
}
#include <stdio.h>
int main() {
Item items[] = {{60, 10}, {100, 20}, {120, 30}};
int W = 50;
int n = sizeof(items) / sizeof(items[0]);
float maxValue = fractionalKnapsack(W, items, n);
printf("%.2f\n", maxValue);
return 0;
}The code sorts items by their value-to-weight ratio in descending order. Then it adds whole items until the knapsack is full. For the last item, it adds the fractional part that fits.
Items sorted by ratio: (60/10=6), (100/20=5), (120/30=4). Total capacity is 50.
Pick item 1 fully (weight 10, value 60), remaining capacity 40.
Pick item 2 fully (weight 20, value 100), remaining capacity 20.
Pick 20/30 fraction of item 3: value = 4 * 20 = 80.
Total value = 60 + 100 + 80 = 240.
The value-to-weight ratio shows how much value each unit of weight contributes. Picking items with the highest ratio first ensures the knapsack holds the most valuable combination.
for (i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (ratio[i] > ratio[j]) { float temp = ratio[i]; ratio[i] = ratio[j]; ratio[j] = temp; Item tempItem = items[i]; items[i] = items[j]; items[j] = tempItem; } } }
The fractional knapsack requires sorting items by descending value-to-weight ratio. The code uses ratio[i] > ratio[j], which sorts ascending. It should be ratio[i] < ratio[j] to sort descending.
typedef struct {
int value;
int weight;
} Item;
float fractionalKnapsack(int W, Item items[], int n) {
float totalValue = 0.0;
int i, curWeight = 0;
float ratio[n];
for (i = 0; i < n; i++) {
ratio[i] = (float)items[i].value / items[i].weight;
}
for (i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
float temp = ratio[i]; ratio[i] = ratio[j]; ratio[j] = temp;
Item tempItem = items[i]; items[i] = items[j]; items[j] = tempItem;
}
}
}
for (i = 0; i < n; i++) {
if (curWeight + items[i].weight <= W) {
curWeight += items[i].weight;
totalValue += items[i].value;
} else {
int remain = W - curWeight;
totalValue += ratio[i] * remain;
break;
}
}
return totalValue;
}
#include <stdio.h>
int main() {
Item items[] = {{60, 10}, {100, 20}, {120, 30}};
int W = 0;
int n = sizeof(items) / sizeof(items[0]);
float maxValue = fractionalKnapsack(W, items, n);
printf("%.2f\n", maxValue);
return 0;
}Since the knapsack capacity is zero, no weight can be added. The total value remains zero.
Fractional knapsack allows taking parts of items, so choosing the highest value-to-weight ratio first always leads to the best total value. 0/1 knapsack requires whole items only, so combinations must be checked to find the best set.