3

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!

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
MehtabC
  • 50
  • 1
  • 1
  • 4
  • I don't think clockwise is the word you are looking for. Clockwise is used to describe the direction of rotation (as in how a clock's hands move). Could you provide an example? – thatidiotguy Oct 20 '14 at 17:29
  • If you look at the answer below from Matt Jones, I think you will understand what I meant. The algorithm is easier to understand if you imagine a real life scenario of maybe a Chess tournament. You play a hand, you move clockwise, you play another, you move again. The issue I have is to translate this movement into code. – MehtabC Oct 20 '14 at 17:33
  • So your question is how to implement this programatically.. Do you have anything you have tried yet? – Matt Jones Oct 20 '14 at 17:34
  • No. I'm drawing a blank on how to even start. All I have is putting all my teams into an array, and I know that 1 will play 10, 2-9, etc. I'm thinking something along the lines of shifting the position of the team along the array, but the issue there is that moving clockwise is a different distance for each location, and that also changes depending on the # of teams in total, which can be anywhere from 2-10. – MehtabC Oct 20 '14 at 17:37

1 Answers1

12

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.

Matt Jones
  • 511
  • 3
  • 12
  • My issue is translating that very concept you mentioned into code. I get how it works, in essence, but I don't know how to portray that in my code. Where you say the word rotate, that is where my issue is. – MehtabC Oct 20 '14 at 17:34
  • I'm still kind of confused, but your code definitely helps me start somewhere, so thanks. – MehtabC Oct 20 '14 at 17:56
  • Easiest way to understand something like this is to trace it through on paper and see what it is doing. – Matt Jones Oct 20 '14 at 18:10
  • Nice code, I am trying to make something like it, but with "180 minutes matches", e.g: day 1: TeamA vs TeamB, TeamC vs TeamD, and after all of these, on the day 4: TeamB vs TeamA, TeamD vs TeamC... Any help? Am I clear? – Alan Donizete Jul 18 '16 at 23:38