10

I'm trying to come up with a reasonable algorithm for this problem:

Let's say you have a bunch of balls. Each ball has at least one color, but can also be multicolored. Each ball has a weight and a value associated with it. There are also a bunch of boxes which are each only one color. Each box has a maximum number of balls it can hold. The goal is to maximize the sum of the value in the boxes while staying under some total weight, W, and the only rule is:

In order to place a ball in a box, it has to at least have the box's color on it

(For example, you can put a blue and green ball into a blue box or a green box, but not into a red box.)

I've dome some research and this seems similar to the knapsack problem and also similar to being solvable by the Hungarian algorithm, but I can't quite seem to reduce it to either problem.

I'm just curious is there's some kind of dynamic programming algorithm for this type of problem to make it solvable in polynomial time, or if it's just the traveling salesman problem in disguise. Would it help if I knew there were at most X colors? Any help is greatly appreciated. I could also formalize the problem a bit with variable names if it would help. Thanks!

Here's a simple example:

Maximum weight: 5

Balls:

1 red ball - (value = 5, weight = 1)

1 blue ball - (value = 3, weight = 1)

1 green/red/blue ball - (value = 2, weight = 4)

1 green/blue ball - (value = 4, weight = 1)

1 red/blue ball - (value = 1, weight = 1)

Boxes:

1 red (holds 1 ball)

1 blue (holds 2 balls)

1 green (holds 1 ball)

Optimal Solution:

red ball in red box

blue ball and red/blue ball in blue box

green/blue ball in green box

Total value: 13 (5 + 3 + 1 + 4)

Total weight: 4 (1 + 1 + 1 + 1)

Note: even though the green/red/blue ball was more valuable than the red/blue ball, it's weight would have put us over the limit.

Edit:

One clarifying point: balls with the same color combination will not necessarily have the same weights and values. For example, you could have a red ball with value 3 and weight 1 and another red ball with value 2 and weight 5.

Edit 2:

We can assume integer weights and values if it helps us come up with a dynamic programming algorithm.

Kenny
  • 179
  • 8
  • What size numbers are your real numbers of boxes, balls, colours? – AakashM Apr 12 '12 at 16:58
  • Hello knapsack-problem? If balls can have arbitrary weights it seems so to me. You don't even need the colors for that :) – Voo Apr 12 '12 at 17:09
  • @AakashM: Reatively small: The number of boxes is on the order of 10. The number of balls per box is on the order of 10. The total number of possible colors is on the order of 10. The number of colors per ball is usually a maximum of 5 (common case is only one or two colors per ball). The total number of available balls is on the order of 1000. – Kenny Apr 12 '12 at 17:10
  • @Voo: can you help me see how this reduces to knapsack? It seems like each of my individual boxes is a knapsack of sorts, but each box had a capacity limit, not a weight limit, and I need to stay under a weight for the sum of all boxes while maximizing value. – Kenny Apr 12 '12 at 17:14
  • @Kenny It's only assuming balls can have arbitrary weights, if that's true - the explanation by the people who answered already is the same as I'd give. – Voo Apr 12 '12 at 17:19
  • In order to solve a problem in polynomial time, you must be able to handle *all instances* of the problem. The addition of a capacity limit doesn't matter, because you can set up the problem with a capacity limit so high it is not relevant (e.g., set it to the total number of balls) – comingstorm Apr 12 '12 at 17:24
  • In other words: adding a capacity limit may make the knapsack problem harder, but it can't make it easier. – comingstorm Apr 12 '12 at 17:27
  • I guess I was hoping for a "pseudo-polynomial time" solution. I should have clarified. Obviously this problem is at least as complicated as knapsack. – Kenny Apr 12 '12 at 17:45

5 Answers5

5

This is at least as complex as the Knapsack problem - consider a case where all balls are red and there is only one red box.

In the case when balls that have the same combination of colors must have the same weights and values consider a case when you have red/blue, red/green, etc. balls and only one red box.

