Generate a schedule for a round robin tournament in C#

[round robin tournament]

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.

    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;
                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);
        return GenerateRoundRobinOdd(num_teams);

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in algorithms, combinatorics, mathematics and tagged , , , , , , , , , , , . Bookmark the permalink.

8 Responses to Generate a schedule for a round robin tournament in C#

  1. Andy Boot says:

    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.

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

  3. Sandeep Rawat says:

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

    • RodStephens says:

      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.

  4. jason says:

    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};

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

  6. Thanks a lot says:

    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:

    $matchdays = array();
    $matchesPerDay = $teamcount/2;
    // matchdays
    for( $i=0; $i<$teamcount-1; ++$i )
        for( $j=0; $j<$matchesPerDay; ++$j )
            $homeIndex = ($matchesPerDay-$j+$i) % $teamcount;
            $awayIndex = ($matchesPerDay+$j+1+$i) % $teamcount;
            // swap every second day, so home and away games change every matchday
            if( 0 == $i%2 )
                $temp = $homeIndex;
                $homeIndex = $awayIndex;
                $awayIndex = $temp;
            $matchday[ $i ] .= $homeIndex . ' vs ' . $awayIndex . '’;
    ksort( $matchday ); // sort the array
    // echo / output the matches of every matchday
    foreach( $matchday as $mday => $matches )
        echo ‘Matchday: ‘ . $mday . ” . $matches . ”;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.