MAS334 Combinatorics

14. Latin squares

Definition 14.1.

Given nonempty finite sets P, Q and N, a Latin rectangle L is a system of elements LijN for iP and jQ such that

  • (a)

    For each iP, all the elements in the row Li* are distinct. In more detail, if iP and j,jQ with jj, then we must have LijLij.

  • (b)

    For each jQ, all the elements in the column L*j are distinct. In more detail, if jQ and i,iP with ii, then we must have LijLij.

We will usually write p=|P| and q=|Q| and n=|N|. Often (but not always) we will have P={1,,p} or P={0,,p-1} and similarly for Q and N.

Remark 14.2.

In each column we have p entries from N which must all be different, and in each column we have q entries from N which must all be different. This can only work if p,qn. Thus, if we fix N with |N|=n, then the maximum possible size of a Latin rectangle is n×n.

Definition 14.3.

A Latin square of size n is a Latin rectangle with |P|=|Q|=|N|=n.

Note that in a Latin square each row contains n different entries taken from N, but |N|=n, so each row must contain each element of N precisely once. Similarly, each column must contain each element of N precisely once.

Example 14.4.

The matrix

[143521]

gives a Latin rectangle with P={1,2} and Q={1,2,3} and N={1,2,3,4,5} so p=2 and q=3 and n=5.

Example 14.5.

The matrix

[1432234141233214]

gives a Latin square with P=Q=N={1,2,3,4} and p=q=n=4.

Example 14.6.

Let G be any finite group, with |G|=n. Take P=Q=N=G and Lg,h=g*h. I claim that this is a Latin square. Indeed, if Lg,h=Lg,h then g*h=g*h and we can multiply on the left by g-1 to see that h=h. By the contrapositive, if hh then Lg,hLg,h. By a similar argument, if gg then Lg,hLg,h, as required.

Example 14.7.

As a special case of the above, we can consider the group /n={0,,n-1}, with addition mod n as the group operation. This gives a Latin square with P=Q=N={0,,n-1} and Lij=i+j(modn). For example, when n=5 we get

L=[0123412340234013401240123].

This example shows that for any n, there is at least one n×n Latin square.

Theorem 14.8.

Let L be a Latin rectangle with p<q=n (so L has the maximum possible width, but not the maximum possible height). Then L can be extended by adding extra rows to make an n×n Latin square.

Remark 14.9.

To prove this theorem and the next theorem, we will apply Hall’s Theorem and related results to a certain matching problem. This can be interpreted as a job allocation problem, in the following way. Suppose that N is a set of n students on a work experience scheme, and that Q is a set of n jobs, and that P={1,,n}. Each student is supposed to do each of the jobs for one day. On day i, job j is done by student Lij. No student can do more than one job at the same time, so for fixed i, the entries Lij must all be different. No student does the same job more than once, so for fixed j, the entries Lij must all be different. Thus, the matrix L must be a Latin square.

In the context of Theorem 14.8, we have constructed the rota for days 1 to p, and our task is to complete the rota for days p+1 to n.

The proof of Theorems 14.8 and 14.22 will also depend on some extra definitions which we now explain.

Definition 14.10.

Let L be a Latin rectangle with parameters p,q,n. For kN we let mL(k) denote the number of occurrences of k in L, and we call this the multiplicity of k. We also put eL(k)=mL(k)+n-p-q and call this the excess of k.

Example 14.11.

For L=[14355314] we have (p,q,n)=(2,4,5) so n-p-q=-1 and

mL(1) =2 mL(2) =0 mL(3) =2 mL(4) =2 mL(5) =2
eL(1) =1 eL(2) =-1 eL(3) =1 eL(4) =1 eL(5) =1.
Remark 14.12.

The occurrences of k in L must appear in different rows, so mL(k) can also be described as the number of rows that contain k. Similarly, the occurrences of k in L must appear in different columns, so mL(k) can also be described as the number of columns that contain k.

Lemma 14.13.

Suppose that q=n, so that L has the maximum possible width. Then we have mL(k)=p and eL(k)=0 for all k. Similarly, if p=n (so that L has the maximum possible height) then mL(k)=q and eL(k)=0 for all k.

Proof.

