Fractional Knapsack- explanation

971 Views Asked by At

The following algorithm is given:

Algorithm FractionalKnapsack(S,W):
Input: Set S of items, such that each item i∈S has a positive benefit b_i and a positive 
weight w_i; positive maximum total weight W
Output: Amount x_i of each item i ∈ S that maximizes the total benefit while not exceeding 
the maximum total weight W. 
for each item i∈S
    x_i<-0
    v_i<-b_i/w_i // value index of item i
w<-0
while w<W do
      remove from S an item i with highest value index // greedy choice
      a<-min{w_i,W-w} // more than W-w causes a weight overflow
      x_i<-a
      w<-w+a

According to my lecture notes:

To see that the fractional knapsack problem satisfies the greedy-choice property, suppose that there are two items $i$ and $j$ such that $$x_i<w_i,\ \ \ x_j>0, \ \ \ \text{ and } v_i<v_j$$

Let $y=\min \{ w_i-x_i, x_j\}$.

We could then replace an amount $y$ of item $j$ with an equal amount of item $i$, thus increasing the total benefit without changing the total weight.

Therefore, we can correctly compute optimal amounts for the items by greedily choosing items with the largest value index.

Could you explain me the idea of the proof?

Why does the following hold?

We could then replace an amount $y$ of item $j$ with an equal amount of item $i$, thus increasing the total benefit without changing the total weight.

I haven't understood it.

EDIT: Why do we deduce from the following:

We could then replace an amount $y$ of item $j$ with an equal amount of item $i$, thus increasing the total benefit without changing the total weight.

that the fractional knapsack problem satisfies the greedy-choice property?

1

There are 1 best solutions below

20
On BEST ANSWER

It holds as, if the case assumed in the text (for contradiction) happens for a valid solution $x_1,\dots,x_n$ of the fractional Knapsack (where $x_k$ represents the fraction of the weight $w_k$ added to the Knpasack in that solution), then the following holds. You have a quantity $y>0$ of item $j$ in your solution (as $y \leq x_j$ by definition) and can still add at least a quantity $y$ of item $i$ to your knapsack before "using all the available quantity" of item $i$) (as $y \leq w_i - x_i$ by definition). So you can swap the quantity $y$ of item $j$ you have for the same quantity of item $i$ (formally decrease $x_j$ by $y$, and increase $x_i$ by $y$), this will still satisfy the constraints.

But the new overall value $V^\prime$ will become greater than the previous value $V$, as $V^\prime = V - y\cdot v_j +y\cdot v_i$; and $v_i > v_j$ (and $y>0$) by assumption.