e-commerce: Algorithm for calculating discounts

Assuming that:

  1. You can compute all available discounts based on your basket
  2. Each product can only have a single discount applied to it
  3. Each discount can only be used once

Then the problem becomes one that is called an assignment problem and can be optimally solved in O(n^3) using the Hungarian algorithm.

You will need to compute a matrix M[a,b] containing the money saved if using discount a on product b. (If a discount does not apply, then set the money saved to 0.)

The Hungarian algorithm will compute the way of assigning discounts to products that saves the most money.

If you don’t have the same number of discounts and products, then add dummy discounts (with zero savings) or dummy products (again with zero savings) until the number of discounts matches the number of products.

Leave a Comment

tech