Suppose that q=n. Then each row has n different elements but |N|=n so each element kN must occur precisely once in each of the p rows. From this we see that mL(k)=p and so eL(k)=p+n-p-q. As q=n this simplifies to eL(k)=0. The case where p=n is essentially the same. ∎

Proof of Theorem 14.8.

Let L be a p×n Latin rectangle. It will be enough to show that we can add one more row to get a (p+1)×n Latin rectangle, because we can then repeat the process if necessary. We will interpret the problem as in Remark 14.9.

For jQ, let CjN be the set of students who are allowed to do job j on day p+1. These are just the students who have not already done job j on any of days 1,,p, or in other words

Cj=N{L1j,,Lpj}.

We are assuming that L is a Latin rectangle, so we have followed the rules on days 1 to p, so students L1j,,Lpj must all be different, so |Cj|=n-p. To make the new row, we just need to solve the usual job allocation problem with candidate sets Cj. For each kN put

Rk={j|kCj},

which is the set of jobs for which student k is qualified (in the sense that they have not already done that job). We will use Proposition 12.4: if there is a constant d such that |Cj|=d for all j and |Rk|d for all k, then the allocation problem is solvable. We will take d=n-p; we have already seen that |Cj|=d for all j. On the other hand, Rk is just the set of columns where we are allowed to put k in the new row, or in other words, the set of columns that do not already contain k. The number of columns that contain k is mL(k), which is p as we explained in Lemma 14.13. Thus, the number of columns that do not contain k is |Rk|=n-p=d as required. Thus, Proposition 12.4 is applicable, so we can solve the allocation problem, and the solution gives us an extra row. ∎

Example 14.14.

Consider the following Latin rectangle, with p=2 and q=n=5:

L=[1245351234].

Recall that Cj is the set of possibilities for position j in the next row. For example, in column 2 we already have a 2 and a 1, so these are not allowed, so C2=N{2,1}={3,4,5}. We can display all the sets Cj as follows:

[1245351234234345135124125]

(We have used abbreviated notation, e.g. 234 for {2,3,4}.) To make the new row, we must choose one element from the possibilities in each column, making sure that we never choose the same element twice. Corollary 55 tells us that this is possible, but does not tell us exactly how to do it. However, in this case it is not difficult: in each column we can just take the first choice that has not already been used. This gives 2,3,1,4,5 as the new row. We can write in this new row and display the possibilities for row 4 as follows:

[1245351234231453445351212]

Again, in each column we can take the first choice that has not already been used. This gives row 4 and leaves only one possibility for row 5. We end up with the following Latin square:

[1245351234231453451245321]
Corollary 14.15.

Let L be a Latin rectangle with q<p=n (so L has the maximum possible height, but not the maximum possible width). Then L can be extended by adding extra columns to make an n×n Latin square.

Proof.

Note that the transpose LT is a Latin square of maximum possible width, so we can use Theorem 14.8 to extend it to a Latin square, then take the transpose again at the end. This just amounts to doing the same steps as before, but with the roles of rows and columns exchanged. ∎

Now consider a Latin rectangle where both p and q are strictly less than n, so neither Theorem 14.8 nor Corollary 14.15 is applicable. Can we still extend it to give an n×n Latin square? It is not hard to find examples where this is not possible.

Example 14.16.

Take P=Q={1,2} and N={1,2,3} and L=[2332]. We could try to extend this to a 3×3 Latin square as follows:

[23a32bcde].

To avoid a clash in row 1, we must take a=1. To avoid a clash in row 2, we must also take b=1. However, this creates an unavoidable clash in column 3. Thus, it is impossible to extend L.

Example 14.17.

Take P=Q={1,2,3,4} and N={1,,6} and

L=[6123563113623214].

It turns out that it is not possible to extend this to a 6×6 Latin square. It is a good exercise to prove this directly. However, we will deduce it from a general theorem instead. We can list the multiplicity and excess of the elements of N as follows:

k123456mL(k)434113eL(k)212-1-11

It turns out that the key point is that some excesses are negative.

Video (Definition 14.18 to Theorem 14.22)

Definition 14.18.

Let L be a Latin rectangle. We say that an element kN is plausible if eL(k)0. More precisely, we say that k is barely plausible if eL(k)=0, and very plausible if eL(k)>0.

