Monday, 19 August 2013

Latin Square Algorithm with certain Pattern For Bracket Play

Latin Square Algorithm with certain Pattern For Bracket Play

I'm trying to figure out an algorithm in C# that will accomplish creating
the latin squares below for a set number of participants up to 128. I just
need to translate this to code. The latin squares below make sure a
balanced number of participants in a pool are distributed evenly in a
basketball bracket.
If you see below, I generate a sequence that is standard in brackets, 12,
1423, 18452736, and so on. The arrays below are created by transposing
arrays.
As you can see in the 4x4 array 14 is reversed to 41 under it, while 23 is
reversed to 32 under it. I then transpose the NE quadrant to the SW
quadrant and vice versa for the other. The 8 quadrant does the same thing
but is a little more complicated to translate it to code. It does the same
thing as a 4x4 for each 4 pair, then it does a array transpose for the
bottom 8x4 array from the top 8x4 array. Any help please?
2 Participants
12
21
4 Participants
1423
4132
2314
3241
8 Participants
18452736
81547262
45183627
54816372
27361845
72638154
36274518
63725481
Here is what I have so far, not dynamic at all and doesnt work with the 8x8.
public int[,] GetLatinSquare(List<int?> maxPositions)
{
var n = maxPositions.Count();
var latinSquare = new int[n, n];
for (int i = 0; i < n; i++)
{
var maxPosition = maxPositions[i];
if (maxPosition != null)
{
latinSquare[0, i] = maxPosition.Value;
}
}
for (var i = 0; i < n; i = i+2)
{
latinSquare[1, i] = latinSquare[0, i + 1];
latinSquare[1, i + 1] = latinSquare[0, i];
}
if (maxPositions.Count() > 2)
{
for (var i = 0; i < n; i = i + n/2)
{
latinSquare[2, i] = latinSquare[0, i + 2];
latinSquare[2, i + 1] = latinSquare[0, i + 3];
latinSquare[2, i + 2] = latinSquare[0, i + 0];
latinSquare[2, i + 3] = latinSquare[0, i + 1];
}
for (var i = 0; i < n; i = i + 2)
{
latinSquare[3, i] = latinSquare[2, i + 1];
latinSquare[3, i + 1] = latinSquare[2, i];
}
}
return latinSquare;
}

No comments:

Post a Comment