Algorithm for the generation of a soccer fixture
What’s the problem?
We wish to create a fixture in which every team will face every other team one single time. It is also required that each team will only play one single match in each of the defined rounds. It’s a classic tournament or league scenario.
The first thing we must do is to see if the amount of teams is odd or even. In case it is odd, an additional team needs to be created which will be used as a “free day”, the team that has a match against it will not play in that round.
Once we have all the teams we must arrange them in the following way:
The first with the last
The second with the penultimate
And so on until every team is assigned
To generate the following rounds, we must take the last element of the previous one (the last team of the first round order) and the last team (team number 6 if there are 6 teams) and we arrange them in the following way:
If the round we are generating is even (First one is 1, so it is always odd) we must take the first team of the previous round and then the last team. On the contrary, if it’s even, the last team goes first and then we put the last team of the previous round.
Note: last team = 6 if there are 6 teams. Last team of previous round = 3 if the round was 6 vs 5, 1 vs 4 and 2 vs 3
Then we must remove the teams that were first in the previous round, keeping the same order.
Once we have this list, we put the eliminated teams at the beginning of it.
Beginning from the end, we take two teams from this list and we put them at the front. We do this with every pair until we go through the entire list.
We keep the process until every round is generated.
We have 6 teams, so we will generate 5 rounds with 3 matches each.
Teams – 1, 2, 3, 4, 5 y 6
1 vs 6, 2 vs 5 and 3 vs 4
We take the last team of the previous round (team 4) and the last team (team 6), since the round is par we must put first 6 and then 4, the result is 6 vs 4.
We remove 6 and 4 from the list of the first round (1, 6, 2, 5, 3 and 4 ) and we sep the resulting order, which results in 1, 2, 5 and 3. We take the last two elements of this list (5 and 3) and we put them after 6 and 4. Finally, we add 1 and 2 to the end of the list:
6 vs 4, 5 vs 3 and 1 vs 2
Applying the same as in the previous round:
2 vs 6, 3 vs 1 and 4 vs 5
6 vs 5, 1 vs 4 and 2 vs 3
3 vs 6, 4 vs 2 and 5 vs 1