--- comments: true --- # 15.2   Fractional knapsack problem !!! question Given $n$ items, the weight of the $i$-th item is $wgt[i-1]$ and its value is $val[i-1]$, and a knapsack with a capacity of $cap$. Each item can be chosen only once, **but a part of the item can be selected, with its value calculated based on the proportion of the weight chosen**, what is the maximum value of the items in the knapsack under the limited capacity? An example is shown in Figure 15-3. ![Example data of the fractional knapsack problem](fractional_knapsack_problem.assets/fractional_knapsack_example.png){ class="animation-figure" }

Figure 15-3   Example data of the fractional knapsack problem

The fractional knapsack problem is very similar overall to the 0-1 knapsack problem, involving the current item $i$ and capacity $c$, aiming to maximize the value within the limited capacity of the knapsack. The difference is that, in this problem, only a part of an item can be chosen. As shown in Figure 15-4, **we can arbitrarily split the items and calculate the corresponding value based on the weight proportion**. 1. For item $i$, its value per unit weight is $val[i-1] / wgt[i-1]$, referred to as the unit value. 2. Suppose we put a part of item $i$ with weight $w$ into the knapsack, then the value added to the knapsack is $w \times val[i-1] / wgt[i-1]$. ![Value per unit weight of the item](fractional_knapsack_problem.assets/fractional_knapsack_unit_value.png){ class="animation-figure" }

Figure 15-4   Value per unit weight of the item

### 1.   Greedy strategy determination Maximizing the total value of the items in the knapsack essentially means maximizing the value per unit weight. From this, the greedy strategy shown in Figure 15-5 can be deduced. 1. Sort the items by their unit value from high to low. 2. Iterate over all items, **greedily choosing the item with the highest unit value in each round**. 3. If the remaining capacity of the knapsack is insufficient, use part of the current item to fill the knapsack. ![Greedy strategy of the fractional knapsack problem](fractional_knapsack_problem.assets/fractional_knapsack_greedy_strategy.png){ class="animation-figure" }

Figure 15-5   Greedy strategy of the fractional knapsack problem

