\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{latexsym}
\usepackage[pdftex]{graphicx}
\usepackage{url}
\usepackage{mathrsfs}
\usepackage{multicol}
\usepackage{mathdots}
 \usepackage{colortbl}
\usepackage{color}
 \definecolor{teal}{rgb}{0.5,0.8,0.8}
\usepackage{hhline}
\usepackage{hyperref}
\hypersetup{colorlinks=true}
\setlength{\oddsidemargin}{.25in}
\setlength{\evensidemargin}{.25in}
\setlength{\textwidth}{6in}
\setlength{\topmargin}{-0.4in}
\setlength{\textheight}{8.5in}


\def\zero{\multicolumn{1}{>{\columncolor{white}}c}{0}}
\def\colCell#1#2{\multicolumn{1}{>{\columncolor{#1}}c}{#2}}

\input{preamble.tex}

\begin{document}

\newcommand{\wqueen}{\includegraphics[width=.15in]{MC2012_LatinSquares_miniqueen.pdf}}
\newcommand{\rook}{\includegraphics[width=.15in]{rook.pdf}}

\lecture{2: The $n$-Queens Problem}{Week 2}

\section{The $n$-Queens Problem}

If you haven't played chess, here's a quick summary of the things you will need to understand for this lecture:
\begin{itemize}
\item \textbf{A $n \times n$ chessboard} is simply a $n \times n$ array of cells.
\item A \textbf{queen} is a piece used in the game of chess; we represent it in these notes with the symbol \wqueen, which is roughly what the top of the piece looks like.  In the game of chess, a queen can move to any cell within the same row, any cell within the same column, or any cell along the two diagonals from whatever cell it starts from.  We illustrate this here:
\begin{center}
\includegraphics[width=2in]{MC2012_LatinSquares_queens.pdf}
\end{center}
\end{itemize}
The $n$-queens problem is the following puzzle:
\begin{quest}
Take a $n \times n$ chessboard.  Can you place $n$ distinct queens on this chessboard, so that no queen can capture any other (i.e.\ so that there is no way to move any one queen into a cell currently occupied by another queen?)
\end{quest}

A good strategy, when given any puzzle, is to just try it for small values!  For $n=1$, this is pretty easy: behold!  
\begin{align*}
\begin{array}{|c|}
\hline \wqueen \\\hline
\end{array}
\end{align*}
For $n=2$, it's also pretty easy to see that this is impossible.  Any one queen in a $2 \times 2$ board can move to any other square; therefore, it is impossible to place a second queen into our grid.  
\begin{align*}
\begin{array}{|c|c|}
\hline \wqueen & \times \\\hline
\times & \times \\\hline
\end{array}
\end{align*}

For $n=3$, it's also not too hard to see that this is impossible.  When you place a \wqueen on a $3 \times 3$ board, it either goes on in the center (in which case it can move to any other square) or one of the side/corner squares, in which case there are precisely two squares to which it cannot move.  Any second queen placed in either of those spaces can move to the other space; therefore, we cannot place a third queen.  
\begin{align*}
\begin{array}{|c|c|c|}
\hline \wqueen & \times & \times \\\hline
\times &  \times  & \\\hline
\times &  & \times \\\hline
\end{array} \to 
\begin{array}{|c|c|c|}
\hline \wqueen & \times & \times \\\hline
\times & \times  & \times \\\hline
\times & \wqueen & \times \\\hline
\end{array}, \quad
\begin{array}{|c|c|c|}
\hline \times & \times &  \\\hline
\wqueen & \times  & \times \\\hline
\times & \times &  \\\hline
\end{array} \to \begin{array}{|c|c|c|}
\hline \times & \times & \times \\\hline
\wqueen & \times  & \times \\\hline
\times & \times &  \wqueen\\\hline
\end{array}, \quad
\begin{array}{|c|c|c|}
\hline \times & \times & \times \\\hline
\times & \wqueen  & \times \\\hline
\times & \times & \times \\\hline
\end{array}.
\end{align*}

