CPKit
v1.6.1

Fractional Knapsack

GreedyEasy

Greedily select commodities density ratios to maximize knapsack profit values.

Alt+Shift+F

Configuration Panel

Sorted Density Items Filling Audit

Time Complexity

O(N log N) sorting + O(N) sweep

Space Complexity

O(N) items array storage
Conceptual Overview

The Fractional Knapsack problem permits items to be broken down fractionally. Given items of weights and values, fill a knapsack of capacity W to maximize value.

Greedy Choice Property

Always pick the item with the highest value-per-unit-weight density (value / weight) next. If the capacity is filled before exhausting the item, take a fraction.

Optimal Substructure

An optimal solution to fractional knapsack contains the highest density item plus the optimal solution to the remaining subproblem with decreased capacity.

Proof Idea / Correction

Proof by contradiction: if we select a lower-density item over a higher-density one, swapping the lower-density fraction with the higher-density one yields a strictly higher profit.

Source: CP-Algorithms greedy reference