8

I am looking to figure out a way to sort people into classes by preference.

For example, say there are 100 students that are each going to be assigned one of five classes:

  • Science - 40 seats
  • Math - 15 seats
  • History - 15 seats
  • Computers - 20 seats
  • Writing - 10 seats

Each student has three preferred classes that are ordered by preference. What is the best way to approach dividing up the students so that as many people get their first and second choice classes as possible, while at the same time making sure that no class has too many students for the room.

I've thought about approaching it by the following method:

  1. Group all students by their first choice class
  2. See which classes have too many students and which have too few
  3. Check to see if any students in the overbooked classes have second choice classes which are underbooked
  4. Move those students accordingly
  5. Repeat 2-4 with 3rd choice classes

While I feel like this is a reasonable implementation, I am wondering if there are any other algorithms that solve this problem in a better way. I have tried searching all over, but I cannot find anything that would solve this kind of problem.

user2338460
  • 81
  • 1
  • 2
  • One 'problem' with these sort of algorithms is that it is easy to 'cheat' by selecting popular (and small) courses as a 2nd and 3rd choice to force a 1st-choice placement.. I would be highly interested in a solution that addresses this somehow (although I currently have no intuition to approach it). – Joost Mar 24 '16 at 14:32
  • 1
    Note that this problem is called the "hospitals/residents," the "student-project allocation," or the "college admissions" problem, which is a variation on the more general "Stable Marriage Problem." [Here is an excellent resource that outlines much of the collegiate level research done on this problem, and gives several implementations of the solution as of year 2000.](http://www.dcs.gla.ac.uk/research/algorithms/stable/) This is the same algorithm NMRP uses today. – Zediiiii Aug 08 '19 at 06:22
  • @Zediiiii: Stable marriage and hospitals/residents are not the same problem as the one in the original question, because they require the groups to rank the students as well (which is in some sense unfair). For a solution see https://cs.stackexchange.com/questions/93886/which-algorithm-can-calculate-an-optimal-allocation-of-students-to-projects. I linked my own implementation there in a comment. – maiphi Nov 19 '21 at 14:35

1 Answers1

4

From your description, this sounds very much like one of the variations of the Stable Marriage Problem

wikipedia

Check the Wiki link and you will see a description of the Gale-Shapley Algorithm, which is a good solution.

 Gale-Shapley Algorithm

glh
  • 4,900
  • 3
  • 23
  • 40
Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • 1
    I like this. So you could say the students are proposimg to the classes and the class accepts if full. Then the students that get kicked out of the class due to another has to propose to their next preference until all sizes are matched? The only key would then be how to order them? A to Z or random? – glh May 02 '13 at 13:07