How fractional knapsack packs by value density

Short answer: The fractional knapsack problem allows any fraction of an item. Sort by value per unit weight and take items from the densest until the next item does not fit. Fill the remaining capacity with a fraction of that item. The greedy choice is optimal for the fractional problem and is not optimal for the 0-1 problem, where cutting an item is forbidden. The sort is O(n log n). The scan is linear.


What the problem actually asks

A capacity W and a set of items with weight and value, with fractions allowed, is a continuous choice. The last unit of capacity should be spent on the densest remaining item, because swapping that unit for a unit of a rarer item would decrease the value. Repeating the argument empties the densest items first.

The 0-1 constraint removes the fraction. The same density order can fill the bag with a light dense item and leave no room for a slightly less dense item that would have used the whole capacity better. The fractional proof ends at the moment cutting is allowed.


What the algorithm guarantees

The density order yields the maximum value among all fractional fillings. Time is O(n log n). Extra memory is the sorted list. Items with zero weight and positive value are degenerate: if value is positive and weight is zero, the value is unbounded or the item is taken for free, and the problem statement should say which. Negative weights or values are outside the standard statement.

The algorithm returns both the value and the fraction taken of each item. A solver that returns only the value of the whole items has solved a different problem and may not be optimal for either.


The working steps

  1. Discard or specially handle a nonpositive weight. Compute value/weight for each remaining item.
  2. Sort by nonincreasing density.
  3. Walk the list. If the item fits entirely, take it and subtract its weight from the remaining capacity.
  4. When an item does not fit, take the fraction remaining capacity divided by its weight, add that fraction times its value, and stop.
  5. Do not use this fraction step on a 0-1 instance. Solve that with the downward knapsack DP, or state that the fraction is infeasible.

The optimality proof uses fractions. 0-1 knapsack is pseudo-polynomial DP. Unbounded knapsack, which repeats whole items, is another DP. Fractional knapsack is the density sort plus one fractional item.


Approaches that look simpler and fail

Approach Why it fails when the same sort is used for indivisible items
Take whole items in density order and skip anything that does not fit That is a 0-1 heuristic. A skipped heavy item can be worth more than the dense crumbs that filled the bag.
Split several items instead of only the marginal one The optimum takes a prefix of the density order whole and splits at most one item. Splitting an earlier item wastes density.
Sort by value alone A valuable heavy item can be worse per unit than a lighter one. The ratio is the key.
Allow a zero-weight positive-value item into the ratio The density is undefined or infinite. The item breaks the ordering assumption.

Indivisible items are 0-1 knapsack. Repeatable whole items are unbounded knapsack. Fractional knapsack is the density greedy algorithm.


A working example

This Python fragment packs a fractional knapsack by value per weight and splits only the marginal item. Weights 10, 20, 30 and values 60, 100, 120 into capacity 50 give 240.

items = [(10, 60), (20, 100), (30, 120)]
items.sort(key=lambda it: it[1] / it[0], reverse=True)
cap = 50
value = 0.0
for weight, val in items:
    if weight <= cap:
        value += val
        cap -= weight
    else:
        value += val * (cap / weight)
        break
assert abs(value - 240) < 1e-9
print(int(value))

Where it is used

Fractional knapsack is the continuous loading problem: grain, liquid, or any cargo that can be split, taken in order of value per weight. The greedy choice is optimal there. The 0-1 knapsack, where an item is whole, needs the capacity row.


When you can drop this algorithm

Drop the density sort as an exact solver when items cannot be cut. Use the 0-1 DP. Keep the sort when fractions are real and the remaining capacity takes a prefix of the marginal item.


Why this stays published

We publish this so a search for fractional knapsack returns the density order and the single fractional item that fills the last gap. Fractional knapsack maximizes value by taking items in order of value per weight and splitting at most one item to fill the remaining capacity.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *