Latin Squares
The content of this note mostly belongs to the first section of TAoCP, Vol. 4A.
Definition
Given a square matrix of size
Examples
We have 16 cards which comprise a combination of 4 ranks, i.e., J, K, Q, A, and 4 suits:

Applications
While studying mathematics, sometimes we cannot find an obvious application to motivate ourselves to study the concept. However, since I am a computer science engineer who is more pragmatic than mathematically inclined, I am especially interested in concepts which have applications in real life. Regarding the Latin square, we get several:
- Sudoku: a variant of the Latin square is one of the most popular puzzles in the world.
- Design of experiments.
- Error correcting codes.
- Cryptography.
War Story
In 1779, a puzzle similar to the aforementioned example attracted the famous mathematician Leonhard Euler:
Thirty-six officers of six different ranks, taken from six different regiments, want to march in a
formation so that each row and each column will contain one officer of each rank and one of each regiment. How can they do it?
Even though he was almost blind, Euler was determined to solve the puzzle and tried to generalize the problem for other cases
However, the puzzle Euler tackled is quite different compared to the definition given in the previous section. More precisely, let
and
Fig. 1 becomes:
Two Latin squares are orthogonal if, when we combine two elements with the same position together, each element in the new matrix is unique. The puzzle of Euler is: given a Latin square, how do we find another Latin matrix which is orthogonal to the given one?
The combined matrix is called a Graeco-Latin matrix.
In order to prove Euler’s conjecture, mathematicians spent almost 180 years with the support of computers. In 1957, Paige and Tompkins programmed on SWAC to find a counterexample with
In 1960, Parker et al. proved that it is possible to find an orthogonal Latin square when
0 1 2 3 4 5 6 7 8 9
1 8 3 2 5 4 7 6 9 0
2 9 5 6 3 0 8 4 7 1
3 7 0 9 8 6 1 5 2 4
4 6 7 5 2 9 0 8 1 3
5 0 9 4 7 8 3 1 6 2
6 5 4 7 1 3 2 9 0 8
7 4 1 8 0 2 9 3 5 6
8 3 6 0 9 1 5 2 4 7
9 2 8 1 6 7 4 0 3 5
One instance of the solution (TAoCP):
0 2 8 5 9 4 7 3 6 1
1 7 4 9 3 6 5 0 2 8
2 5 6 4 8 7 0 1 9 3
3 6 9 0 4 5 8 2 1 7
4 8 1 7 5 3 6 9 0 2
5 1 7 8 0 2 9 4 3 6
6 9 0 2 7 1 3 8 4 5
7 3 5 1 2 0 4 6 8 9
8 0 2 3 6 9 1 7 5 4
9 4 3 6 1 8 2 5 7 0
Transversals
Parker utilized a concept called transversal which was previously proposed by Euler. A transversal is a way to choose a sequence of
For example: 0859734216 means that we choose 0 in column 1, 8 in column 2, 5 in column 3, etc. Suppose that
The algorithm below counts the number of transversals of a Latin square.
typedef vector<vector<int>> mat;
class transversal {
private:
int n;
vector<bool> row;
vector<bool> nums;
mat input;
int cnt;
public:
transversal(const mat& in_): n(in_.size()), input(in_),
row(n, false), nums(n, false), cnt(0) {
assert(in_.size() == in_[0].size());
}
void print() { // debugging
for (auto& rows: input) {
for (auto& e: rows) cout << e << " ";
cout << endl;
}
}
int compute(int idx) {
cnt = 0;
fill(row.begin(), row.end(), false);
fill(nums.begin(), nums.end(), false);
nums[input[idx][0]] = true;
row[idx] = true;
comp(1);
return cnt;
}
private:
void comp(int idx) { // idx = index of column
if (idx == n) {
cnt++;
return;
}
for (int i = 0; i < n; ++i) {
if (!row[i] && !nums[input[i][idx]] ) {
row[i] = true;
nums[input[i][idx]] = true;
comp(idx+1);
row[i] = false;
nums[input[i][idx]] = false;
}
}
}
};
From the above input, we get the output:
79 96 76 87 70 84 83 75 95 63
It is exactly the same as in the book. We can later modify the code to find all transversals given an input. After that, for every transversal
References
- Knuth, Donald E. The Art of Computer Programming, Volume 4A: Combinatorial Algorithms, Part 1.
- Latin square - Wikipedia
- http://elscorcho.50webs.com/latinsquares.pdf