So here's the problem.
I have 8 categories, and about a hundred items. Each item can fit into 1, 2, 3 or 4 of these categories (and we know which ones each item can fit into). The task is to have 'the most even distribution' of the items among the categories, i.e. it is best if all the categories have something in them before putting more than one into a category. Once an item has been placed in a category, it cannot be repeated in another.
Example 1, with eight items
Categories labelled, A to H. Items are represented by X's.
Code:
A | B | C | D | E | F | G | H
----------------------------
X | X | X | X | X | X | X | X .......Table (1)
A | B | C | D | E | F | G | H
----------------------------
X | X | X | X | X | | X | X
X | | | | | | | .......Table (2)
A | B | C | D | E | F | G | H
----------------------------
X | X | X | X | X | | | X
X | | X | | | | | .......Table (3)
In the above case (with eight items), 1 is better than 2 or 3. Basically, to
fill a row as quickly as possible is the task, given that some items will only fit into some categories, and that you can't put the same item into two categories in the end!
Example 2, with ten items
Categories labelled, A to H. Items are represented by X's.
Code:
A | B | C | D | E | F | G | H
----------------------------
X | X | X | X | X | X | X | X .......Table (1)
X | | X | | | | |
A | B | C | D | E | F | G | H
----------------------------
X | X | X | X | X | | X | X
X | | X | X | | | | .......Table (2)
A | B | C | D | E | F | G | H
----------------------------
X | X | X | X | X | | |
X | X | X | X | | | |
| X | | | | | | .......Table (3)
Again, 1 is better than 2 or 3.
Example data:
Code:
Item X could fit into category A.
Item Y could fit into categories A or B.
Item Z could fit into category C.
Item W could fit into categories B or F.
The second stage after this is filling the next row up as quickly as possible, but I'm not too concerned about this at the moment (one step at a time, right!)
I'm having some difficulty thinking of an algorithm for this, and I don't know what to search for in my favourite search engine (which may or may not begin with a G), so
help, please!
PS I'll be using PHP/MySQL, not that that really matters at this stage!