📜 ⬆️ ⬇️

A little bit about packing backpack

Many people know the so-called task of packing a backpack.
In short, let me remind you: from a pile of items you need to choose such that the backpack was crammed to the eyeballs and could still be dragged away.
More formally, it is necessary from this set A of pairs of numbers a (i) b (i), to choose such that the sum of the numbers does not exceed the predetermined S, and the sum of the numbers b is maximum. Σa (n) ≤ S, Σb (n) = max.

Initial set:

Σ
a31425263222823one9163
beleven12fivethirty3125nineteen273233225


The last column shows the total weight Σa of all items and their total value Σb. Asked S (load capacity) should not exceed Σa, otherwise the solution is trivial - we can carry everything. Given these limitations, using the total cost of Σb, we can estimate how much the total cost of the solution obtained differs from the absolute maximum.
')
There are several ways to solve this problem, they are described in Wikipedia.
We are interested in the "greedy" algorithm.
It consists in calculating for each pair the value d = a / b, by which the pairs are sorted and then selected.

Set indicating the value of d:

Σ
a31425263222823one9163
beleven12fivethirty3125nineteen273233225
d3.670.860.21.150.9712.50.681.1732.03.67


Sorted by d set
Σ
aone239232632142825163
b3225eleven3327thirty3112nineteenfive225
d32.012.53.673.671.171.150.970.860.680.20


Let's try to find a solution with S = 60.
Σ
aone239232632142825163
b3225eleven3327thirty3112nineteenfive225
d32.012.53.673.671.171.150.970.860.680.20

The first five items will give us Σa = 38, Σb = 128. The next item does not fit. With it, Σa = 64.
The hole remaining after laying the first five items turned out to be the size: 60-38 = 22. If you view the set to the end, there is another item that fits into this hole.
Σ
aone239232632142825163
b3225eleven3327thirty3112nineteenfive225
d32.012.53.673.671.171.150.970.860.680.20
Total: Σa = 52, Σb = 140.

Unfortunately, this is not the optimal solution.

If we replace item 23-27 with 26-30,

Σ
aone239232632142825163
b3225eleven3327thirty3112nineteenfive225
d32.012.53.673.671.171.150.970.860.680.20
then Σa = 55, Σb = 143, which is already the optimal solution.

Consider the limiting case. We have two items that are placed one by one in a backpack, but not together. The natural solution would be to take a more expensive item, despite its greater weight. Obviously, the price of the item being laid has a higher priority than weight.
A small revaluation of the value of d allows to shift the priority to the side we need.

Instead of the simple relation d = b / a, take d = b 2 / a and still sort by d.

Sorted by d = b 2 / a set
Σ
aone293262332142825163
b322533eleventhirty273112nineteenfive225
d1024312.512140.3334.631.730.010.312.91.0

For the same S = 60
Σ
aone293262332142825163
b322533eleventhirty273112nineteenfive225
d1024312.512140.3334.631.730.010.312.91.0
Σa = 55, Σb = 143. We immediately come to the optimal solution.

Thus, the task of packing a backpack is solved in linear time and is not NP complete.

Source: https://habr.com/ru/post/205890/


All Articles