# Algorithm for scheduling shifts

I am trying to write a program to help scheduling shifts for the employees of a small business. There are 28 shifts that needs to be assigned to 28 employees (so this means that each person gets a shift per week). Each employee can provide a set of preferred shifts, so my idea was to procede as follows: 1) first assign the preferred shift to those employees that provided only one preference 2) after step #1, go back and assign each remaining shift to the first person whose preferences match said shift

Is this a smart way to approach this problem? If not, can you help me understand what is wrong and/or point me towards some literature that may help me with that?

• there is no good solution to this problem
– Ewan
Mar 6, 2018 at 20:56
• – gnat
Mar 6, 2018 at 20:59
• You're asking for an algorithm, but first you must tell us the actual requirements. What should the result be when not all preferences can be satisfied? Does someone get "first draft"? Should people who got an unwanted shift be compensated next week? Mar 6, 2018 at 22:15
• The what algorithm question has a lot more detailed requirements than this one. Mar 7, 2018 at 12:47

The supposed algorithm will not work, because it contains no collision handling. But there is a simple way to extend it to make it at least produce some solution:

• in step 1, if there are collisions for a shift, throw a dice among the employees which attend for the same shift, and pick one

• in step 2, the same

• add a step 3: distribute all remaining employees among the remaining shifts randomly

You could also try a slightly different approach: don't separate step 1 and 2. Instead, start with all shifts which are given as first preference by at least one employee, no matter if they gave one or more preferences. Then,

• throw a dice among those employees to decide who gets the shift.

• remove that shift from the preference list of each employee where it occurs

• repeat this until the the preference lists of all employees became empty

• finally, distribute all remaining employees among the remaining shifts at random.

It sounds like you are trying to solve the "Nurse Scheduling Problem" (NSP), although your problem seems to be a simplified version of it. There are many articles written about how to find feasible solutions to the problem. If you want something up and running fast, you could try to use Google's python package, which can be found here: Employee Scheduling