Reputation: 43
I'm trying to code a Double Elimination tournament where the brackets are based on mod 4. The first round should handle all of the byes so that after round 2 there will be no more byes. I'm having a hard time trying to figure out the actual math behind determining the amount of byes I need. If anyone can help me with the math behind this it would be greatly appreciated.
There are 4 possible answers for anything mod 4 (0,1,2,3) I need to handle byes for 1,2,3.
An example of what I mean is 13 players so (13%4=1) so the round 1 bracket should look like 1vs2 2vs3 3vs4 4vs5 5vs6
and round 2 is 7vs winner 8vs winner 9vs winner winner vs winner and then you have the losers bracket
Basically if your familiar with the website Challenge, I want to generate my brackets similar to them, but I can't figure out the math behind their determining of the byes.
If anybody has done something similar to this I would greatly appreciate his help.
Upvotes: 3
Views: 4233
Reputation: 27282
Where N
is the number of competitors, the number of rounds will be:
nRounds = Math.Ceiling( Math.Log( N, 2 ) );
The number of slots in the first round will be:
firstRoundSlots = Math.Pow( 2, nRounds );
The top competitors get byes, so in your example, there are 13 competitors in the round of 16, so the top 3 competitors get byes. In other words, the number of byes are firstRoundSlots - N
.
The order of the bouts is a little more complicated. Basically the idea is that the finals bout is the best competitor vs. the second-best competitor. In the semifinal, the best competitor squares off against the third best competitor, and the second best competitor squares off against the 4th best competitor. And so on and so forth. So it's easiest to work backwards from the finals to generate the order.
However, if you want to know an algorithm for generating the bout orders in reverse (i.e., starting with round 1 and working your way towards the finals), I wrote a blog post about that here:
Upvotes: 3
Reputation: 7807
Simplest algorithm I can think of:
Upvotes: 0
Reputation: 12670
I'll work you through how I solved this.
You want a number of players in round 2 that is a power of 2.
The number of players in round 2 is: (matches in round 1)/2 + byes)
Let P be the number of players.
2^n = (P-byes)/2 + byes
2^(n+1) = P-byes + 2*byes
2^(u) = P + byes
So find the smallest u
s.t. 2^u >= P
, there are then 2^u-P
byes.
example cases:
7 -> 2^u=8 -> (8-7) -> 1 bye
1 bye, 3 matches -> 4 players in round 2
It's not mod 4, compare 9 players to 13: 9 -> 2^u=16 -> (16-9) -> 7 byes 13 -> 2^u=16 -> (16-13) -> 3 byes
A more interesting question would be how to arrange for the minimum number of byes, allowing for some in other rounds than the first.
Upvotes: 0