For $n=4$, however, we can do it!  Consider the following arrangement:

\begin{align*}
\begin{array}{|c|c|c|c|}
\hline \times & \times & \wqueen & \times \\\hline
\wqueen & \times  & \times & \times \\\hline
\times & \times & \times  & \wqueen\\\hline
\times & \wqueen & \times &\times \\\hline
\end{array}
\end{align*}

The above pattern you can find by just exhaustively searching through possible ways to place queens on a $4 \times 4$ chessboard.  In general, however, we want a systematic approach; i.e.\ a pattern that we can follow to always solve the $n$-queens problem, or tell us that no such solution exists.

To do this, we turn to a second object in mathematics: the concept of a \textbf{Latin square}!

\section{Latin Squares}


\begin{definition}
A \textbf{Latin square} of order $n$ is a $n \times n$ array filled with $n$ distinct symbols (by convention $\{1, \ldots n\}$), such that no symbol is repeated twice in any row or column. 
\end{definition}
\begin{exmp}
Here are all of the Latin squares of order 2:
\begin{align*}
\begin{array}{|c|c|}\hline
1 & 2 \\\hline
2 & 1 \\\hline
\end{array}~, \quad
\begin{array}{|c|c|}\hline
2 & 1 \\\hline
1 & 2 \\\hline
\end{array}~.
\end{align*}
\end{exmp}

A quick observation we can make is the following:

\begin{prop}
Latin squares exist for all $n$.
\end{prop}
\begin{proof}
Behold!
\begin{align*}
\begin{array}{|c|c|c|c|c|}\hline
 1 & 2 & \ldots & n-1 & n\\\hline
 2 & 3 & \ldots & n & 1\\\hline
3 & 4& \ldots & 1 & 2 \\\hline
 \vdots & \vdots & \ddots & \vdots & \vdots\\\hline
 n & 1 & \ldots & n-2 & n-1\\\hline
\end{array}
\end{align*}
\end{proof}

Given this observation, a natural question to ask might be ``How many Latin squares exist of a given order $n$?''  And indeed, this is an excellent question!  So excellent, in fact, that it turns out that we have no idea what the answer to it is; indeed, we only know the true number of Latin squares of any given order up to 11!
\begin{align*}
\begin{array}{|c|c|c|}
\hline
\textrm{n} & \textrm{reduced Latin squares of size n}\footnotemark & \textrm{all Latin squares of size n}\\
\hline1 & 1 & 1\\
\hline2 & 1 & 2 \\
\hline3 & 1 & 12 \\
\hline4 & 4 & 576 \\
\hline5 & 56 & 161280\\
\hline6 & 9408 & 812851200\\
\hline7 & 16942080&61479419904000\\
\hline8& 535281401856&108776032459082956800\\
\hline9&377597570964258816&5524751496156892842531225600\\
\hline10&7580721483160132811489280 & 9982437658213039871725064756920320000\\
\hline11&5363937773277371298119673540771840&776966836171770144107444346734230682311065600000 \\
\hline 12 & ? & ?\\
\hline
\end{array}
\end{align*}

\footnotetext{(A \textbf{reduced} Latin square of size $n$ is a Latin square where the first column and row are both $(1,2,3\ldots n)$.)} 
Asymptotically, the best we know (and you could show, given a lot of linear algebra tools) that 
\begin{align*}
L(n) \sim \left( \frac{n}{e^2} \right)^{n^2}.
\end{align*}


From here, you might wonder what the connection between Latin squares and the $n$-queens problem is!  This is perhaps best illustrated by considering a related (and much easier) problem, the \textbf{$n$-rooks} problem:
\begin{quest}
Take a $n \times n$ chessboard.  A \textbf{rook} in the game of chess is a piece, represented by the symbol \rook; from whatever cell it is in, it can move to any cell within the same row or column a as itself.

Can you place $n$ distinct rooks on this chessboard, so that no rook can capture any other?
\end{quest}

