For one project, I need to compute matchs planning between several teams.
Requirement:
- I’ve a pair number of teams
- All teams have to play against every other team once(and only once)
- All team plays at the same time
E.g.
With 4teams(A,B, C and D), I would like to be able to compute this:
Round 1
- A vs B
- C vs D
Round 2
- A vs D
- B vs C
Round 3
- A vs C
- B vs D
The problem is that, there is some choice in round X which will make impossible any match in round X+1(teams have already played against other teams).
I think I can use some backtracking technique, but I’m searching if there is an algorithm for this?
This will be implemented in c#.
Have you any idea about how to do this?
Try round-robin. This is a simple scheduling algorithm to share timeslots among processes, but this problem reminds me it somehow.
EDIT
Now here is an implementation of Round-robin tournament. If we have ODD number of teams, we must insert a dummy team, otherwise there would be a team without opponent. As number of rounds are EVEN, number of total rounds are (NumberOfTeams-1). At the very beginning we set-up the first round:
A B C D E F G H
H G F E D C B A
So, Team A – H, Team B – G, etc.
From now, we keep one team fixed, for instance A. Then we shift A_Side teams from the 2nd position to the right. The last team will come into position 2. ( A B C D E F G H will be A H B C D E F G). See rotate_A_side() recursive method (just for fun).
The half of the B_Sides are shifted left. This will make H G F E D – G F E D.
As team selection is symmetric (A plays with H, then H plays wiht A), the upper half of B_Side is the reverse copy of the A_Side low part teams. So, D C B A will be C B H A). See rotate_B_side().
So, Round 2 is:
A H B C D E F G
G F E D C B H A
To give all rounds, simply repeat the aforementioned shifting steps. See NextRound()
Here is a c# class which implements the algorithm:
From your code, you can use it like:
6 Teams gave me the following output:
Round #1 A-F ; B-E ; C-D ; D-C ; E-B ; F-A ;
Round #2 A-E ; F-D ; B-C ; C-B ; D-F ; E-A ;
Round #3 A-D ; E-C ; F-B ; B-F ; C-E ; D-A ;
Round #4 A-C ; D-B ; E-F ; F-E ; B-D ; C-A ;
Round #5 A-B ; C-F ; D-E ; E-D ; F-C ; B-A ;
I replaced Team 1 with A, etc.
rotate_B_Side() should be refined, this is a quick approach.