-2

I am creating a schedule for a sports league with several dozen teams. I already have all of the games in a set order and now I just need to assign one team to be the "home" team and one to be "away" for each game.

The problem has two constraints:

  1. Each pair of teams must play an equal number of home and away games against each other. For example, if team A and team B play 4 games, then 2 must be hosted by A and 2 by B. Assume that each pair of teams plays an even number of games against each other.
  2. No team should have more than three consecutive home games or three consecutive away games at any point in the schedule.

I have been trying to use brute force in R to solve this problem but I can't get any of my code blocks to solve the issue in a timely fashion. Does anyone have any advice on how to deal with either (or both) of the above constraints algorithmically?

adustybowler
  • 47
  • 1
  • 3

1 Answers1

2

You need to do more research on simple scheduling. There are a lot of references on line for these things. Here are the basics for your application. Let's assume a league of 6 teams; the process is the same for any number.

Match 1: Simply write down the team numbers in order, in pairs, in a ring. Flatten he ring into two lines. Matches are upper (home) and lower(away).

1 2 3
6 5 4

Matches 2-5: Team 1 stays in place; the others rotate around the ring.

1 6 2
5 4 3

1 5 6
4 3 2

1 4 5
3 2 6

1 3 4
2 6 5

That's one full cycle. To balance the home-away schedule, simply invert the fixtures every other match:

1 2 3    5 4 3    1 5 6    3 2 6    1 3 4
6 5 4    1 6 2    4 3 2    1 4 5    2 6 5

There's your first full round. Simply replicate this, again switching home-away fixtures in alternate rounds. Thus, the second round would be:

6 5 4    1 6 2    4 3 2    1 4 5    2 6 5
1 2 3    5 4 3    1 5 6    3 2 6    1 3 4

Repeat this pair of rounds as many times as needed to get the length of schedule you need.

If you have an odd quantity of teams, simply declare one of the numbers to be the "bye" in the schedule. I find it easiest to follow if I use the non-rotating team -- team 1 in this example.

Note that this home-switching process guarantees that no team has three consecutive matches either home or away: they get two in a row when rounding the end of the row. However, even the two-in-a-row doesn't suffer at the end of the round: both of those teams break the streak in the first match of the next round.


Unfortunately, for an arbitrary existing schedule, you are stuck with a brute-force search with backtracking. You can employ some limits and heuristics, such as balancing partial home-away fixtures as the first option at each juncture. Still, the better approach is to make your original schedule correct by design.

There's also a slight problem that you cannot guarantee that your existing schedule will fulfill the given requirements. For instance, given the 8-team fixtures in this order:

1 2 3 4
5 6 7 8

1 2 5 6
3 4 7 8

1 3 5 7
2 4 6 8

It is not possible to avoid having at least two teams playing three consecutive home or away matches.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • This would be great if my schedule was a true round-robin, but it is not. I have already created a more complicated schedule with teams playing divisional opponents more often than non-divisional opponents, for example. I would like to assign home/away to my schedule and not create a new one. – adustybowler Jan 08 '19 at 22:29
  • Please edit the additional constraints into the problem description. – Prune Jan 08 '19 at 22:33
  • For the situation you describe, simply handle it the way most professional leagues do it: divide the season into intra-division and inter-division portions. Apply the above algorithm to each portion. For instance, USA baseball does this with interleague play. – Prune Jan 08 '19 at 22:35