The answer to this is yes; moreover, we can use Latin squares to create solutions to this problem!  Simply take any $n \times n$ Latin square, choose a symbol $k$, and place rooks on each instance of that symbol $k$ in our square!  This places exactly one rook in each row and column, giving us $n$ rooks in total; moreover, because there are no repeated symbols in our Latin square in any row or column, this means that none of our rooks can capture each other!  We give a $5 \times 5$ example here, where we place rooks on all instances of the symbol 3:
\begin{align*}
\begin{array}{|c|c|c|c|c|}\hline
1  &2 & 3 & 4 & 5 \\\hline
 2&3 &4   &5 &1  \\\hline
3& 4 & 5 &  1 & 2 \\\hline
4 & 5 & 1 &2 & 3\\\hline
5& 1 &2  &3 & 4\\\hline
\end{array} ~~\mapsto ~~
\begin{array}{|c|c|c|c|c|}
\hline  & & \rook &  &  \\\hline
 & \rook&   &  &  \\\hline
\rook&  &  &   &  \\\hline
 &  &  & & \rook\\\hline
&  &  & \rook& \\\hline
\end{array}
\end{align*}

So: Latin squares already capture some of the information involved in solving the $n$-queens puzzle; we're just missing the diagonals part!  To fix this, consider the following definition:
\begin{defn}
A \textbf{broken right diagonal}, or \textbf{wraparound right diagonal}, in a Latin square $L$ is the set of $n$ cells acquired by starting from one of the cells in our top row and repeatedly taking the cell that's one below and one to the right of this cell, wrapping around our square if we hit the last column, until we get to the last row. 
\begin{align*}
\begin{array}{|c|c|c|c|c|}
\hline  & & \times &  &  \\\hline
 & &   & \times &  \\\hline
&  &  &   & \times \\\hline
\times &  &  & & \\\hline
& \times &  & & \\\hline
\end{array}
\end{align*}
A \textbf{broken left diagonal} is the same kind of object, except wrapping around to the left instead of the right.

Given these definitions, a Latin square $L$ is called \textbf{pandiagonal} (alternately, \textbf{diabolic}, or \textbf{perfect}, depending on the author) if every broken diagonal contains no repeated symbols.  
\end{defn}

Pandiagonal Latin squares should feel like a much stronger version of the diagonal Latin squares we studied earlier; as opposed to just requiring that the main diagonal and antidiagonal contain no repeats, we now require \textbf{every} diagonal, including the broken ones, to not have any repeats.  

The reason we care about these is the following observation:

\begin{prop}
Suppose that $L$ is a $n \times n$ pandiagonal Latin square.  Choose any symbol $s$ occurring in $L$.  Take a $n \times n$ chessboard, and place a queen on every cell containing $s$.  Then this is a solution to the $n$-queens problem.
\end{prop}
\begin{proof}
Because $L$ is pandiagonal, there are no repeated symbols $s$ in any row, column, or broken diagonal: therefore, in particular, if we place a queen at every cell containing a symbol $s$, none of these queens can move to a cell containing another queen.
\end{proof}

So: we care about pandiagonal Latin squares!  The next natural question we could ask is whether these even exist.  To answer this, we need one last concept; \textbf{modular arithmetic!}

\section{Modular Arithmetic}


We start with an example you've seen and worked with since you've been very tiny, even though you likely didn't think of it this way: time!

\begin{defn}
The set $\mathbb{Z}/12\mathbb{Z}$, of ``clock numbers,'' is defined along with an addition operation $+$ and multiplication operation $\cdot$ as follows:
\begin{itemize}
\item Our set is the numbers $\{0,1,2,3,4,5,6,7,8,9,10,11\}$.
\item Our addition operation is the operation ``addition mod 12,'' or ``clock arithmetic,'' defined as follows: we say that $a + b \equiv c \mod 12$ if the two integers $a+b$ and $c$ differ by a multiple of 12.  Another way of thinking of this is as follows: take a clock, and replace the 12 with a 0.  To find out what the quantity $a+b$ is, take your clock, set the hour hand so that it points at $a$, and then advance the clock $b$ hours; the result is what we call $a+b$.

