Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

algorithms for tournament brackets (NCAA, etc.)

Tags:

c#

algorithm

I'm trying implement a bracket in my program (using C#/.NET MVC) and I am stuck trying to figure out some algorithm.

For example, I have a bracket like this with 8 entries (A,B,C,D,E,F,G,H)

Bracket Example

I'm trying to figure out if there's an algorithmic way to

  1. depending on # of entries, find # of games per round

  2. depending on # of entries, for a specific game #, what is the corresponding game # in the next round?

For example, in this case, for 8 entries, the example are:

  1. for round 1, there are 4 games. Round 2, 2 games. Round 3, 1 game
  2. game 2 in round 1 corresponds to game 5 in round 2.

I also thought about storing this info in a table, but it seems overkill since it never changes, but here it is anyway:

enter image description here

Any help will be greatly appreciated!

Cheers,

Dean

like image 452
netwire Avatar asked May 20 '11 11:05

netwire


People also ask

Who is the best bracket predictor?

Delphi Bracketology has been ranked consistently in the top of the BracketMatrix rankings of all bracketologists in the country.

How do you predict bracket scores?

The most common method is to award 1 point for correct predictions in the first round, 2 in the second round, 4 in the third, 8 in the fourth, 16 in the fifth, and 32 in the sixth and final round. However, you could also go with a point scheme like 1-2-3-4-5-6 to make each round weighted more evenly.

How are brackets seeded?

The process of entering or "planting" contestants on the brackets to start the tournament is referred to as seeding the tournament. When the bracket has been drawn and all the contestants have been assigned their starting position in the championship first round, the bracket can be said to have been "seeded."

How do brackets work in tournaments?

A tournament bracket pits an even number of teams in several rounds of games until there's only one team standing. A bracket contains a minimum of four games,, but usually the tournament field contains many more teams.


2 Answers

C# code for the first part of your question:

// N = Initial Team Count
// R = Zero-Based Round #
// Games = (N / (2 ^ R)) / 2
public double GamesPerRound(int totalTeams, int currentRound) {
    var result = (totalTeams / Math.Pow(2, currentRound)) / 2;

    // Happens if you exceed the maximum possible rounds given number of teams
    if (result < 1.0F) throw new InvalidOperationException();

    return result;
}

The next step in solving part (2) is to know the minimum game number for a given round. An intuitive way to do that would be through a for loop, but there's probably a better method:

var totalTeams = 8;
var selectedRound = 2;
var firstGame = 1;
// If we start with round 1, this doesn't execute and firstGame remains at 1
for (var currentRound = 1; currentRound < selectedRound; currentRound++) {
    var gamesPerRound = GamesPerRound(totalTeams, currentRound);
    firstGame += gamesPerRound;
}
like image 190
Yuck Avatar answered Sep 27 '22 19:09

Yuck


Quoting @Yuck who answered the first question perfectly.

C# code for the first part of your question:

// N = Initial Team Count
// R = Zero-Based Round #
// Games = (N / (2 ^ R)) / 2
public double GamesPerRound(int totalTeams, int currentRound) {
    var result = (totalTeams / Math.Pow(2, currentRound)) / 2;

    // Happens if you exceed the maximum possible rounds given number of teams
    if (result < 1.0F) throw new InvalidOperationException();

    return result;
}

Moving on to the second question:

//G = current game.
//T = total teams
//Next round game = (T / 2) + RoundedUp(G / 2)
//i. e.: G = 2, T = 8
       //Next round game = (8 / 2) + RoundedUp(2 / 2) = 5
public int NextGame(int totalTeams, int currentGame) {
    return (totalTeams / 2) + (int)Math.Ceiling((double)currentGame / 2);
}
like image 25
Diego Avatar answered Sep 27 '22 19:09

Diego