Data Structures & Algorithms

Table of Contents

Fractional knapsack

Problem

take as much as as possible from best benefit per weight ratio.

(Benefit,Weight) = {(12, 4), (10, 6), (8, 5), (11, 7), (14, 3), (7, 1), (9, 6)} Total Weight (W) = 18

Solution

Build a table, and calculate the B/W ratio for each entry:

SBWR=B/W
11243
2106$1 \frac{4}{6}$
385$1 \frac{3}{5}$
4117$1 \frac{4}{7}$
5143$1 \frac{2}{2}$
6717
796$1 \frac{1}{2}$

W = 18.

Start taking by highest ratio. Formula:

$B_{\text{S with highest ratio}}\times\frac{\text{items taken}}{weight}$

Item$B_6\times\frac{1}{1}$$B_5\times\frac{3}{3}$$B_1\times\frac{4}{4}$$B_2\times\frac{6}{6}$$B_3\times\frac{4}{5}$
Cum.totalweight1481418

For total profits, calculate the addition of all items.

Complexity

if S is a heap-based priority queue, with highest benefit per weight value having highest priority,

then in O(nlogn)