Algorithm to select random pairs, schedule matchups - ruby

I'm working in Ruby, but I think this question is best asked agnostic of language. It may be assumed that we have access to basic list/array functions, as well as a "random" number generator. Here's what I'd like to be able to do:
Given a collection of n teams, with n even,
Randomly pair each team with an opponent, such that every team is part of exactly one pair. Call this ROUND 1.
Randomly generate n-2 subsequent rounds (ROUND 2 through ROUND n-1) such that:
Each round has the same property as the first (every team is a
member of one pair), and
After all the rounds, every team has faced every other team exactly once.
I imagine that algorithms for doing exactly this must be well known, but as a self-taught coder I'm having trouble figuring out how to find them.

I belive You are describing a round robin tournament. The wikipedia page gives an algorithm.
If You need a way to randomize the schedule, randomize team order, round order, etc.

Well not sure if this is the most efficient algorithm but:
Randomly assign N teams into two lists of same length n/2 (List1, List2)
Starting with i = 0:
Create pairs: List1[i],List2[i] = a team pair
Repeat for i = 1-> (n/2-1)
For rounds 2-> n/2-1:
Rotate List2, so that the first team in List2 is now at the end.
Repeat steps 2 through 5, until List2 has been cycled once.

This link was very helpful to me the last time I wrote a round robin scheduling algorithm. It includes a C implementation of a first fit algorithm for round robin pairings.
http://www.devenezia.com/downloads/round-robin/
In addition to the algorithm, he has some helpful links to other aspects of tournament scheduling (balancing home and away games, as well as rotating teams across fields/courts).
Note that you don't necessarily want a "random" order to the pairings in all cases. If, for example, you were scheduling a round robin soccer league for 8 games that only had 6 teams, then each team is going to have to play two other teams twice. If you want to make a more enjoyable season for everyone, you have to start worrying about seeding so that you don't have your top 2 teams clobbering the two weakest teams in their last two games. You'd be better off arranging for the extra games to be paired against teams of similar strength/seeding.

Based on info I found through Maniek's link, I went with the following:
A simple round robin algorithm that
a. Starts with pairings achieved by zipping [0,...,(n-1)/2] and [(n-1)/2 + 1,..., n-1]. (So, if n==10, we have 0 paired with 5, 1 with 6, etc.)
b. Rotates all but one team n-2 times clockwise until all teams have played each other. (So in round 2 we pair 1 with 6, 5 with 7, etc.)
Randomly assigns one of [0,..., n-1] to each of the teams.

Related

Algorithm for pairing players and allocating into courts

I am making an algorithm which allocates badminton players into games (2 x 2) in the following way:
Players are divided into pairs.
All possible pair combinations must be done so everyone plays with everyone. If there are 10 players, everyone will belong to 9 pairs.
So far, this is simple to implement.
Then, games should be allocated into 2 courts. This means, 2 games can be going on simultaneously but of course, a player can't be part of two games going on at the same time.
My algorithm idea was:
Create an array containing all possible pairs.
Allocate two pairs from the array into court 1 to play against each other. If the second pair has overlapping members with pair 1, take the next pair from the array. Iterate the array in order and remove allocated pairs from it.
Do the same for court 2 but also make sure that the pairs do not contain overlapping members with players in court 1.
Make a new round and continue from step 2.
This works quite well but as a side effect, on the last rounds, only court 1 will be used because it is impossible to find any more pairs into court 2 fulfilling the condition in step 3. So the capacity of court 2 will be wasted. I am a little unsure if it is even possible to find a perfect solution to this problem. If yes, how the algorithm could be improved?

probability of winning a special sing-elimination tournament using dynamic programming and bitmask

