# Swiss Pairing Algorithm

I am building a simple Android application for hosting Chess tournaments, I am having some difficulty implementing a Swiss paring algorithm, I need link to an article or a paper that explains how I can achieve that.

I would also like to know any ideas that can help me. I am following the rules in this Article Swiss Pairing Rules

I am particularly having problem ensuring that

1. For each player the difference between the number of black and the number of white games shall not be greater than 2 or less than –2. Each system may have exceptions to this rule in the last round of a tournament.

2. No player shall receive the same colour three times in a row. Each system may have exceptions to this rule in the last round of a tournament.

3. In general, a player is given the colour with which he played less games.

If colours are already balanced, then, in general, the player is given the colour that alternates from the last one with which he played.

Jeremy Bierema has written a Swiss pairing program and put it on Github. You could look at the code there which is in C++.

At any stage in the tournament you have to have a list of forbidden pairings.

The first, obvious entries are pairings that have occurred previously. The second, banning 3 whites in a row, is ww v ww since one of them will have to be white giving www. Similarly bb v bb is forbidden. Then the white/black imbalance, so 2 v 2 and -2 v -2 are forbidden since in each case one player will end up with a 3 or -3 imbalance.

So, for each player you need to keep track of previous opponents, colour sequence and colour imbalance. This gives you the information to know which pairings are forbidden. In the last round you should also perform the forbidden pairing check for colours and pair with this in mind if possible. If not then you can ignore colour restrictions in the last round only.

The "previous opponent" forbidden pairings are cumulative. The "colour" forbidden pairings change from round to round as the players' colour sequences and imbalances change.

There's a Python library called PyPair, that allows to easily generate the pairings, a short example:

Players = { 1:"Tim",
2:"Jeff",
3:"Kristi",
4:"Jacob",
5:"Doug",
6:"Karen",
7:"David"}

to = Tournament()

for player in Players: