Jump to content

Efficient sorting into categories with choices

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Liglogthepanda

Liglogthepanda

    Newbie

  • Members
  • Pip
  • 1 posts
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.

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.

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:

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 matters at this early stage!

#2
Luke Bradley

Luke Bradley

    Newbie

  • Members
  • Pip
  • 6 posts
Seems pretty straightforward. Wouldn't you just take the next item, and put in in the bin with the least stuff that it can fit in, (b1) then for all other bins with less stuff in them, search b1 for one item that can fit in the other bin, and remove that one item and place it in the smallest other bin it can fit it? I can think of one infinite recursion situation where all the bins have the same amount of stuff and something goes in circles, but if that is accounted for it seems complete...