Let's say we have N teams in a tournament and based on historical data we know what is the probability of each team winning any other team .Lets put all the probabilities in a matrix called P . P[a][b] is the probability team a winning team b. It is obvious that P[a][a] = 0 and P[a][b] = 1-P[b][a].
In this tournament at every round, two of teams compete against each other and the loser is eliminated. This two team are chosen randomly (with equal possibility of each team being picked). So at the first round we have n teams, next n-1 teams and so on until only one team remains and becomes the champion. What is the probability of each team becoming the champion? ( 1 <= N <= 18).
At first when I didn't know how to approach the problem but after some reading and search and keeping in mind that max n is 18 I figured at that using Dynamic programming and Bitmask is the way to go. How ever I couldn't figure at a solution. Here are my problems:
I have really hard time to figure at what are the sub problems and what sub problems should not be recomputed, basically I can't find a well defined recursive ( or not recursive) equation for the problem
In bitmask+dp problems we usually define something like dp[mask][n] or dp[n][mask]. I tried different approaches to define the mask but since the general solution is not clear to me there was no success
Some guidance on this two problems would be very helpful.
This is not really a dynamic programming problem.
If you have a vector V that gives the probability of each player being in the game after n rounds, then you can calculate the player probabilities for n+1 rounds by:
V'i = 2/((18-n)(17-n)) * sum over all j!=i of [ViVjPi,j]
That first factor is the probability that any given available match will be chosen, which depends on the number of previous rounds, because each successive round has fewer players to match up.
The second part is the probability of the players being available for each match, times the probability that the current player will win.
Just do this calculation 17 times to get the player probabilities after 17 rounds, which is the answer you're looking for. You can even drop that first factor, and fix it at the end by normalizing the vector so that the probabilities sum to 1.

Optimizing a Combination Algorithm

I'm organizing a game tournament in a week. I began thinking about the best way to mathematically arrange the teams (so that they are really even, thus more competitive). So here's the data:
20 players in the event
Each player is assigned a skill level (high number = skilled)
4 teams of 5 players each (although I'd prefer to build a algorithm that takes these as
variables)
I'm using a computer to solve the problem
So, I have 20 players. I'd like to generate 4 teams with 5 players each. To do this, I'd like to generate a list of all possible team combinations. To evaluate a team combination, I:
Generate a combination of teams (a match)
Sum the total skill for each team based off the players in that team
Compare each team to each other, the highest difference between any two teams in the match is the "tolerance" level for that match. If the tolerance level is higher than a certain cap, the match is discarded
My current approach is to generate a base X number that is N digits long, where X is the number of teams I want, and N is the number of players. Then increment the base X number by 1, I'll get every possible team combination, and I can generate a list of matches that have low tolerance values.
The problem with this, as you probably know, is for 4 teams with 20 players, that's (4-1)^20 in base 3, which is 1E12 matches to check through. (This takes a long time on my computer). Is there a mathematical way to simplify this calculation to be doable in a short period of time?
By current method also allows for the possibility of uneven players spread across the number of teams, which is preferable. If this can't be present with a highly performant algorithm, then it's okay not to use it.
Try following approach:
For teams 1 to 4: Take the strongest player from the remaining
The the same in another direction: from 4 to 1
Again 1 to 4
Again 4 to 1
In the last round use random
This works well when player skills are distributed more or less evenly. If not, then the probability of bigger differences between teams is higher.

Finding subsets being used at most k times

Every now and then I read all those conspiracy theories about Lotto-based games being controlled and a computer browsing through the combinations chosen by the players and determining the non-used subset. It got me thinking - how would such algorithm have to work in order to determine such subsets really efficiently? Finding non-used numbers is definitely crossed out as is finding the least used because it's not necesserily providing us with a solution. Also, going deeper, how could an algorithm efficiently choose such a subset that it was used some k times by the players? Saying more formally:
We are given a set of 50 numbers 1 to 50. In the draw 6 numbers are picked.
INPUT: m subsets each consisting of 6 distinct numbers 1 to 50 each,
integer k (0<=k) being the maximum players having all of their 6
numbers correct.
OUTPUT: Subsets which make not more than k players win the jackpot ('winning' means all the numbers they chose were picked in the draw).
Is there any efficient algorithm which could calculate this without using a terrabyte HDD to store all the encounters of every possible 50!/(44!*6!) in the pessimistic case? Honestly, I can't think of any.
If I wanted to run such a conspirancy I would first of all acquire the list of submissions by players. Then I would generate random lottery selections and see how many winners would be produced by each such selection. Then just choose the random lottery selection most attractive to me. There is little point doing anything more sophisticated, because that is probably already powerful enough to be noticed by staticians.
If you want to corrupt the lottery it would probably be easier and safer to select a few competitors you favour and have them win the lottery. In (the book) "1984" I think the state simply announced imaginary lottery winners, with the announcement in each area announcing somebody outside the area. One of the ideas in "The Beckoning Lady" by Margery Allingham is of a gang who attempt to set up a racecourse so they can rig races to allow them to disguise bribes as winnings.
First of all, the total number of combinations (choosing 6 from 50) is not very large. It is about 16 million which can be easily handled.
For each combination keep a count of number of people who played it. While declaring a winner choose the combination that has less than k plays.
If the number within each subset are sorted, then you can treat your subsets as strings - sort them in lexicographical order, then it is easy to count how many players selected each subset (and which subsets were not selected at all). So the time is proportional to the number of players and not the number of numbers in the lottery.

