6

I'm fairly new to complex algorithms (been doing simple programs til now), and I'm trying to develop a program where the user can input a desired budget, and the program would search for the optimal products the user can purchase from each category. example:

Products:
Shirt:
shirtA - $20
shirtB - $15
shirtC - $10
Pants:
pantsA - $30
pantsB - $25
pantsC - $20
Shoes:
shoesA - $20
shoesB - $15
shoesC - $10

User Input (budget): $60

Output:
shirtB - $15
PantsA - $30
shoesB - $15
total: $60

...or something like that. what kind of algorithm should I be researching for? I find this hard because I do not know where to begin in understanding what algorithm to use. See, this is for class, and our professor wants us to indicate what kind of algorithm we used. I think I can actually finish this if I just brute force trial and error this thing, but then I wouldn't know what algorithm I used. anyway, thanks guys.

10
  • 9
    Seems to be a variant of the Knapsack problem Commented Jul 15, 2015 at 13:28
  • 1
    Is it necessary that at most one item from each category (Shirt, Pants, Shoes) is selected? Commented Jul 15, 2015 at 13:28
  • Search for an optimisation algorithm. You'll want to look at the various types that exist, and figure out whether or not this problem falls within the exact type they can solve. Note that at worst, if this is a form of bin-packing problem, the optimal solution is NP-hard. Commented Jul 15, 2015 at 13:29
  • 1
    Why is the 15-30-15 option better than 20-20-20? (If you can answer this question, you are probably a good deal closer to finding the algorithm) Commented Jul 15, 2015 at 13:29
  • 1
    Note also that if one from each category must be selected this problem is easier than if it is unconstrained. Commented Jul 15, 2015 at 13:30

2 Answers 2

3

The problem is a variation of the Knapsack problem; instead of choosing whether an item is to be included in the solution, a choice must be made which item to take from each category. Although not explicitly stated in the Wikipedia article, the formulation in the question can be solved within a pseudopolynomial runtime bound via Dynamic programming by modifying the recurrence relation for the basic formulation.

Sign up to request clarification or add additional context in comments.

Comments

0

Just a fun with Python's itertools:

import itertools

def solutions(dicts, value):
    return [keys for keys in itertools.product(*dicts) if sum(d[k] for d, k in zip(dicts, keys)) == value]

Shirt = {"shirtA": 20, "shirtB": 15, "shirtC": 10}
Pants = {"pantsA": 30, "pantsB": 25, "pantsC": 20}
Shoes = {"shoesA": 20, "shoesB": 15, "shoesC": 10}

print solutions([Shirt, Pants, Shoes], 60)

We generate all allowed combinations (one piece from each category) but we keep only these that give the required sum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.