Click here to see a description of the algorithm the program uses to schedule a round robin tournament.

Function `GenerateRoundRobinOdd` returns an array where `results[i, j]` gives the opponent of team `i` in round `j` of the round robin tournament. This function works only for an odd number of teams. The link above explains the method.

private const int BYE = -1; // Return an array where results(i, j) gives // the opponent of team i in round j. // Note: num_teams must be odd. private int [,] GenerateRoundRobinOdd(int num_teams) { int n2 = (int)((num_teams - 1) / 2); int[,] results = new int[num_teams, num_teams]; // Initialize the list of teams. int[] teams = new int[num_teams]; for (int i = 0; i < num_teams; i++) teams[i] = i; // Start the rounds. for (int round = 0; round < num_teams; round++) { for (int i = 0; i < n2; i++) { int team1 = teams[n2 - i]; int team2 = teams[n2 + i + 1]; results[team1, round] = team2; results[team2, round] = team1; } // Set the team with the bye. results[teams[0], round] = BYE; // Rotate the array. RotateArray(teams); } return results; }

Helper function `RotateArray` rotates the items in the team array. The algorithm calls this routine after each round.

// Rotate the entries one position. private void RotateArray(int[] teams) { int tmp = teams[teams.Length - 1]; Array.Copy(teams, 0, teams, 1, teams.Length - 1); teams[0] = tmp; }

Function `GenerateRoundRobinEven` returns a similar array for an even number of teams. It calls `GenerateRoundRobinOdd` to make a schedule for a tournament with one fewer teams. It then expands the result array and replaces the byes with the additional team. See the link above for a more complete explanation.

private int[,] GenerateRoundRobinEven(int num_teams) { // Generate the result for one fewer teams. int[,] results = GenerateRoundRobinOdd(num_teams - 1); // Copy the results into a bigger array, // replacing the byes with the extra team. int[,] results2 = new int[num_teams, num_teams - 1]; for (int team = 0; team < num_teams - 1; team++) { for (int round = 0; round < num_teams - 1; round++) { if (results[team, round] == BYE) { // Change the bye to the new team. results2[team, round] = num_teams - 1; results2[num_teams - 1, round] = team; } else { results2[team, round] = results[team, round]; } } } return results2; }

Function `GenerateRoundRobin` simply calls functions `GenerateRoundRobinOdd` and `GenerateRoundRobinEven` depending on whether the number of teams is odd or even.

private int[,] GenerateRoundRobin(int num_teams) { if (num_teams % 2 == 0) return GenerateRoundRobinEven(num_teams); else return GenerateRoundRobinOdd(num_teams); }

Fantastic piece of code, this saved me a great amount of time on a golf fixture generator for my clients website. Thanks alot. This has been bookmarked.

Pingback: Algoritmos: Round Robin | ñoño Blog

Do you have such Algo for Single elimination and Double elimination too?

Those are generally easier because you can use a pre-built bracket. If you google for “single elimination brackets” or “double elimination brackets” you should see a lot of choices for tournaments with different numbers of teams.

If you have an odd number of teams, give some of the top teams byes. For example, if you have 7 teams, use an 8 team bracket and give the top seeded team a bye in round 1.

Let me know if you have special needs that won’t fit these sorts of brackets.

Bro can you help to replace the numbers with teams names

using a linq query like:

var listeams = form t in DBcontext.teams select new {t.team_name};

I wouldn’t use LINQ for that. You can probably use LINQ, but it will be unnecessarily complicated and confusing.

Instead, just loop through the results and use the values in the result array as indices into the array holding the team names. See this example:

Display team names for a round robin tournament in C#

Pingback: Display team names for a round robin tournament in C# - C# HelperC# Helper

Thanks man!

The array shuffling isn’t needed BTW, just add the index of the first loop and give it a %.

Here is my simplified PHP test version, enjoy: