Searching an algorithm to make "matchs"
For a little cards tournament where we play in teams (2vs2), I need to create a "plan" of who is going to play against whom.
"Rules" are:
- We have a number of teams, each consisting of a pair of players.
- We have several "rounds" in the tournament.
- The number of rounds is lower than the number of teams.
The goal is to make a plan in which no team will play twice against another team.
I tried the "heavy" way with backtracking, but as I thought, the complexity is big and we quickly have an huge number of possibilities to calculate, so I am looking for an algor开发者_JS百科ithm which is able to make me a plan quickly.
Here is an example of what I want for the output, it has been generated with my "heavy way":
Tournament with 16 teams and 10 rounds
Round 0
Team 0 versus Team 1
Team 2 versus Team 3
Team 4 versus Team 5
Team 6 versus Team 7
Team 8 versus Team 9
Team 10 versus Team 11
Team 12 versus Team 13
Team 14 versus Team 15
Round 1
Team 0 versus Team 2
Team 1 versus Team 3
Team 4 versus Team 6
Team 5 versus Team 7
Team 8 versus Team 10
Team 9 versus Team 11
Team 12 versus Team 14
Team 13 versus Team 15
Round 2
Team 0 versus Team 3
Team 1 versus Team 2
Team 4 versus Team 7
Team 5 versus Team 6
Team 8 versus Team 11
Team 9 versus Team 10
Team 12 versus Team 15
Team 13 versus Team 14
Round 3
Team 0 versus Team 4
Team 1 versus Team 5
Team 2 versus Team 6
Team 3 versus Team 7
Team 8 versus Team 12
Team 9 versus Team 13
Team 10 versus Team 14
Team 11 versus Team 15
Round 4
Team 0 versus Team 5
Team 1 versus Team 4
Team 2 versus Team 7
Team 3 versus Team 6
Team 8 versus Team 13
Team 9 versus Team 12
Team 10 versus Team 15
Team 11 versus Team 14
Round 5
Team 0 versus Team 6
Team 1 versus Team 7
Team 2 versus Team 4
Team 3 versus Team 5
Team 8 versus Team 14
Team 9 versus Team 15
Team 10 versus Team 12
Team 11 versus Team 13
Round 6
Team 0 versus Team 7
Team 1 versus Team 6
Team 2 versus Team 5
Team 3 versus Team 4
Team 8 versus Team 15
Team 9 versus Team 14
Team 10 versus Team 13
Team 11 versus Team 12
Round 7
Team 0 versus Team 8
Team 1 versus Team 9
Team 2 versus Team 10
Team 3 versus Team 11
Team 4 versus Team 12
Team 5 versus Team 13
Team 6 versus Team 14
Team 7 versus Team 15
Round 8
Team 0 versus Team 9
Team 1 versus Team 8
Team 2 versus Team 11
Team 3 versus Team 10
Team 4 versus Team 13
Team 5 versus Team 12
Team 6 versus Team 15
Team 7 versus Team 14
Round 9
Team 0 versus Team 10
Team 1 versus Team 11
Team 2 versus Team 8
Team 3 versus Team 9
Team 4 versus Team 14
Team 5 versus Team 15
Team 6 versus Team 12
Team 7 versus Team 13
If you just want a few predetermined rounds, seed the participants randomly, then apply round robin. The easiest way to do that is to arrange symbols for the teams like this:
A B C D
E F G H
So, in the first round, the pairings are A
-E
, B
-F
, etc.. Then A
is fixed, and all the others rotate one place clockwise:
A E B C
F G H D
Repeat.
If the number of rounds is less than n - 1, I would suggest the swiss system. You can do the pairings by hand, but there are many pairing programs already out there, some using heuristics, some a variant of Edmond's "blossom" minimum weight perfect matching algorithm.
You can adapt a selection sort algorithm to generate the permutations. I did something like this a long time back. See the "Permutations in pairs" section of this article.
The code's in Pascal, but should be easy enough to convert to C#.
精彩评论