I have an algorithm problem having to do with scheduling teams in rotation as fairly as possible

I have a project for school where I have to come up with an algorithm for scheduling 4 teams to play volleyball on one court, such that each team gets as close to the same amount of time as possible to play.
If you always have the winners stay in and rotate out the loser, then the 4th ranked team will never play and the #1 team always will.
The goal is to have everybody play the same amount of time.
The simplest answer is team 1 play team 2, then team 3 play team 4 and keep switching, but then team 1 never gets to play team 3 or 4 and so on.
So I'm trying to figure out an algorithm that will allow everybody to play everybody else at some point without having one team sit out a lot more than any other team.
Suggestions?
How about this: Make a hashtable H of size NC2, in this case, 6. It looks like:
H[12] = 0
H[13] = 0
H[14] = 0
H[23] = 0
H[24] = 0
H[34] = 0
I am assuming it would be trivial to generate the keys.
Now to schedule a game, scan through the hash and pick the key with the lowest value (one pass). The teams denoted by the key play the game and you increment the value by one.
EDIT:
To add another constraint that no team should wait too long, make another hash W:
W[1] = 0
W[2] = 0
W[3] = 0
W[4] = 0
After every game increment the W value for the team that did not play, by one.
Now when picking up the least played team if there are more than one team combo with low play score, take help from this hash to determine which team must play next.
well you should play 1-2 3-4, 1-3 2-4, 1-4 2-3 and then start all over again.
If there are N teams and you want all pairs of them to play once, then there are "N choose 2" = N*(N-1)/2 games you need to run.
To enumerate them, just put the teams in an ordered list and have the first team play every other team, then have the second team play all the teams below it in the list, and so on. If you want to spread the games out so teams have similar rest intervals between games, then see Knuth.
Check out the wikipedia entry on round robin scheduling.
pretend it's a small sports league, and repeat the "seasons"...
(in most sports leagues in Europe, all teams play against all other teams a couple of times during a season)
The REQUIREMENTS for the BALANCED ROUND ROBIN algorithm, for the Team championship scheduling may be found here:
Constellation Algorithm - Balanced Round Robin
The requirements of the algorithm can be defined by these four constraints:
1) All versus all
Each team must meet exactly once, and once only, the other teams in the division/ league.
If the division is composed of n teams, the championship takes place in the n-1 rounds.
2) Alternations HOME / AWAY rule
The sequence of alternations HOME / AWAY matches for every teams in the division league, should be retained if possible. For any team in the division league at most once in the sequence of consecutive matches HAHA, occurs the BREAK of the rhythm, i.e. HH or AA match in the two consecutive rounds.
3) The rule of the last slot number
The team with the highest slot number must always be positioned in the last row of the grid. For each subsequent iteration the highest slot number of grid alternates left and right position; left column (home) and right (away).
The system used to compose the league schedule is "counter-clockwise circuit." In the construction of matches in one round of the championship, a division with an even number of teams. If in a division is present odd number of teams, it will be inserted a BYE/Dummy team in the highest slot number of grid/ring.
4) HH and AA non-terminal and not initial
Cadence HH or AA must never happen at the beginning or at the end of the of matches for any team in the division.

Resources