Proposition 14.19.

If L can be extended to an n×n Latin square, then every element kN is plausible for L.

Proof.

Choose a Latin square extending L. This will have the form

[LLL′′L′′′],

where L, L′′ and L′′′ have shape p×(n-q), (n-p)×q and (n-p)×(n-q). Let L* be the top part, consisting of L and L. This is a p×n Latin rectangle, so Lemma 14.13 tells us that

mL(k)+mL(k)=mL*(k)=p,

so mL(k)=p-mL(k). On the other hand, L has n-q columns, and there is at most one occurrence of k per column, so mL(k)n-q, so p-mL(k)p+q-n. Putting this together, we get mL(k)p+q-n and so eL(k)=mL(k)+n-p-q0. ∎

In Example 14.17, we see that 4 and 5 have negative excess so they are not plausible, so there cannot be any extension to a 6×6 Latin square. We now discuss another example.

Example 14.20.

Consider the following Latin rectangle with p=4 and q=5 and n=7:

L=[56132652471435647561].

The multiplicities and excesses are as follows:

k1234567mL(k)3223442eL(k)1001220

We have eL(k)0 so all elements are plausible, so we might guess that L can be extended to a 7×7 Latin square. However, Proposition 14.19 does not give us any guarantees about this. If we had found that eL(k)<0 for some k, then Proposition 14.19 would tell us that is definitely no extension. However, when eL(k)0 for all k we can only say (for the moment) that the question remains open. To go beyond this we need another lemma and another theorem.

Lemma 14.21.

Let L be a p×q Latin rectangle, where 0<p<n and 0<qn, and suppose that every kN is plausible for L. Then we can add an extra row to obtain a (p+1)×q Latin rectangle L such that every kN is still plausible for L.

Proof.

We will again interpret this in terms of Remark 14.9. Here we have chosen students to do jobs 1 to q on days 1 to p, but we have not yet decided anything about jobs q+1 to n (perhaps the relevant managers are still on holiday). Our immediate task is to allocate students to jobs 1 to q on day p+1.

We again put d=n-p>0. We again have a job for each jQ, with candidates Cj=N{L1j,,Lpj}, so |Cj|=n-p=d. We again put Rk={j|kCj}, which corresponds to the set of columns not containing k, or the set of jobs that student k can still do. Remark 14.12 tells us that the number of columns that do contain k is mL(k), so the number of columns that do not contain k is |Rk|=q-mL(k). The plausibility condition says that mL(k)+n-p-q0, which translates to q-mL(k)n-p=d, so we see that |Rk|d. In fact, we have |Rk|=d iff eL(k)=0 iff k is barely plausible. The students with |Rk|=d will be called “talented” (although in this model, the fact that they can still do many jobs is not really related to talent). By Corollary 12.5, we can solve the job allocation problem in such a way that every talented student gets assigned a job for day p+1. By adding this as a new row, we get a new Latin rectangle L of size (p+1)×q. Now note that

mL(k)={mL(k)+1 if k is in the new row mL(k) otherwise 

so

eL(k)=mL(k)+n-p-q-1={eL(k) if k is in the new row eL(k)-1 otherwise ,

so in particular eL(k)eL(k)-1 in all cases. Thus, if eL(k)>0 then eL(k)0. On the other hand, if eL(k)=0 then k is barely plausible for L, so student k is “talented”, so k appears in the new row by construction, so eL(k)=eL(k)=0. Thus, in all cases we have eL(k)0. ∎

Theorem 14.22.

Let L be a p×q Latin rectangle, and suppose that every kN is plausible for L. Then L can be extended to an n×n Latin square.

Proof.

We can apply the lemma repeatedly until we get a n×q Latin rectangle, then we can apply Corollary 14.15 to get an n×n Latin rectangle. ∎

Example 14.23.

We now show how to extend the the rectangle from Example 14.20. The process is controlled by the following two tables.

[561324 71 465247 134 314356 272 7475612 33 2 23712 346 7 1273 45 566 6 371 2 462 73 51 65 1 7 1 6 2 3 457 5]   k1234567eL(k)1001220eL(k)1001110eL′′(k)0001100

In the left hand table, the top left block is the original matrix L. In the right hand table, the second row shows the excesses of 1,,7 in L; in particular, the numbers 2, 3 and 7 have eL(k)=0 so they are barely plausible. We want to add a new row, making sure that we include the barely plausible numbers 2, 3 and 7. The possibilities for columns 1,,6 are 237, 123, 467, 127 and 345, as shown in row 5 on the left. From these sets we choose 2, 3, 7, 1 and 4, as indicated by the bold entries in row 5. This gives a 5×5 Latin rectangle which we call L. For the next step, we need to know the excesses for L, which we denote by eL(k). As we saw in the proof of Lemma 14.21, we have eL(k)=eL(k) if k appears in the new row, and eL(k)=eL(k)-1 if k does not appear in the new row. The resulting values are shown in row 3 of the right hand table. In particular, 2,3 and 7 are barely plausible for L. To get the potential entries for row 6, we simply take the sets of potential entries from row 5 and remove the bold ones, leaving 37, 12, 46, 27 and 35. We must choose five distinct numbers, one from each of these sets, in such a way that the barely plausible numbers 2,3 and 7 are included. We choose 3,2,4,7,5, as indicated by the bold entries in row 6. This gives a 6×5 Latin rectangle which we call L′′. The excesses for E′′ are again shown in the right hand table. However, we do not really need them, because there is now only one possible way to fill in row 7, namely (7,1,6,2,3). This gives a 7×5 Latin rectangle. As this has the maximum possible height, we are back in the context of Corollary 14.15, and we do not need to keep track of excesses any more. To the right of the vertical bar, we have written the possible entries for column 7. As our first step (indicated by the superscript 1) we decide to try choosing 7 for the entry in row 1. For the second step (indicated by the superscript 2) we consider row 3. The possible choices there are 2 and 7, but we already used 7 for row 1, so we must use 2 for row 3. For the third step, we consider row 4. The possible choices there are 2 and 3, but we already used 2 for row 3, so we must use 3 for row 4. Continuing in the same way, we must use 1 in row 2, then 6 in row 6, then 5 in row 5, then 4 in row 7. This gives (7,1,2,3,5,6,4) as column 6, and leaves (4,3,7,2,6,1,5) as the only possibility for column 7. We end up with the following Latin square:

[5613274652471314356274756132237145632475617162345]

We now discuss some facts about the number of possible n×n Latin squares.

Definition 14.24.

We let n denote the set of all Latin squares L with P=Q=N={1,,n}. We will find |n| for n4. We say that a Latin square Ln is reduced if the first row is (1,2,,n) and the first column is also (1,2,,n). We write n for the set of reduced Latin squares.

Example 14.25.

For the degenerate case n=1 the only possible Latin square is L=[1], so 1=1 and |1|=|1|=1.

Example 14.26.

For n=2 we have 2={[1221],[2112]}. The first of these lies in 2 but the second does not. We therefore have |2|=1 and |2|=2.

Example 14.27.

For n=3 there are 12 Latin squares, as follows:

[123231312][123312231][132321213][132213321][213132321][213321132][231312123][231123312][312123231][312231123][321213132][321132213]

Of these only the first lies in 3. Thus, we have |3|=1 and |3|=24.

Proposition 14.28.

For any n we have |n|=n!(n-1)!|n|.

Proof.
  • (a)

    We can permute the columns of a Latin square and it will still be a Latin square.

  • (b)

    We can also permute the rows of a Latin square and it will still be a Latin square.

  • (c)

    There is a unique way to permute the columns so that the first row becomes (1,,n).

  • (d)

    After we have done this, the top left entry will be 1, and the first entries in columns 2,,n will therefore be 2,,n in some order. Thus, there is a unique way to permute rows 2,,n so that the first column becomes 1,,n. We now have a Latin square in n.

By thinking about these steps in the reverse order, we obtain the following fact. We can obtain any Latin square in n by starting with a square in n, permuting rows 2,,n in any of (n-1)! possible ways, then permuting the columns in any of n! possible ways. The claim is clear from this. ∎

Proposition 14.29.

We have |4|=4 and so |4|=4!×3!×4=576.

Proof.

We claim that 4 consists of the following 4 squares. The superscripts are just there to help us follow the proof.

L1=[1234230*421134416254132738]L2=[1234240*113231346254342718]L3=[1234210*413234315*264342718]L4=[1234210*413234325*164341728]

The numbers in the superscripts indicate the order in which we should think about the entries; the stars indicate places where we have a choice about what to do.

The first row and column have to be (1,2,3,4). Thus, the first place where we have any choice is the (2,2) position, which we have marked with the superscript 0*. We already have a 2 in the corresponding row (and also in the corresponding column), so we cannot put a 2 in this position; we must have a 3, a 4 or a 1. For square L1 we choose to put a 3 in the (2,2) position. It turns out that we then have no more choices. To see this, consider the superscript 1, which appears in position (2,4) in L1. There we have already placed a 2 and a 3 in the same row and a 4 in the same column, so we have to put a 1 in that slot. Now consider the superscript 2, which appears in position (2,3) in L1. We have already placed 1, 2 and 3 in the same row, so we are forced to put 4 in this slot. Now continue with the positions with superscripts 3,4,,8; we again see that there is never any choice, and we have to fill in the entries as in L1. Thus, L1 is the only possible Latin square in 4 that has a 3 in position (2,2). Similarly, L2 is the only Latin square in 4 that has a 4 in position (2,2). The only other possibility is to put a 1 in position (2,2), as in L3. Just as in the case of L1 and L2, we find that there is no choice about what to put in the positions with superscripts 1,,4. However, when we get to the superscript 5* in position (3,3), we find that we do have a choice: we can either put in a 1 or a 2. If we put in a 1 then we are forced to fill in the remaining 3 slots as in L3, but if we put in a 2 then we are forced to fill in the remaining 3 slots as in L4. We thus have 4={L1,L2,L3,L4} as claimed.

Remark 14.30.

The numbers |n| grow very quickly as n increases:

n|n|1121314455669,408716,942,0808535,281,401,856

We will not prove any of this.

We now start to discuss the theory of orthogonal Latin squares. We will give an example before the definition.

Example 14.31.

Consider the following matrices:

L=[012120201]  M=[021102210]  L*M=[001221112002220110]

Both L and M are Latin squares. The matrix L*M is formed by merging L and M in an obvious way: in symbols, the entry (L*M)ij is the ordered pair (Lij,Mij). There are 9 possible pairs uv with u,v{0,1,2}, as follows:

00, 01, 02,10, 11, 12,20, 21, 22.

It is not hard to check that each of these pair occurs precisely once in L*M.

Definition 14.32.

Let L and M be two n×n Latin squares, with the same sets P, Q and N. Let L*M be the matrix with entries (L*M)ij=(Lij,Mij)N2 for each iP and jQ. We say that L and M are orthogonal if each of the n2 elements of N2 occurs precisely once in L*M. Equivalently, L and M are orthogonal if the entries in L*M are all different.

Example 14.33.

Consider the following matrices:

L=[123312231]  M=[1abcdefg2]  L*M=[112a3b3c1d2e2f3g12]

We will try to find a,,g such that M is a Latin square and is orthogonal to L.

  • L*M must contain 13 somewhere, and this can only happen if d=3 so that 13 appears as the middle entry.

  • In M, entry b lies in the same row as 1 and in the same column as 2, so it must be different from 1 and 2, so it must be equal to 3. By the same logic we also have f=3.

  • Now M is as shown on the left below. To make this a Latin square, each row must contain 1,2 and 3, and each column must contain 1,2 and 3. The only way to achieve this is to take a=c=2 and e=g=1, giving the matrix shown on the right below.

    M=[1a3c3e3g2]=[123231312]
  • We now have

    L*M=[112233321321233112].

    Inspection shows that each of the 9 possible pairs 11, 12, 13,21, 22, 23,31, 32, 33 appears precisely once in L*M, so we have succeeded in finding a Latin square that is orthogonal to L.

We now consider a different problem. Given n>0, can we find a long list of n×n Latin squares L1,,Lr such that Lu and Lv are orthogonal when uv? Our first result gives an upper bound on the possible length of such a list.

Theorem 14.34.

Suppose we have a list L1,,Lr of mutually orthogonal Latin squares of size n. Then rn-1.

Proof.

Look at the first two rows of Lu:

In position (2,1) (at the beginning of the second row), we have some number x{1,,n}. Because Lu is a Latin square, every number must appear in every row. In particular, x must also appear in row 1. It cannot appear in position (1,1), because we would then have two x’s in the first column. So x must also appear at position (1,mu) for some mu{2,,n}. We have now defined numbers m1,,mr; we claim that they are all different. Indeed, suppose that vu, so the first two rows of Lv have the form

If mu and mv were the same, then in Lu*Lv we would have a pattern like this:

so xy would appear twice in Lu*Lv. However, Lu and Lv are assumed to be orthogonal, so each pair occurs precisely once in Lu*Lv, so xy cannot appear twice, so mv must be different from mu.

We now know that the numbers m1,,mr are all different and all lie in {2,,n}. This is clearly only possible if rn-1. ∎

We now see that the maximum possible length of a list of mutually orthogonal n×n Latin squares is at most n-1. Can we achieve this upper bound? We can give a key example using modular arithmetic. We first need to recall a basic fact.

Proposition 14.35.

Let p be a prime. Then every nonzero element of the ring /p has a multiplicative inverse. Thus, /p is a field, and the set (/p){0} is a group under multiplication.

Proof.

Any nonzero element of /p is represented by an integer a with 0<a<p. Let d be the gcd of a and p, so d=au+pv for some integers u and v (by the euclidean algorithm). Now d must divide p, and p is prime, so d=1 or d=p. However, d must also divide a, and p does not divide a because 0<a<p, so we must have d=1. This means that au+pv=1, so au=1(modp), so u is an inverse for a in /p. A field is just a nontrivial commutative ring in which every nonzero element is invertible, so we see that /p is a field. Now let b be another nonzero element, and let v be inverse to b. We then have (ab)(uv)=(au)(bv)=1. This would be impossible if ab were zero mod p, so we must have ab0. This means that the set (/p){0} is closed under multiplication, and it is now easy to see that it is a group. ∎

Remark 14.36.

The proposition implies that if a,b/p with b0 then the element a/b=a.b-1 is well-defined, and that division has all the usual algebraic properties, provided that we are careful not to divide by anything that could be zero.

Proposition 14.37.

Let p be a prime. For 0<u<p define Liju=i+uj(modp). Then Lu is a Latin square (with P=Q=N=/p). Moreover, Lu and Lv are orthogonal if uv (so we have a list of p-1 mutually orthogonal Latin squares of size p).

Proof.

If Liju=Liju we have i+uj=i+uj so i=i. Similarly, if Liju=Liju then i+uj=i+uj in /p. We can rearrange to get u.(j-j)=0 but u is invertible in /p so we can multiply by u-1 to get j-j=0 and so j=j (in /p). This shows that Lu is a Latin square.

Now consider Lu*Lv, where 0<u,v<p with uv, so (Lu*Lv)ij=(i+uj,i+vj). We want to show that every pair (x,y)(/p)2 appears precisely once in this table, or in other words that there is a unique pair (i,j) with(i+uj,i+vj)=(x,y), or that the simultaneous equations i+uj=x and i+vj=y have a unique solution. These equations can be solved in the standard way to give i=(vx-uy)/(v-u) and j=(x-y)/(u-v) as required. ∎

Remark 14.38.

Now consider a number n that is a prime power, say n=pv for some prime number p and some v>1. In this case the ring /n is not a field, but there is a more complicated way to define a field F with |F|=n. We will not discuss the construction here, but it can be found in most books on field theory. Now suppose that uF with u0 (so there are n-1 possible choices for u). We can again define a Latin square Lu with P=Q=N=F by Liju=i+uj, and we again find that Lu and Lv are orthogonal when uv. Thus, we have a list of n-1 mutually orthogonal Latin squares of size n.

The first number that is not a prime or prime power is 6. This case is already hard.

Theorem 14.39.

There are not even two mutually orthogonal Latin squares of size 6.

Proof.

This was conjectured by Euler in the 18th century, and proved by Tarry in 1900. A more digestible proof was given by Stinson in 1982. We will not give any details here. ∎