Algorithm for searching the combination with lowest price

时间:2017-01-10 02:26:47

标签: algorithm combinations minimization

I have a set a products (a shopping cart), and a set of discount/offers to be applied over the products of the user shopping cart, but it is also possible that a product inside a cart fits into more than one offer. I'm trying to find an algorithm that choose, over a set of products, which discount/offers to apply and over which products. For example: a shopping cart that consists of 5 products with the following base prices:

p1: 6€
p2: 6€
p3: 9€
p4: 8€
p5: 12€

And the following discount/offers:

(p1, p5):     18€ => 10€   (you save 8€, or a 44.44%)
(p1, p2):     12€ => 9€    (you save 3€, or a 25%)
(p2, p3, p4): 23€ => 21€   (you save 2€, or a 8.70%)
p3:           9€ => 8€   (you save 1€, or a 11.00%)

And of course, if you apply the offer (p1, p5), you cannot choose the (p1, p2) offer, because p1 is then repeated, or the (p3, p4, p5) and the p3 discounts. The algorithm I'm searching for is the set of offers to choose to save the maximum amount of money with the restriction that no product can get more than one discount/offer.

An approach is to start with the offer that saves the most, and choose, in that order, the offers that doesn't add more than once any product:

(1) Choose (p1, p5): 10€
    Reject (p1, p2): p1 repeated
(2) Choose (p2, p3, p4): 21€
    Reject p3: repeated
    Reject p2: repeated
    Reject p4: repeated
End: we already have p1, p2, p3, p4 and p5
Total: 31€

Other approach is to start with the offer that saves "proportionally" (respect to the combined base price) more money:

(1) Choose (p1, p5): 10€
    Reject (p1, p2): p1 repeated
(2) Choose p3: 8€
    Reject (p2, p3, p4): p3 repeated
(3) Choose p2: 6€
(4) Choose p4: 8€
End: we already have p1, p2, p3, p4 and p5
Total: 32€

In this specific case, the "saving more money" wins, but I have got counterexamples where the "proportional saving" wins. I've tried even with "proportional saving" respect to the cart base price.

What is the best algorithm in this case?

NOTE: Preferred language to illustrate your solution, pseudocode or C++.

NOTE: The linked "duplicated problem" has no solution to the question. Even though, the formulation is far different (I have not yet fully understand the linked question).

0 个答案:

没有答案