### 2.   Code implementation We have created an `Item` class in order to sort the items by their unit value. We loop and make greedy choices until the knapsack is full, then exit and return the solution: === "Python" ```python title="fractional_knapsack.py" class Item: """物品""" def __init__(self, w: int, v: int): self.w = w # 物品重量 self.v = v # 物品价值 def fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int: """分数背包:贪心""" # 创建物品列表,包含两个属性:重量、价值 items = [Item(w, v) for w, v in zip(wgt, val)] # 按照单位价值 item.v / item.w 从高到低进行排序 items.sort(key=lambda item: item.v / item.w, reverse=True) # 循环贪心选择 res = 0 for item in items: if item.w <= cap: # 若剩余容量充足,则将当前物品整个装进背包 res += item.v cap -= item.w else: # 若剩余容量不足,则将当前物品的一部分装进背包 res += (item.v / item.w) * cap # 已无剩余容量,因此跳出循环 break return res ``` === "C++" ```cpp title="fractional_knapsack.cpp" /* 物品 */ class Item { public: int w; // 物品重量 int v; // 物品价值 Item(int w, int v) : w(w), v(v) { } }; /* 分数背包:贪心 */ double fractionalKnapsack(vector &wgt, vector &val, int cap) { // 创建物品列表,包含两个属性:重量、价值 vector items; for (int i = 0; i < wgt.size(); i++) { items.push_back(Item(wgt[i], val[i])); } // 按照单位价值 item.v / item.w 从高到低进行排序 sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; }); // 循环贪心选择 double res = 0; for (auto &item : items) { if (item.w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v; cap -= item.w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += (double)item.v / item.w * cap; // 已无剩余容量,因此跳出循环 break; } } return res; } ``` === "Java" ```java title="fractional_knapsack.java" /* 物品 */ class Item { int w; // 物品重量 int v; // 物品价值 public Item(int w, int v) { this.w = w; this.v = v; } } /* 分数背包:贪心 */ double fractionalKnapsack(int[] wgt, int[] val, int cap) { // 创建物品列表,包含两个属性:重量、价值 Item[] items = new Item[wgt.length]; for (int i = 0; i < wgt.length; i++) { items[i] = new Item(wgt[i], val[i]); } // 按照单位价值 item.v / item.w 从高到低进行排序 Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w))); // 循环贪心选择 double res = 0; for (Item item : items) { if (item.w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v; cap -= item.w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += (double) item.v / item.w * cap; // 已无剩余容量,因此跳出循环 break; } } return res; } ``` === "C#" ```csharp title="fractional_knapsack.cs" /* 物品 */ class Item(int w, int v) { public int w = w; // 物品重量 public int v = v; // 物品价值 } /* 分数背包:贪心 */ double FractionalKnapsack(int[] wgt, int[] val, int cap) { // 创建物品列表,包含两个属性:重量、价值 Item[] items = new Item[wgt.Length]; for (int i = 0; i < wgt.Length; i++) { items[i] = new Item(wgt[i], val[i]); } // 按照单位价值 item.v / item.w 从高到低进行排序 Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w)); // 循环贪心选择 double res = 0; foreach (Item item in items) { if (item.w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v; cap -= item.w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += (double)item.v / item.w * cap; // 已无剩余容量,因此跳出循环 break; } } return res; } ``` === "Go" ```go title="fractional_knapsack.go" /* 物品 */ type Item struct { w int // 物品重量 v int // 物品价值 } /* 分数背包:贪心 */ func fractionalKnapsack(wgt []int, val []int, cap int) float64 { // 创建物品列表,包含两个属性:重量、价值 items := make([]Item, len(wgt)) for i := 0; i < len(wgt); i++ { items[i] = Item{wgt[i], val[i]} } // 按照单位价值 item.v / item.w 从高到低进行排序 sort.Slice(items, func(i, j int) bool { return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w) }) // 循环贪心选择 res := 0.0 for _, item := range items { if item.w <= cap { // 若剩余容量充足,则将当前物品整个装进背包 res += float64(item.v) cap -= item.w } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += float64(item.v) / float64(item.w) * float64(cap) // 已无剩余容量,因此跳出循环 break } } return res } ``` === "Swift" ```swift title="fractional_knapsack.swift" /* 物品 */ class Item { var w: Int // 物品重量 var v: Int // 物品价值 init(w: Int, v: Int) { self.w = w self.v = v } } /* 分数背包:贪心 */ func fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double { // 创建物品列表,包含两个属性:重量、价值 var items = zip(wgt, val).map { Item(w: $0, v: $1) } // 按照单位价值 item.v / item.w 从高到低进行排序 items.sort { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) } // 循环贪心选择 var res = 0.0 var cap = cap for item in items { if item.w <= cap { // 若剩余容量充足,则将当前物品整个装进背包 res += Double(item.v) cap -= item.w } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += Double(item.v) / Double(item.w) * Double(cap) // 已无剩余容量,因此跳出循环 break } } return res } ``` === "JS" ```javascript title="fractional_knapsack.js" /* 物品 */ class Item { constructor(w, v) { this.w = w; // 物品重量 this.v = v; // 物品价值 } } /* 分数背包:贪心 */ function fractionalKnapsack(wgt, val, cap) { // 创建物品列表,包含两个属性:重量、价值 const items = wgt.map((w, i) => new Item(w, val[i])); // 按照单位价值 item.v / item.w 从高到低进行排序 items.sort((a, b) => b.v / b.w - a.v / a.w); // 循环贪心选择 let res = 0; for (const item of items) { if (item.w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v; cap -= item.w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += (item.v / item.w) * cap; // 已无剩余容量,因此跳出循环 break; } } return res; } ``` === "TS" ```typescript title="fractional_knapsack.ts" /* 物品 */ class Item { w: number; // 物品重量 v: number; // 物品价值 constructor(w: number, v: number) { this.w = w; this.v = v; } } /* 分数背包:贪心 */ function fractionalKnapsack(wgt: number[], val: number[], cap: number): number { // 创建物品列表,包含两个属性:重量、价值 const items: Item[] = wgt.map((w, i) => new Item(w, val[i])); // 按照单位价值 item.v / item.w 从高到低进行排序 items.sort((a, b) => b.v / b.w - a.v / a.w); // 循环贪心选择 let res = 0; for (const item of items) { if (item.w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v; cap -= item.w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += (item.v / item.w) * cap; // 已无剩余容量,因此跳出循环 break; } } return res; } ``` === "Dart" ```dart title="fractional_knapsack.dart" /* 物品 */ class Item { int w; // 物品重量 int v; // 物品价值 Item(this.w, this.v); } /* 分数背包:贪心 */ double fractionalKnapsack(List wgt, List val, int cap) { // 创建物品列表,包含两个属性:重量、价值 List items = List.generate(wgt.length, (i) => Item(wgt[i], val[i])); // 按照单位价值 item.v / item.w 从高到低进行排序 items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w)); // 循环贪心选择 double res = 0; for (Item item in items) { if (item.w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v; cap -= item.w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += item.v / item.w * cap; // 已无剩余容量,因此跳出循环 break; } } return res; } ``` === "Rust" ```rust title="fractional_knapsack.rs" /* 物品 */ struct Item { w: i32, // 物品重量 v: i32, // 物品价值 } impl Item { fn new(w: i32, v: i32) -> Self { Self { w, v } } } /* 分数背包:贪心 */ fn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 { // 创建物品列表,包含两个属性:重量、价值 let mut items = wgt .iter() .zip(val.iter()) .map(|(&w, &v)| Item::new(w, v)) .collect::>(); // 按照单位价值 item.v / item.w 从高到低进行排序 items.sort_by(|a, b| { (b.v as f64 / b.w as f64) .partial_cmp(&(a.v as f64 / a.w as f64)) .unwrap() }); // 循环贪心选择 let mut res = 0.0; for item in &items { if item.w <= cap { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v as f64; cap -= item.w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += item.v as f64 / item.w as f64 * cap as f64; // 已无剩余容量,因此跳出循环 break; } } res } ``` === "C" ```c title="fractional_knapsack.c" /* 物品 */ typedef struct { int w; // 物品重量 int v; // 物品价值 } Item; /* 分数背包:贪心 */ float fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) { // 创建物品列表,包含两个属性:重量、价值 Item *items = malloc(sizeof(Item) * itemCount); for (int i = 0; i < itemCount; i++) { items[i] = (Item){.w = wgt[i], .v = val[i]}; } // 按照单位价值 item.v / item.w 从高到低进行排序 qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity); // 循环贪心选择 float res = 0.0; for (int i = 0; i < itemCount; i++) { if (items[i].w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += items[i].v; cap -= items[i].w; } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += (float)cap / items[i].w * items[i].v; cap = 0; break; } } free(items); return res; } ``` === "Kotlin" ```kotlin title="fractional_knapsack.kt" /* 物品 */ class Item( val w: Int, // 物品 val v: Int // 物品价值 ) /* 分数背包:贪心 */ fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double { // 创建物品列表,包含两个属性:重量、价值 var cap = c val items = arrayOfNulls(wgt.size) for (i in wgt.indices) { items[i] = Item(wgt[i], _val[i]) } // 按照单位价值 item.v / item.w 从高到低进行排序 items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) } // 循环贪心选择 var res = 0.0 for (item in items) { if (item!!.w <= cap) { // 若剩余容量充足,则将当前物品整个装进背包 res += item.v cap -= item.w } else { // 若剩余容量不足,则将当前物品的一部分装进背包 res += item.v.toDouble() / item.w * cap // 已无剩余容量,因此跳出循环 break } } return res } ``` === "Ruby" ```ruby title="fractional_knapsack.rb" [class]{Item}-[func]{} [class]{}-[func]{fractional_knapsack} ``` === "Zig" ```zig title="fractional_knapsack.zig" [class]{Item}-[func]{} [class]{}-[func]{fractionalKnapsack} ``` ??? pythontutor "Code Visualization"
Apart from sorting, in the worst case, the entire list of items needs to be traversed, **hence the time complexity is $O(n)$**, where $n$ is the number of items. Since an `Item` object list is initialized, **the space complexity is $O(n)$**. ### 3.   Correctness proof Using proof by contradiction. Suppose item $x$ has the highest unit value, and some algorithm yields a maximum value `res`, but the solution does not include item $x$. Now remove a unit weight of any item from the knapsack and replace it with a unit weight of item $x$. Since the unit value of item $x$ is the highest, the total value after replacement will definitely be greater than `res`. **This contradicts the assumption that `res` is the optimal solution, proving that the optimal solution must include item $x$**. For other items in this solution, we can also construct the above contradiction. Overall, **items with greater unit value are always better choices**, proving that the greedy strategy is effective. As shown in Figure 15-6, if the item weight and unit value are viewed as the horizontal and vertical axes of a two-dimensional chart respectively, the fractional knapsack problem can be transformed into "seeking the largest area enclosed within a limited horizontal axis range". This analogy can help us understand the effectiveness of the greedy strategy from a geometric perspective. ![Geometric representation of the fractional knapsack problem](fractional_knapsack_problem.assets/fractional_knapsack_area_chart.png){ class="animation-figure" }

Figure 15-6   Geometric representation of the fractional knapsack problem