0
0
DSA Cprogramming

Fractional Knapsack Problem in DSA C

Choose your learning style9 modes available
Mental Model
We want to fill a bag with items to get the most value, and we can take parts of items if needed.
Analogy: Imagine filling a jar with different types of honey jars. You want the sweetest honey first, and if the jar is almost full, you take only part of the last honey jar.
Knapsack capacity: [__________] (empty)
Items sorted by value/weight ratio:
Item1(value=60, weight=10) -> ratio=6
Item2(value=100, weight=20) -> ratio=5
Item3(value=120, weight=30) -> ratio=4
Dry Run Walkthrough
Input: items: [(value=60, weight=10), (value=100, weight=20), (value=120, weight=30)], knapsack capacity=50
Goal: Maximize total value in knapsack by taking whole or fractional parts of items
Step 1: Sort items by value/weight ratio descending
Items order: (60,10) ratio=6 -> (100,20) ratio=5 -> (120,30) ratio=4
Why: We want to pick items with highest value per weight first to maximize value
Step 2: Take whole item 1 (weight 10) into knapsack
Knapsack: 10/50 filled, total value=60
Why: Item 1 fits fully, so take it all
Step 3: Take whole item 2 (weight 20) into knapsack
Knapsack: 30/50 filled, total value=160
Why: Item 2 fits fully, so take it all
Step 4: Take fraction of item 3 to fill remaining capacity (20/30 fraction)
Knapsack: 50/50 filled, total value=160 + (120 * 20/30) = 240
Why: Only part of item 3 fits, so take fraction to fill knapsack
Result:
Knapsack full: total value=240
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Structure to store item value and weight
typedef struct {
    int value;
    int weight;
    double ratio;
} Item;

// Comparator to sort items by value/weight ratio descending
int cmp(const void *a, const void *b) {
    Item *item1 = (Item *)a;
    Item *item2 = (Item *)b;
    if (item2->ratio > item1->ratio) return 1;
    else if (item2->ratio < item1->ratio) return -1;
    else return 0;
}

// Function to solve fractional knapsack
double fractionalKnapsack(Item items[], int n, int capacity) {
    // Calculate ratio for each item
    for (int i = 0; i < n; i++) {
        items[i].ratio = (double)items[i].value / items[i].weight;
    }

    // Sort items by ratio descending
    qsort(items, n, sizeof(Item), cmp);

    double totalValue = 0.0;
    int remaining = capacity;

    for (int i = 0; i < n; i++) {
        if (items[i].weight <= remaining) {
            // Take whole item
            totalValue += items[i].value;
            remaining -= items[i].weight;
        } else {
            // Take fraction of item
            totalValue += items[i].ratio * remaining;
            break; // Knapsack full
        }
    }
    return totalValue;
}

int main() {
    Item items[] = {{60, 10, 0}, {100, 20, 0}, {120, 30, 0}};
    int n = sizeof(items) / sizeof(items[0]);
    int capacity = 50;

    double maxValue = fractionalKnapsack(items, n, capacity);
    printf("Maximum value in knapsack = %.2f\n", maxValue);
    return 0;
}
items[i].ratio = (double)items[i].value / items[i].weight;
Calculate value per weight ratio for sorting priority
qsort(items, n, sizeof(Item), cmp);
Sort items by descending ratio to pick best value first
if (items[i].weight <= remaining) {
Check if whole item fits in remaining capacity
totalValue += items[i].value;
Add full item value to total
remaining -= items[i].weight;
Reduce remaining capacity by item weight
totalValue += items[i].ratio * remaining;
Add fractional value for partial item to fill knapsack
break;
Stop after filling knapsack with fraction
OutputSuccess
Maximum value in knapsack = 240.00
Complexity Analysis
Time: O(n log n) because we sort n items by ratio, then iterate once over them
Space: O(n) for storing items and their ratios
vs Alternative: Better than trying all subsets (exponential), greedy sorting gives optimal fractional solution efficiently
Edge Cases
Empty items list
Returns 0 value since no items to take
DSA C
for (int i = 0; i < n; i++) {
Knapsack capacity zero
Returns 0 value since no capacity to take items
DSA C
int remaining = capacity;
Item with zero weight (invalid)
Division by zero avoided by input validation (not shown here), else program may crash
DSA C
items[i].ratio = (double)items[i].value / items[i].weight;
When to Use This Pattern
When you need to maximize value with divisible items and limited capacity, use the fractional knapsack greedy approach sorting by value/weight ratio.
Common Mistakes
Mistake: Sorting items by value or weight alone instead of value/weight ratio
Fix: Sort items by value divided by weight to prioritize best value per unit weight
Mistake: Not taking fractional part when item doesn't fully fit
Fix: Calculate fraction of item to fill remaining capacity and add proportional value
Summary
Find maximum value by taking whole or fractional parts of items to fill knapsack capacity.
Use when items can be divided and you want the best value for limited capacity.
Sort items by value/weight ratio and take as much as possible from highest ratio first.