Knapsack algorithm implementation based on https://stackoverflow.com/a/49353839/4448410
This repo provides an example of an algorithm to :
- select N products from a mysql database table
- their prices sum to S
- do this randomly(or appear random)
This is a version of the https://en.wikipedia.org/wiki/Knapsack_problem
The algorithm is based on the following:
In a collection of n positive numbers that sum up to S, at least one of them will be less than S divided by n (S/n)
- Select a product randomly where price < S/N. Get its price, lets say X.
- Select a product randomly where price < (S - X)/(N-1). Get its price, assume Y.
- Select a product randomly where price < (S - X - Y)/(N-2).
- Repeat this and get till you get N-1 elements and the remainingPrive P.
- Select a product that price equals to P. If not found, repeat.
This is intended to be used for providing random collections of products that their price sum to a fixed value.