Paweł Obrok
  • 21,259
  • 8
  • 72
  • 67
  • Yep +1. The multicolored aspect of balls could make the usual dynamic programming solution a bit more complicated though. – Voo Apr 12 '12 at 17:11
  • To clarify: balls with the same color combination will not necessarily have the same weights and values. For example, you could have a red ball with value 3 and weight 1 and another red ball with value 2 and weight 5. – Kenny Apr 12 '12 at 17:20
  • 1
    Well anyway - you can represent any instance of knapsack with this problem so it is NP-hard. – Paweł Obrok Apr 12 '12 at 17:27
  • @obrok: I see. I guess I was hoping that since you can use dynamic programming to solve the knapsack problem in polynomial time (assuming weights and values are integers), you might be able to employ a similar technique here. – Kenny Apr 12 '12 at 17:34
  • 2
    It's pseudo-polynomial actually - that means in this case polynomial in the sum of the weights - which might be very large even for small problems! An effective dynamic algorithm is not likely to exist for this problem - the state is very complex - each kind of ball and box must be treated separately, so it's unlikely that nodes in the search tree will repeat. – Paweł Obrok Apr 12 '12 at 17:36
  • Ah, thanks for the clarification. That's what I was afraid of. – Kenny Apr 12 '12 at 17:40
  • 2
    @Kenny This proof does not rule out the possibility of a dynamic programming solution (pseudo-polynomial). Knapsack is *weakly NP-complete*. To completely establish such a nonexistence, we have to reduce a *strongly NP-complete* problem to it (with some extra requirements on the reduction itself). – aelguindy Apr 13 '12 at 09:30
5

If there is no bound on the number of boxes, then this problem is strongly NP-hard by reduction from 3-partition (set up n/3 boxes and make all the things rainbow-colored with value = weight).

If the number of boxes is constant, then there's a pseudo-polynomial time algorithm via dynamic programming, where each DP state consists of how full each box is.

junction
  • 131
  • 1
  • Can you elaborate a little on how this DP algorithm would work assuming a constant number of boxes? Thanks! – Kenny Apr 13 '12 at 02:21
  • Consider the items one by one. At each step, keep a table indexed by tuples describing how full each box is. For example, index (2, 2, 3) would mean that the first box has 2 units of items, the second, 2 as well, and the third, 3. Each table entry is the maximum value that can obtained over the items considered so far such that each box is filled to the level specified by the index of that entry. – junction Apr 16 '12 at 15:28
3

The reduction from knapsack is as follows. Given your knapsack instance, you create an instance of the balls and bins problem: for each item of the knapsack instance you have a ball with the same weight and value as the item. Then you have a box representing the knapsack. The balls and the box are all blue. The capacity of the box is the limit given in the knapsack problem. Given a solution to your problem, we have a set of balls in the box whose total weight is at most the knapsack limit, and whose total value is maximised.

Irit Katriel
  • 3,129
  • 1
  • 13
  • 18
3

This problem is NP-complete, because it subsumes the knapsack problem.

That is, it's not just similar to the knapsack problem: if there is one bowl, all the balls have that bowl's color, and the maximum number of balls in the bowl is the total number of balls, then the problem is exactly the knapsack problem.

If an algorithm could solve this problem in polynomial time, it could solve any knapsack problem in polynomial time. But, since the knapsack problem is NP-complete, this problem is, too.

comingstorm
  • 23,012
  • 2
  • 38
  • 64
  • I agree that with one box, this reduces to knapsack, but I don't see how I could use the same techniques to solve this problem when there are multiple boxes with different colors. Since balls can have more than one color, they could potentially fit in different boxes, so the individual "knapsacks" are not independent. – Kenny Apr 12 '12 at 17:30
  • 2
    Your question was "is the problem solvable in polynomial time". That's worst-case: if you can compose *any possible* problem it can't solve in polynomial time, then the answer is "no". Another way to put it: if you find an algorithm that can solve this problem in polynomial time, you have proved that P=NP. And while we don't know for *sure* that this is impossible, it doesn't seem very likely... – comingstorm Apr 12 '12 at 17:53
  • Thanks, I should have clarified earlier: I aw hoping a "pseudo-polynomial" solution might be possible/practical as well. – Kenny Apr 12 '12 at 18:56
0

The best you can do in this situation is get an approximation of the optimal solution - the knapsack problem is not solvable in polynomial time itself. You may be able to still get good (although not guaranteed to be optimal) results in polynomial time if you can generate a good algorithm for it.

Mikey
  • 2,793
  • 28
  • 36