mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-11 19:30:59 +00:00
build
This commit is contained in:
@@ -6,7 +6,7 @@ comments: true
|
||||
|
||||
!!! 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 below.
|
||||
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.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -14,7 +14,7 @@ comments: true
|
||||
|
||||
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 the Figure 15-4 , **we can arbitrarily split the items and calculate the corresponding value based on the weight proportion**.
|
||||
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]$.
|
||||
@@ -25,7 +25,7 @@ The difference is that, in this problem, only a part of an item can be chosen. A
|
||||
|
||||
### 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 below can be deduced.
|
||||
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**.
|
||||
@@ -526,7 +526,7 @@ Now remove a unit weight of any item from the knapsack and replace it with a uni
|
||||
|
||||
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 the 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.
|
||||
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.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user