Last active
June 9, 2022 02:14
-
-
Save ZacharyPatten/5c8509ac705cd5fa1f8e09e29b7a9543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int numberOfTeams = 9; | |
Console.WriteLine("Matches:"); | |
(int, int)[] matches = Statics.ScheduleRoundRobinMatches(numberOfTeams); | |
for (int i = 0; i < matches.Length; i++) | |
{ | |
Console.WriteLine($"{i + 1}. #{matches[i].Item1} vs #{matches[i].Item2}"); | |
} | |
public static class Statics | |
{ | |
/// <summary> | |
/// Schedules matches between a set number of teams where each team plays each other | |
/// team once ordered to maximize resting time between each team's matches. | |
/// </summary> | |
/// <param name="numberOfTeams">The number of teams to schedule matches for.</param> | |
/// <returns> | |
/// All the matches necessary for each team to play each other team once ordered | |
/// to maximize resting time between each team's matches. | |
/// </returns> | |
public static (int, int)[] ScheduleRoundRobinMatches(int numberOfTeams) | |
{ | |
int size = numberOfTeams; | |
int matchCount = (size - 1) * size / 2; | |
(int, int)[] matches = new (int, int)[matchCount]; | |
for (int i = 1, k = 0; i <= size; i++) | |
{ | |
for (int j = i + 1; j <= size; j++, k++) | |
{ | |
matches[k] = (i, j); | |
} | |
} | |
int[] counts = new int[size]; | |
int[] bias = new int[size]; | |
(int, int)[] scheduled = new (int, int)[matchCount]; | |
for (int i = 0; i < matchCount; i++) | |
{ | |
int index = -1; | |
int priority = int.MaxValue; | |
for (int j = 0; j < matchCount - i; j++) | |
{ | |
int currentPriority = | |
bias[matches[j].Item1 - 1] + counts[matches[j].Item1 - 1] + | |
bias[matches[j].Item2 - 1] + counts[matches[j].Item2 - 1]; | |
if (currentPriority < priority) | |
{ | |
index = j; | |
priority = currentPriority; | |
} | |
} | |
scheduled[i] = matches[index]; | |
for (int j = index; j < matchCount - i - 1; j++) | |
{ | |
(matches[j], matches[j + 1]) = (matches[j + 1], matches[j]); | |
} | |
for (int j = 0; j < size; j++) | |
{ | |
bias[j]--; | |
} | |
bias[scheduled[i].Item1 - 1] = 0; | |
bias[scheduled[i].Item2 - 1] = 0; | |
counts[scheduled[i].Item1 - 1]++; | |
counts[scheduled[i].Item2 - 1]++; | |
} | |
return scheduled; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment