Double round robin tournament
Asked Answered
R

3

6

I am developing a sport torunament in Java based on round robin scheduling algorithm. For n teams I want to generate 2(n-1) rounds with n/2 matches. That is that every team must play a match in a round, and every 2 teams meet twice, once away and once home. I managed to implement the algoritm except for the home/away part. I am able to generate the rounds, but can not "swap" the teams in the second half of rounds so they play both away and home.

Here is what I have so far:

public class sports {
  public static void main(String[] args) {
    //obtain the number of teams from user input
    Scanner input = new Scanner(System.in);
    System.out.print("How many teams should the fixture table have?");
    int teams = input.nextInt();
    // Generate the schedule using round robin algorithm.
    int totalRounds = (teams - 1) * 2;
    int matchesPerRound = teams / 2;
    String[][] rounds = new String[totalRounds][matchesPerRound];
    for (int round = 0; round < totalRounds; round++) {
      for (int match = 0; match < matchesPerRound; match++) {
        int home = (round + match) % (teams - 1);
        int away = (teams - 1 - match + round) % (teams - 1);
        // Last team stays in the same place
        // while the others rotate around it.
        if (match == 0) {
          away = teams - 1;
        }
        // Add one so teams are number 1 to teams
        // not 0 to teams - 1 upon display.
        rounds[round][match] = ("team " + (home + 1)
            + " plays against team " + (away + 1));
      }
    }
    // Display the rounds
    for (int i = 0; i < rounds.length; i++) {
      System.out.println("Round " + (i + 1));
      System.out.println(Arrays.asList(rounds[i]));
      System.out.println();
    }
  }
}

Don't mind even/odd number of teams, for now I am only interested in even teams number.

Rake answered 31/12, 2013 at 8:39 Comment(0)
D
3

To codify True Soft's answer,

String roundString;
if (round < halfRoundMark) {
    roundString = ("team " + (home + 1)
            + " plays against team " + (away + 1));
} else {
    roundString = ("team " + (away + 1)
            + " plays against team " + (home + 1));
}
rounds[round][match] = roundString;

where

int halfRoundMark = (totalRounds / 2);
Deejay answered 31/12, 2013 at 8:52 Comment(0)
Z
0

You have to see when you get to the half of the rounds, and then switch the home and away teams.

Zoniazoning answered 31/12, 2013 at 8:50 Comment(0)
B
0

Pleas Refer this link which described Round Robin Algorithm for "Odd-Number of teams" as well as "Even-Number of teams".

Ref this link:- https://nrich.maths.org/1443

1) Odd Number of teams :- For Odd number of teams, Consider following formulas and conditions

Abbreviations :-

TNR = Total Number Of Rounds
NOT = Number Of Teams
MPR = Match Per Round

Formulas :-

Total Number Of Rounds = Number Of Teams
Match Per Round = [ ( Number Of Teams - 1 ) / 2 ]

Conditions :- There must be only one team in round, which gets bye(would not play game). So each round has only one bye and each team will play once in round with other team.

Lets consider this algorithm's implementation in c# (Odd-Number Of Teams)

Suppose we have 5 Teams

Teams :-

Barcilona (0)
Brazil   (1)
Madrid  (2)
Colambo  (3)
Woshington DC  (4)

Here ,

Number Of Teams (NOT)= 5

Total Number Of Rounds (TNR) = 5

Matches Per Round (MPR) = [ ( NOT - 1 ) / 2 ] = [ ( 5 - 1 ) /2 ] = 2

Now Conside the link in which each round has one team which gets bye, and other team play once in round with another team.

now put all teams in array named Teams

int[] teams = new int[NOT]; 
so, int[] teams = new int[5];
now teams = [0,1,2,3,4]

now we will rotate array by 1 step left while moving on next round and teams[0] will always gets By and play games between other team but the problem is that how we can decide which team plays game with which team. so to resolve those issue, refere Reference Link. So you can identify that In round 0, matched will be played with following teams

Teams[0] = 0 = Barcilona Gets Bye(Not played game)
Teams[1] - Teams[4] = (Brazil - Woshington DC)
Teams[2] - Teams[3] = (Madrid - Colambo)

so, consider the following implementation

public class Teams {
    public int Team1 { get; set; }
    public int Team2 { get; set; }
}

//Dictionary< round , matches >
var matchScheduling = new Dictionary<int, List<Teams>>();

for (int round = 0; round < TNR; round++) {
    List<Teams> matches = new List<Teams>();
    int team1 = teams[0];
    int team2 = -1;
    matches.Add(new Teams() { Team1 = team1, Team2 = team2 });

    for (int match = 0; match < MPR; match++) {
        team1 = teams[match + 1];
        team2 = teams[(NOT.Length - 1) - match];
        roundTeams.Add(new Teams() { Team1 = team1, Team2 = team2 });
    }

    matchScheduling.Add(round, roundTeams);

    //Now for next round, Rotate team Array
    int length = teams.Length;
    int tmp = teams[length - 1];
    for (int i = length - 1; i > 0; i--) {
        teams[i] = teams[i - 1];
    }
    teams[0] = tmp;
}
Braga answered 1/3, 2019 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.