Loading learning content...
Consider two versions of the same challenge:
Scenario A (Discrete): You're buying gold bars to invest in. Each bar weighs exactly 1 kilogram and costs a specific price. You cannot buy half a bar — it's all or nothing.
Scenario B (Fractional): You're buying gold dust by weight. You can purchase exactly as much as you want — 1kg, 0.7kg, or even 0.001kg.
These two scenarios have fundamentally different optimization characteristics. The first requires combinatorial reasoning — which discrete bars maximize value? The second admits a beautifully simple solution: buy the cheapest gold per gram first, then the next cheapest, until your budget is exhausted.
The Fractional Knapsack problem falls into Scenario B. The ability to take fractions of items completely transforms the mathematical structure of the problem, enabling a greedy algorithm that is both simple and provably optimal.
By the end of this page, you will understand: (1) what it means mathematically and algorithmically to take fractions, (2) how partial selections are computed, (3) why fractionality eliminates the need for backtracking or combinatorial search, and (4) the precise conditions under which taking fractions is meaningful in real problems.
In the Fractional Knapsack problem, we associate with each item i a decision variable xᵢ representing the fraction of that item we take.
Formal Definition:
For item i with weight wᵢ and value vᵢ:
Weight and Value of Fractional Selection:
This linear scaling is crucial. If you take half of an item (xᵢ = 0.5), you consume half its weight and gain half its value. The proportionality is exact — there are no economies or diseconomies of scale.
The assumption that value scales linearly with fraction taken (value of 50% = 50% of total value) is essential. If there were setup costs, minimum viable quantities, or diminishing returns, the problem would become more complex. Fractional Knapsack assumes perfect divisibility with perfect linear value scaling.
Computing Fractional Contributions:
Let's make this concrete with an example item:
What happens at different fractions?
| Fraction (xᵢ) | Weight Used | Value Gained | Density Check |
|---|---|---|---|
| 0.0 | 0 kg | $0 | — |
| 0.25 | 2.5 kg | $50 | 50/2.5 = $20/kg ✓ |
| 0.5 | 5 kg | $100 | 100/5 = $20/kg ✓ |
| 0.75 | 7.5 kg | $150 | 150/7.5 = $20/kg ✓ |
| 1.0 | 10 kg | $200 | 200/10 = $20/kg ✓ |
Key Observation: The value density remains constant regardless of the fraction taken. This invariance is why the greedy approach works — each incremental unit of this item has the same 'return rate'.
The ability to take fractions fundamentally changes the optimization landscape. To understand why, we must examine what goes wrong in the discrete (0/1) case and how fractionality solves it.
The Discrete Problem — Wasted Capacity:
Consider 0/1 Knapsack with capacity W = 10 and two items:
Greedy by density selects A first (density 5 > 4). After taking A, remaining capacity = 3. Item B needs 5, so it can't fit. Greedy gets value = 35, with 3 units of wasted capacity.
But the optimal 0/1 solution might be to skip A and take only B? No — value 20 < 35. Actually, greedy wins here. But consider:
Greedy takes A (value 30), remaining capacity = 4, B doesn't fit. Total = 30. Optimal: Take B only? No, 24 < 30. But what if there's a third item that fits in 4?
With more items, the discrete case can have greedy failures. The point: discrete selection creates 'leftovers' that may be suboptimally filled.
In Fractional Knapsack, you NEVER have leftover capacity (unless total item weight is less than W). After taking high-density items fully, you fill remaining capacity with a precise fraction of the next item. Every unit of capacity is utilized at the best possible rate.
The Fractional Solution — Perfect Packing:
With the same items (A: w=6, v=30; B: w=5, v=24; W=10):
Greedy by density:
Total: 30 + 19.2 = 49.2
Every unit of the 10kg capacity is filled. The last 4kg is filled with the highest-density available material (which happens to be part of B).
The Core Insight:
Fractionality ensures that each unit of capacity is allocated to the currently-highest density material. Since density represents value per unit weight, this means each marginal unit of capacity is earning the best possible return. No reallocation can improve the solution — it is already optimal.
This is why greedy works: every local decision (take highest density available) is also globally optimal when fractions are allowed.
Let's formalize the greedy algorithm for Fractional Knapsack. The procedure is elegant and efficient.
Algorithm: Fractional Knapsack Greedy
Input: n items with (wᵢ, vᵢ), capacity W
Output: fractions x₁, x₂, ..., xₙ and total value
1. Compute value density ρᵢ = vᵢ/wᵢ for each item i
2. Sort items in decreasing order of ρᵢ
3. Initialize: remaining_capacity = W, total_value = 0
4. For each item i in sorted order:
a. If wᵢ ≤ remaining_capacity:
- Take all of item i: xᵢ = 1
- remaining_capacity -= wᵢ
- total_value += vᵢ
b. Else if remaining_capacity > 0:
- Take fraction: xᵢ = remaining_capacity / wᵢ
- total_value += xᵢ * vᵢ
- remaining_capacity = 0
- Break (knapsack is full)
c. Else:
- xᵢ = 0 (no room left)
5. Return x₁...xₙ, total_value
Step-by-Step Trace — Example:
Items: A (w=10, v=60, ρ=6), B (w=20, v=100, ρ=5), C (w=30, v=120, ρ=4) Capacity W = 50
After Sorting by Density: A, B, C
Iteration 1 (Item A):
Iteration 2 (Item B):
Iteration 3 (Item C):
Final Solution:
Notice how the fractional selection of C perfectly fills the remaining 20kg. We didn't waste any capacity, and we didn't have to backtrack or reconsider earlier decisions. The greedy approach naturally produces an exact fill at maximum value.
When we reach an item that doesn't fully fit, we must compute the precise fraction to take. This calculation is straightforward but worth examining carefully.
The Fractional Fill Calculation:
Given:
The fraction of item i to take is:
xᵢ = R / wᵢ
The value gained is:
xᵢ × vᵢ = (R / wᵢ) × vᵢ = R × (vᵢ / wᵢ) = R × ρᵢ
This last form is illuminating: the value gained equals the remaining capacity times the item's value density. We're essentially 'spending' R units of capacity at the item's exchange rate.
Numerical Precision Considerations:
In practice, fractional calculations may involve floating-point numbers. Consider:
Implementation Tips:
Alternative Perspective — Density Interpretation:
Another way to think about the fractional fill:
This matches exactly with looking at density as an 'exchange rate' between capacity and value.
In the greedy algorithm, at most one item is taken fractionally. All items with higher density are taken fully (they fit), all items with lower density are taken not at all (capacity exhausted). Only the 'boundary' item — the one we're processing when capacity runs out — is taken fractionally.
Not all problems permit fractional selection. Understanding when fractions are meaningful helps identify problems that fit the Fractional Knapsack model.
Characteristics of Fractional Problems:
Examples Where Fractions Are Valid:
| Domain | Item | Why Fractions Work |
|---|---|---|
| Finance | Investment capital | Can invest any dollar amount |
| Shipping | Bulk commodities | Load any tonnage of ore |
| Computing | CPU allocation | Assign fractional cores/threads |
| Time | Work hours | Work 2.5 hours on a task |
| Bandwidth | Data streams | Allocate 37.5% of bandwidth |
Half a car is not a car. Half a software license may be meaningless. An unfinished construction project may have zero value. When items have 'integrity' — where partial completion yields zero or nonlinear value — the 0/1 Knapsack model applies, and greedy fails.
The Spectrum of Divisibility:
Real problems often fall on a spectrum:
Modeling Decision:
When facing a real problem, ask: "If I take 73.2% of this item, does that make sense operationally?" If yes, model as Fractional Knapsack. If no, model as 0/1.
To truly understand why the greedy algorithm works, we must identify the invariant — a property that holds after each step and guarantees eventual optimality.
The Greedy Knapsack Invariant:
At every step, the current partial solution is optimal for the capacity used so far.
Intuitive Argument:
Consider the first unit of capacity. Which item should 'occupy' it? Since we can take fractions, we should allocate this unit to the item with highest value density — say, item A with ρ_A = 10.
Now consider the second unit. It should go to the highest-density item available. If A still has supply, it stays at A. If A is exhausted, move to the second-highest density item.
This reasoning extends to every unit of capacity. Each incremental allocation goes to the best available 'rate of return.' Since fractions allow perfect subdivision, every unit of capacity earns the maximum possible value — no reallocation can improve the outcome.
Imagine filling the knapsack one gram at a time. At each gram, ask: 'What's the highest value-per-gram I can get right now?' Always choose that option. Fractionality means you can always take exactly one gram of whatever is best. This myopic process is globally optimal!
Why Discrete Knapsack Breaks This:
In 0/1 Knapsack, you can't take items 'one gram at a time.' You must commit to entire items. The first gram of a large item commits you to potentially many grams you might later regret.
Example:
Greedy takes A (ρ=2): weight=100, value=200. Done.
Optimal 0/1: Take B+C: weight=100, value=175. Wait, that's worse!
Hmm, actually greedy wins here. Let me construct a better counterexample:
Greedy takes A (ρ=2): weight=50, value=100. Remaining=10. Nothing fits. Optimal 0/1: Take B+C: weight=60, value=109.
Greedy: 100 vs. Optimal: 109. The discrete constraint causes failures.
A powerful way to understand Fractional Knapsack is to visualize it as filling a container with liquids of varying densities.
The Liquid Analogy:
Imagine:
Optimal Strategy:
The tank ends up stratified: the densest liquid at bottom, less dense liquid above, with a precise fractional pour from one drum to top off.
Graphical Interpretation:
Plot items on a graph:
Items become horizontal bars at their density level, with width equal to their weight. To find total value for capacity W:
The area under this 'staircase' curve (sorted by decreasing density) represents total value. Any other ordering would have less area for the same width — proving greedy optimality graphically.
If we had infinitely many infinitesimally small items, the 'staircase' becomes a smooth decreasing curve. The optimal strategy remains the same: integrate from highest density down until capacity is reached. Fractional Knapsack is essentially this continuous limit applied to finitely many items.
We've thoroughly explored what it means to take fractions in the Fractional Knapsack problem and why this capability is the key to greedy optimality.
What's Next:
Now that we understand fractional selection, the next page formally presents the greedy by value-to-weight ratio algorithm. We'll implement it step by step, analyze its time complexity, and provide a rigorous proof of its optimality using the exchange argument.
You now understand how fractional selection works in the Fractional Knapsack problem: the mathematics of partial selection, why it enables greedy solutions, how to compute exact fractions, and when fractional models are appropriate. Next, we'll formalize the greedy algorithm and prove its optimality.