Problem 5: Fractional Knapsack Problem
A thief is given the choice of n objects to steal, but only has one knapsack with a capacity of taking
M weight. Each object $i$ has weight $w_i$, and profit $p_i$.
(a) First, suppose that the objects are divisible (e.g., the thief is in a cheese shop and the items
are rolls of cheese that can be cut). For each object $i$, if a fraction $x_i$, $0 \le x_i \le 1$ (taking
$x_i = 1$ would be taking the entire object) is placed in the knapsack, then the profit earned
is $p_i x_i$. Come up with an efficient greedy algorithm for maximizing the profit of the thief.
Prove the correctness and running time.
(b) Now, suppose the items cannot be taken fractionally. In other words, the thief can either
take an entire item or leave it behind. Suppose we also know the following: the order of these
items when sorted by increasing weight is the same as their order when sorted by decreasing
value. Give a greedy algorithm to find an optimal solution to this variant of the knapsack
problem. Prove the correctness and running time.