Round Robin Algorithm Implementation Java

10,725

One way to do this is the following:

Number the teams 1..n. (n=8 in this example) Write all the teams in two rows.

The columns show which teams will play in that round (1 vs 8, 2 vs 7, ...).

1 2 3 4
8 7 6 5

Now, keep 1 fixed, but rotate (clockwise) all the other teams. In week 2, you get

1 8 2 3
7 6 5 4

and in week 3, you get

1 7 8 2
6 5 4 3

This continues through week n-1, in this case,

1 3 4 5
2 8 7 6

If n is odd, do the same thing but add a dummy team. Whoever is matched against the dummy team gets a bye that week.

For example:

1 2 3 4 5
9 8 7 6 0         (0 being the bye)

Continue the rotation as above.

Code example:

void ListMatches(List<string> ListTeam)
{
    if (ListTeam.Count % 2 != 0)
    {
        ListTeam.Add("Bye"); // If odd number of teams add a dummy
    }

    int numDays = (numTeams - 1); // Days needed to complete tournament
    int halfSize = numTeams / 2;

    List<string> teams = new List<string>();

    teams.AddRange(ListTeam); // Add teams to List and remove the first team
    teams.RemoveAt=(0);

    int teamsSize = teams.Count;

    for (int day = 0; day < numDays; day++)
    {
        Console.WriteLine("Day {0}", (day + 1));

        int teamIdx = day % teamsSize;

        Console.WriteLine("{0} vs {1}", teams[teamIdx], ListTeam[0]);

        for (int idx = 1; idx < halfSize; idx++)
        {               
            int firstTeam = (day + idx) % teamsSize;
            int secondTeam = (day  + teamsSize - idx) % teamsSize;
            Console.WriteLine("{0} vs {1}", teams[firstTeam], teams[secondTeam]);
        }
    }
}

Basically what this is doing is putting all teams except the first in a list. Next, increase the index you are starting at by 1 each day. For this team you are focused on, you match this team with Team1. For the next team in the list, you match it to the same index starting from the other end of the list, but treating the first index spot at whatever day it is + 1.

Share:
10,725
MehtabC
Author by

MehtabC

Updated on June 14, 2022

Comments

  • MehtabC
    MehtabC almost 2 years

    My issue is fairly simple, I think, but I feel that I need some different perspectives, because I cannot seem to translate this algorithm into code.

    I need to make a sports team schedule, where n teams (in this case, 10 teams) play in a round robin format. The rules follow basic round-robin formats, where one team can only play one other team at a given time, and all teams must play all other teams once.

    I have found that the algorithm is to hold team 1 in the spot, and rotate the rest clockwise. A dummy team can be used to handle odd numbers for n. The issue arises in the "clockwise" part of the algorithm. I have no idea how I can translate the concept of rotate clockwise to my teams. For example, if I have them split it in an array TeamArray, and TeamArray[0] plays TeamArray[10], etc, in week 1, how can I make them move clockwise for week 2?

    I'm not looking for a handout answer, but rather for some help to look at this algorithm in a creative way so that I can translate the concept of moving the teams clockwise.

    I appreciate all the help and would be glad to answer anything that might be confusing in my initial question. Thanks!