views:

234

answers:

2

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 also has a number on it. There are also a bunch of boxes which are each only one color. The goal is to maximize the sum of the numbers on the balls in the boxes, and the only rules are:

  • in order to place a ball in a box, it has to at least have the box's color on it
  • you can only put one ball in each box.

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 come up with a few optimizations that help a lot in terms of running time. For example, you can sort the balls in descending order of point value. Then as you go from highest number to lowest, if the ball only has one color, and there are no other higher-point balls that contain that color, you can put it in that box (and thus remove that box and that ball from the remaining combinations).

I'm just curious is there's some kind of dynamic algorithm for this type of problem, 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. Thanks!


Edit - here's a simple example:

Balls:

  • 1 red ball - 5 points
  • 1 blue ball - 3 points
  • 1 green/red ball - 2 points
  • 1 green/blue ball - 4 points
  • 1 red/blue ball - 1 point

Boxes:

  • 1 red
  • 1 blue
  • 1 green

Optimal Solution:

  • red ball in red box
  • blue ball in blue box
  • green/blue ball in green box

    Total value: 12 points (5 + 3 + 4)

+10  A: 

This is a special case of the maximum weight matching problem on a weighted bipartite graph. Construct a graph whose left vertices correspond to balls, whose right vertices correspond to boxes and with the edge joining a ball and a box having weight V where V is the number on the ball if the ball can be placed in the box, and 0 otherwise. Add extra boxes or balls joined to the other side with edges of weight zero until you have the same number of vertices on each side. The assignment you're looking for is determined by the set of edges of nonzero weight in the maximum (total) weight matching in the resulting graph.

The assignment algorithm can be solved in O(n^3) time, where n is here the maximum of the number of balls or boxes, using the Hungarian algorithm. (BTW, I should make the disclaimer that I only mention the Hungarian algorithm because it is the theoretical result I happen to be familiar with and it presumably answers the question in the title of whether the original problem is NP-hard. I have no idea whether it is the best algorithm to use in practice.)

Reid Barton
Another link: http://en.wikipedia.org/wiki/Matching_(graph_theory)#Maximum_matchings_in_bipartite_graphs
sdcvvc
Wow, this looks very promising! It's gonna take me some time to digest this and actually try to implement it, but thank you so much!
Kenny
Let me try to take this even one step further... let's say the first goal was the maximize the number of balls placed in boxes, and then the tie-breaker would be the maximum sum. Is this still solvable in polynomial time?
Kenny
Negative *edge weights* shouldn't affect an algorithm for solving the max weight matching problem. Even if they did, you can easily convert an instance of the max weight matching problem with negative edge weights into an instance with nonnegative weights and the same optimal matching by adding a sufficiently large constant to every edge weight. (continued)
Reid Barton
Thanks Reid, I made the same realization about negative edge weights right after I posted that comment, so I deleted it and rephrased my question (above your comment).
Kenny
Yes, in that case you can change all of the edge weights that were 0 to -C where C is a sufficiently large constant, specifically, larger than the sum of all the ball weights. Then the maximum weight matching is, firstly, the one with the fewest edges of weight -C, i.e. the maximum cardinality matching, and secondly among those the one with maximum total weight on the positive edges.
Reid Barton
Beautiful. You rock Reid, thanks a million!
Kenny
A: 

Have you tried a greedy alg? Sort by points/value and place in box if possible. If there are any exceptions im missing id like to see them.

Anders
Let's say you have a red/green ball worth 10 points and a red ball worth 5 points. You have a green box and a red box. Using a greedy algorithm, you first put the red/green ball in the red box, and then you aren't able to place the red ball anywhere :(
Kenny
Yeah but i belive your wrong. When using a greedy alg you can be able to place the red/green ball in the green box, how you chose to handle it when there are more options is up to you. Greedy just wants you to place the 10 points ball before you place the 5 points ball.
Anders
If you have to take into account moves you'll make in the future, that is a lot of extra work, while greedy algorithms are known for their efficiency. Greedy algorithms are efficient because they, like change making, are able to completely ignore the future to make any choice at all that gives the highest reward possible this turn.
Olathe