delphi3000.com Article
| How To Create A Round Robin Tournament Schedule | |
|---|---|
| Undertitle: | |
| URL: | http://www.delphi3000.com/article.asp?ID=1790 |
| Category: | Algorithm |
| Uploader: | Charles Doumar |
| Question: | How do I create a balanced round robin tournament schedule which ensures that every team plays every other team once with the minimum amount of idle time (a/k/a byes) and ensures that teams have an equal number of home and away games. |
| Answer: | The creation of a balanced round robin schedule is more complex than it first appears. There are several delphi round-robin components that are available on the web ... for example the one from engerning objects international at http://www.engineeringobjects.com/RndRobin.htm which costs $495.00. I wrote this algorithm to quickly and cheaply calculate a round robin schedule. Currently, the program has a hard-coded maximum number of 500 teams; however, this can easily be increased. If you have no need for such an algorithm then so be it, but I hope that anyone who needs to create such a schedule will give this algorithm a try. The CreateRoundRobin procedure will easily create a schedule for you.
For example assume that there are 5 teams, a valid schedule would be. Round 1 Home : 1 Away: 4 Home : 2 Away: 3 Home : 5 Away: -1 Round 2 Home : 5 Away: 3 Home : 1 Away: 2 Home : 4 Away: -1 Round 3 Home : 4 Away: 2 Home : 5 Away: 1 Home : 3 Away: -1 Round 4 Home : 3 Away: 1 Home : 4 Away: 5 Home : 2 Away: -1 Round 5 Home : 2 Away: 5 Home : 3 Away: 4 Home : 1 Away: -1 Most traditional round robin schedules look like the following, but there is no easy way to calculate which team is the home team (or has the home field) and which team is away team (or plays on the away field). team \ 1 2 3 4 5 6 7 8 9 10 round \............................................................ 1: 10 9 8 7 6 5 4 3 2 1 2: 6 10 9 8 7 1 5 4 3 2 3: 2 1 10 9 8 7 6 5 4 3 4: 7 3 2 10 9 8 1 6 5 4 5: 3 4 1 2 10 9 8 7 6 5 6: 8 5 4 3 2 10 9 1 7 6 7: 4 6 5 1 3 2 10 9 8 7 8: 9 7 6 5 4 3 2 10 1 8 9: 5 8 7 6 1 4 3 2 10 9 Instead of creating a tratitional [N by N] array, I have created an [N by 1/2 N by 2] array to keep track of the home and away aspects of the round robin tournament. const MaxTeams = 500; MaxRounds = MaxTeams; MaxGames = (MaxTeams+1) Div 2; Home = 1; Away = 2; Bye : Integer = -1; type TGameAry = Array[1..MaxTeams] of Integer; TRoundRobinAry = Array[1..MaxRounds,1..MaxGames,Home..Away] of Integer; .... procedure TForm1.CreateRoundRobin(var RoundRobinAry: TRoundRobinAry; const Teams: Integer); var GameAry : TGameAry; Half, Rounds, Bottom, SwitchBottom, TempGameValue, i, ii, iii : Integer; begin For i := 1 to MaxRounds do For ii := 1 to MaxGames do For iii := 1 to 2 do RoundRobinAry[i][ii][iii] := 0; IF ((Teams < 2) OR (Teams > MaxTeams)) Then exit; //Initilize Team Array with Team Numbers For i := 1 to Teams do GameAry[i] := i; if (Teams < MaxTeams) then For i := (Teams+1) to MaxTeams do GameAry[i] := 0; Half := (Teams-1) Div 2; IF ((Teams Mod 2)=0) Then Begin Rounds := Teams - 1; Bottom := Teams - 2; SwitchBottom := Bottom + 1; End else Begin Rounds := Teams; Bottom := Teams - 1; SwitchBottom := Teams; end; for i := 1 to Rounds do begin for ii := 1 to Half do begin RoundRobinAry[i][ii][Home] := GameAry[ii]; RoundRobinAry[i][ii][Away] := GameAry[Bottom-ii+1]; end; If ((Teams - Bottom) = 2) // if even number of teams then begin if i mod 2 = 0 then begin RoundRobinAry[i][Half+1][Home] := GameAry[SwitchBottom]; RoundRobinAry[i][Half+1][Away] := GameAry[Bottom+2]; end else begin RoundRobinAry[i][Half+1][Away] := GameAry[SwitchBottom]; RoundRobinAry[i][Half+1][Home] := GameAry[Bottom+2]; end end else begin // if odd number of teams then idle team gets a bye RoundRobinAry[i][Half+1][Home] := GameAry[SwitchBottom]; RoundRobinAry[i][Half+1][Away] := Bye; end; //rotate value of gameary TempGameValue := GameAry[SwitchBottom]; for ii := SwitchBottom downto 2 do GameAry[ii] := GameAry[ii-1]; GameAry[1] := TempGameValue; end; end; You can easily print the result to a richedit with a push of a button. procedure TForm1.Button1Click(Sender: TObject); const NoOfTeams : integer = 7; var roundrobinary : TRoundRobinAry; begin RichEdit1.Clear; CreateRoundRobin(RoundRobinAry,NoOfTeams); PrintFullChart(RoundRobinAry); //see below end; Procedure Tform1.PrintFullChart(const RoundRobinAry: TRoundRobinAry;); var i,ii : integer; begin richedit1.Lines.BeginUpdate; i := 1; ii := 1; repeat; Richedit1.Lines.Add(Format('Round : %d', [i])); while (RoundRobinAry[i][ii][Home] <> 0) do begin if RoundRobinAry[i][ii][away] <> - 1 then Richedit1.Lines.Add(Format('Home : %3.0d Away: %3.0d', [RoundRobinAry[i][ii][Home],RoundRobinAry[i][ii][Away]])) else Richedit1.Lines.Add(Format('BYE FOR TEAM : %3.0d', [RoundRobinAry[i][ii][Home]])); inc(ii); end; inc(i); ii := 1; until (RoundRobinAry[i][ii][Home] = 0); Richedit1.Lines.endupdate; end; |
| Copyright 2000 delphi3000.com Contact: delphi3000@bluestep.com' |
| Comments to this article |
|---|
byron noel llamas (Jul 3 2007 1:47PM) | ||
can you please give me a double round-robin tournament sched. for the bbasketball with 6 teams..i need it now..ASAP...tnx... RE: please help me.. |
Jean Robbins (Jan 22 2007 5:25PM) | ||
Can someone help me? I have 8 teams with only 3 courts. I need to make a schedule where everyone plays everyone. Thanks RE: round robin |
sam (Jan 8 2005 4:51PM) | ||
i need a 20 team round robin schedule RE: round robin RE: round robin RE: round robin |
Bill (Nov 9 2001 8:40PM) | ||
i am attempting to divide a 24 team golf league in a schedule that has either tri or quad meets ... everyone plays each other once in ONE round help RE: tournament |
Dave (Feb 20 2001 2:15PM) | ||
Excellent Code. Sure beats trying to think it up all myself. I ported it to Visual Basic and it worked great except the home / away logic needed some tweaking. The code as is, for example, will put team #1 in all homes games for the first half of the rounds and all away games for the next half. To alternate the home and away games I propose that you change this: for ii := 1 to Half do begin RoundRobinAry[i][ii][Home] := GameAry[ii]; RoundRobinAry[i][ii][Away] := GameAry[Bottom-ii+1]; end; To this: ====== for ii := 1 to Half do begin IF ((i Mod 2)=0) Then Begin RoundRobinAry[i][ii][Home] := GameAry[ii]; RoundRobinAry[i][ii][Away] := GameAry[Bottom-ii+1]; end else Begin RoundRobinAry[i][ii][Away] := GameAry[ii]; RoundRobinAry[i][ii][Home] := GameAry[Bottom-ii+1]; end; end; Dave Johnston RE: Excellent Code RE: Excellent Code |
Chris Allsop (Jan 24 2001 6:37PM) | ||
This code is superb. Its exactly what I was looking for, and as far as I am concerned, who cares what category its supposed to be in or what hassles people have with it. I think its a great bit of coding. Full credits to the author. WINNT RE: Great code |
S S B Magesh Puvananthiran (Jan 24 2001 4:46PM) | ||
It's really a good algorithm that can be implemented in any language... Good work!!! Thanx. Magesh. <%If Session("sSecurityLevel") >= 2 Then%> |
Mike Heydon (Jan 23 2001 1:59AM) | ||
Nice example of resolving permutations and combinations. It is very valid under the Algorythm section and it can be adapted for various functions. <%If Session("sSecurityLevel") >= 2 Then%> |
Bruce (Jan 19 2001 10:00AM) | ||
I for one loved this article. And to those critics out there delphi isn't just for databases. <%If Session("sSecurityLevel") >= 2 Then%> |
skinny (Jan 17 2001 10:00AM) | ||
There's an algorithm section under this site, which if you noticed is what it's filed under. What's your problem? RE: Algorithm's are important! RE: Algorithm's are important! |
Aqab Bin Talal (Jan 17 2001 8:08AM) | ||
shouldn't this artical be on www.algorithms3000.com ?? what does Robin Tournament Schedule (whatever that is) got to do with Delphi ?? Lighten up... Don't like it? Don't read it! |