r/PHPhelp • u/ser_89 • Oct 25 '22
Greedy Algorithm with added functionality.
I have a specific function that I require assistance with. I am busy with a project where licenses need to be allocated based on a specific quantity as per user input. The licenses are bundled as an example 1 user, 5 users, 10 users 20 users and 50 users. If the input is 20 it's easy to determine that the license that needs to be allocated is one for 20 users. If the input is 27 I can use a greedy algorithm to determine the best way to allocate would be 1 x 20 users, 1 x 5 users and 2 x 1 user.
My problem now though is that in most cases there is a threshold where it is actually more cost effective to rather allocate a license that caters for more users compared to the input specified. For example 19 users it is more cost effective to provide a license for 20 compared to 1 x 10 users, 1 x 5 users and 4 x 1 user.
What would be the best approach to approach the above and avoid unnecessary recalculation and comparison. The obvious approach is to run the greedy algorithm and get a total cost and compare the cost of another license where total users > users as per input.
The issue I see is already making me scratch my head. Is for example 28 users. I already know the most cost effective approach is 1 x 20 users and 1 x 10 users. The next license up is 50 users. So it will be too expensive. However the greedy algorithm gives me 1 x 20 user, 1 x 5 user and lastly 3 x 1 user.
Any assistance will be greatly appreciated.
1
u/buycallsbro Oct 25 '22
I won’t pretend to know the best way, but what I’m thinking is you make a little algorithm which determines where the thresholds are and store them. It would only need to be run when the prices/user offers change.
So for example you look up 28 and if it’s not in the table but is in between 27 and 30, then 30 is what you go with. You could store that 30 is made up of 20 and 10, or just figure it out after retrieval.
1
u/MateusAzevedo Oct 25 '22
I don't have anything specific to the problem to help, but I want to mention this is a similar problem to when you withdraw money from ATMs. It isn't very advisable to always give the same bills for a given amount of money, or the ATM will likely run out of some of them pretty fast. AFAIK, there's an algorithm to calculate multiple possibilities and decide which to use based on how many of each bills are left.
Maybe that's something you can look for: compute all the possible license combo, use the one that cost less.
1
u/Plastonick Oct 25 '22
This is similar to the knapsack problem, with the caveat that we have a minimum expected cost and we're trying to reduce the weight required for it.
There's a solution here for it: https://stackoverflow.com/questions/65260866/knapsack-with-min-weight
1
u/ardicli2000 Oct 25 '22
I would implement a while loop in which I use modulus operator until remainder is bigger than 1.
Put your package numbers in an array, sort array values from big to small and iterate through array elements for modulus.
Assign each result to a new array with licence number as key.
1
u/richardathome Oct 25 '22
What's the maximum number of licences someone would buy? 100? 200?
Pre-calculate (by hand in necessary) and store the result in some common memory and just look it up.
3
u/wubblewobble Oct 25 '22 edited Oct 25 '22
The vague (i.e. might be nonsense) idea that I have in my head is to incrementally calculate a lookup table of best combos; the best combo for 1 user, then 2, then 3...
Then when it comes to 28 users, we have the best combos for 1-27 users. We can try 27 + 1, 26 + 2 etc, but I think we'll be able to exclude answers containing more than 2 factors, such as 23 + 1 + 1 + 1 + 1 + 1 because we'll already have the best answer for 5 (i.e. whether five single user licences, or a single 5-user licence is cheaper to get 5 licences).
Not sure if it'll play out and work in reality, but it's my best idea currently. It may (or may not?) be possible to exclude further combos too?
edit: Another thought - is there another more general programming or algorithms subreddit that might better be able to answer this?