For example, $3+5 \equiv 8 \mod 12$, and $11 + 3 \equiv 2 \mod 12$.  This operation tells us how to add things in our set.
\item Similarly, our multiplication operation is the operation ``multiplication mod 12,'' written $a \cdot b \equiv c \mod 12$, and holds whenever $a+b$ and $c$ differ by a multiple of 12.  Again, given any pair of numbers $a,b$, to find the result of this ``clock multiplication,'' look at the integer $a\cdot b$, and add or take away copies of 12 until you get a number between 0 and 11. 

For example, $2 \cdot 3 \equiv 6 \mod 12$, $4 \cdot 4 \equiv 4 \mod 12$, and $6 \cdot 4 \equiv 0 \mod 12$.
\end{itemize}
We often will denote this object as $\langle \mathbb{Z} / 12\mathbb{Z}, +, \cdot\rangle$, instead of as $\mathcal{C}$.
\end{defn}

Simple enough, right?  We can generalize this as follows:

\begin{defn}
The object $\langle \mathbb{Z}/n\mathbb{Z}, +, \cdot\rangle$,i.e.s defined as follows:
\begin{itemize}
\item Your set is the numbers $\{0,1,2,\ldots n-1\}$.
\item Your addition operation is the operation ``addition mod n,'' defined as follows: we say that $a + b \equiv c \mod n$ if the two integers $a+b$ and $c$ differ by a multiple of $n$.

For example, suppose that $n = 3.$  Then $1+1 \equiv 2 \mod 3$, and $2+ 2 \equiv 1 \mod 3$.  
\item Similarly, our multiplication operation is the operation ``multiplication mod n,'' written $a \cdot b \equiv c \mod n$, and holds whenever $a+b$ and $c$ differ by a multiple of $n$.

For example, if $n = 7$, then $2 \cdot 3 \equiv 6 \mod 7$, $4 \cdot 4 \equiv 2 \mod 7$, and $6 \cdot 4 \equiv 3 \mod 7$.
\end{itemize}
\end{defn}

\section{Combining It All Together: Pandiagonal Latin Squares}

We use modular arithmetic to create a pandiagonal Latin square as follows:

\begin{construct}
Take any value of $n$, and any two numbers $a,b \in \{0, \ldots n-1\}$.  Consider the following square populated with the elements $\{0, 1\ldots n-1\}$:
\begin{align*}
L = \begin{array}{|c|c|c|c|c|c|}
\hline
0 & a & 2a & 3a & \ldots & (n-1)a \\\hline
b & b+a & b + 2a & b+3a & \ldots & b + (n-1)a \\\hline
2b & 2b+a & 2(b+a) & 2b+3a & \ldots & 2b+(n-1)a \\\hline
3b & 3b+a & 3b+2a & 3(b+a) & \ldots & 3b + (n-1)a \\\hline
\vdots & \vdots & \vdots & \vdots  & \ddots & \vdots \\\hline
(n-1)b & (n-1)b + a & (n-1)b + 2a & (n-1)b + 3a & \ldots & (n-1)(b+a)\\\hline
\end{array} \mod n.
\end{align*}
In other words, $L$'s $(i,j)$-th cell contains the symbol given by taking the quantity $bi + aj \mod n$.  
\end{construct}

Given this construction, a question we'd like to ask is the following: for what values of $n$ is this a pandiagonal Latin square?

Well: let's start smaller, and just ask that it's a normal Latin square.  In order for this to hold, we need to not have any repeats in any given row: in other words, that no two cells $(i,j), (k,j)$ contain the same symbol.  But this can happen only if
\begin{align*}
\left(bi + aj \equiv bk + aj \mod n \right)\Leftrightarrow \left( bi \equiv bk \mod n \right).
\end{align*}
If $b$ and $n$ have common factors, then this is possible; let $i = 0$ and $k = \frac{n}{\textrm{GCD}(b,n)}$!  However, if $b$ and $n$ are relatively prime, then this can only happen if $i = k$; i.e. if we've picked the same cell!  So we have no repeats in any row if and ony if $b$ and $n$ are relatively prime.

