{"id":183,"date":"2026-11-13T12:33:50","date_gmt":"2026-11-13T04:33:50","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fractional-knapsack-packs-by-value-density\/"},"modified":"2026-11-13T21:52:00","modified_gmt":"2026-11-13T13:52:00","slug":"how-fractional-knapsack-packs-by-value-density","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fractional-knapsack-packs-by-value-density\/","title":{"rendered":"How fractional knapsack packs by value density"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>fractional knapsack<\/strong> 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 <strong>O(n log n)<\/strong>. The scan is linear.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Discard or specially handle a nonpositive weight. Compute value\/weight for each remaining item.<\/li>\n<li>Sort by nonincreasing density.<\/li>\n<li>Walk the list. If the item fits entirely, take it and subtract its weight from the remaining capacity.<\/li>\n<li>When an item does not fit, take the fraction remaining capacity divided by its weight, add that fraction times its value, and stop.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the same sort is used for indivisible items<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Take whole items in density order and skip anything that does not fit<\/td>\n<td>That is a 0-1 heuristic. A skipped heavy item can be worth more than the dense crumbs that filled the bag.<\/td>\n<\/tr>\n<tr>\n<td>Split several items instead of only the marginal one<\/td>\n<td>The optimum takes a prefix of the density order whole and splits at most one item. Splitting an earlier item wastes density.<\/td>\n<\/tr>\n<tr>\n<td>Sort by value alone<\/td>\n<td>A valuable heavy item can be worse per unit than a lighter one. The ratio is the key.<\/td>\n<\/tr>\n<tr>\n<td>Allow a zero-weight positive-value item into the ratio<\/td>\n<td>The density is undefined or infinite. The item breaks the ordering assumption.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Indivisible items are 0-1 knapsack. Repeatable whole items are unbounded knapsack. Fractional knapsack is the density greedy algorithm.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>240<\/code>.<\/p>\n\n<pre><code class=\"language-python\">items = [(10, 60), (20, 100), (30, 120)]\nitems.sort(key=lambda it: it[1] \/ it[0], reverse=True)\ncap = 50\nvalue = 0.0\nfor weight, val in items:\n    if weight &lt;= cap:\n        value += val\n        cap -= weight\n    else:\n        value += val * (cap \/ weight)\n        break\nassert abs(value - 240) &lt; 1e-9\nprint(int(value))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for fractional knapsack returns the density order and the single fractional item that fills the last gap. <strong>Fractional knapsack maximizes value by taking items in order of value per weight and splitting at most one item to fill the remaining capacity.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,37],"tags":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-greedy"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":582,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/183\/revisions\/582"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}