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.
Algorithm description
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:
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
Example
We have 6 teams, so we will generate 5 rounds with 3 matches each.
Teams – 1, 2, 3, 4, 5 y 6
First round
1 vs 6, 2 vs 5 and 3 vs 4
Second round
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
Third round
Applying the same as in the previous round:
2 vs 6, 3 vs 1 and 4 vs 5
Fourth round
6 vs 5, 1 vs 4 and 2 vs 3
Fifth round
3 vs 6, 4 vs 2 and 5 vs 1
Tweet This Post