Similarly, if we look at any column, we can see that there are no repeats in any column if and only if $a$ and $n$ are relatively prime.  Therefore, this construction is a Latin square if and only if $a,b$ are both relatively prime to $n$.

What about being a pandiagonal Latin square?   Well: consider any broken right diagonal.  If we're careful with how we write it mod $n$, we can see that it's actually of the form
\begin{align*}
\begin{array}{|c|c|c|c|c|c|c|c|}
\hline
 &  \ldots &&  \colCell{teal}{ka} &  & \ldots & \\\hline
& \ldots & &&  \colCell{teal}{ka + (a+b) }& \ldots & \\\hline
& \ddots & && & \ddots  & \\\hline
& \ldots & & &&   \ldots & \colCell{teal}{ka + (n-k-1)(a+b)} \\\hline
\colCell{teal}{ka + (n-k)(a+b) } &\ldots &&&&& \\\hline
& \ddots & && &   & \\\hline
& \ldots & \colCell{teal}{ka + (n-1)(a+b) }&&&& \\\hline
\end{array} 
\end{align*}
Notice how at each step we go up by $(a+b)$ each time!  Therefore, the entry of our diagonal at any given row $x$ is just $ka + x(a+b)$, and we have a repeat in our diagonal if and only if there are two rows $x,y$ such that
\begin{align*}
ka + x(a+b) \equiv ka + y(a+b) \mod n.
\end{align*}
But this happens if and only if $(x-y)(a+b)$ is a multiple of $n$.  If the two numbers $n$, $a+b$ are relatively prime, then this can happen if and only if $x-y$ is 0; in other words, we don't have any repeats!

Similarly, if you look at the entries in any broken left diagonal, you can see that we increase by $b-a$ as we go down each step:

\begin{align*}
\begin{array}{|c|c|c|c|c|c|c|c|}
\hline
 &  \ldots &   &  \colCell{teal}{ka}& &\ldots & \\\hline
& \ldots &\colCell{teal}{ ka + (b-a)} &  && \ldots & \\\hline
& \iddots & & && \iddots  & \\\hline
\colCell{teal}{ka + (n-k-1)(b-a) }& \ldots & && &   \ldots &\\\hline
 &&&&&& \colCell{teal}{ka + (n-k-1)(b-a)} \\\hline
& \iddots & & &&  \iddots & \\\hline
& \ldots &  &&\colCell{teal}{ka + (n-1)(a+b)}&& \\\hline
\end{array} 
\end{align*}

Therefore, all of the entries in any broken left diagonal are distinct if $a-b$ is relatively prime to $n$, by the exact same kind of argument!

In particular, suppose that we set $a= 2, b = 1$.  Then we have that this construction gives us a pandiagonal Latin square if and only if $n$ is relatively prime to $1, 2, 1+2 = 3, and 1-2 = -1$.  In other words, this works for all values of $n$ that are odd and not multiples of 3!  We run an example here, just to show that it did indeed work:

\begin{align*}
\begin{array}{|c|c|c|c|c|}
\hline
0 &2&4&1&3 \\\hline
1&3&0&2&4\\\hline
2&4&1&3&0\\\hline
3&0&2&4&1\\\hline
4&1&3&0&2\\\hline
\end{array} \longrightarrow
\begin{array}{|c|c|c|c|c|}
\hline
\wqueen &\times &\times&\times&\times \\\hline
\times&\times& \wqueen&\times&\times\\\hline
\times&\times&\times&\times&\wqueen\\\hline
\times&\wqueen&\times&\times&\times\\\hline
\times&\times&\times& \wqueen&\times\\\hline
\end{array}
\end{align*}


Cool!  



\end{document}