Is it possible to swap only two elements on a 3x3 grid by swapping rows and columns?

1.1k Views Asked by At

Take a 3x3 grid with a different number in each spot, like one cell of a Sudoku puzzle:

\begin{array}{|c|c|c|} \hline 1&2&3\\ \hline 4&5&6\\ \hline 7&8&9\\ \hline \end{array}

Suppose you can rearrange the number by swapping two rows or two columns.

$$\left.\begin{array}{|c|c|c|}\hline 1&2&3\\ \hline 4&5&6\\ \hline7 & 8 & 9\\ \hline \end{array} \longrightarrow \begin{array}{|c|c|c|}\hline 1&2&3\\ \hline7&8&9\\ \hline4&5&6\\ \hline \end{array} \longrightarrow \begin{array}{|c|c|c|}\hline2&1&3\\ \hline8&7&9\\ \hline5&4&6\\ \hline \end{array} \right.$$

Is it possible to manipulate the grid such that an arbitrary pair of numbers is swapped (with the rest returned to their original positions)?

e.g. $$\left.\begin{array}{|c|c|c|}\hline 1&2&6\\ \hline 4&5&3\\ \hline7&8&9\\ \hline \end{array} \qquad\text{or}\qquad \begin{array}{|c|c|c|}\hline 8&2&3\\ \hline4&5&6\\ \hline7&1&9\\ \hline \end{array} \qquad\text{or}\qquad \begin{array}{|c|c|c|}\hline1&2&3\\ \hline4&7&6\\ \hline5&8&9\\ \hline \end{array} \right.$$

2

There are 2 best solutions below

3
On

Four numbers that are in the "corners" of a rectangle (like 1379 or 1278) will remain in the corners of a rectangle no matter how many or which swaps happen. So you cannot swap just two elements while keeping the rest fixed.

0
On

Let's apply this answer from the post that I have linked in the comments.

Note that applying a row-swap followed by a column-swap has the same effect as applying that column-swap followed by the row-swap (however, column-swaps cannot generally be switched with each other, and neither can row-swaps).

With that in mind, we can suppose that all row-swaps are performed before the column-swaps. That is, we can argue that every possible rearrangement can be obtained by some series of row-swaps followed by some series of column-swaps. The net effect of the row-swaps is some permutation of the rows, and the net effect of the column-swaps is some permutation of the columns.

There are $3 \times 2 \times 1 = 6$ ways that we can permute three rows around, and likewise $6$ ways that we can permute three columns around. So all together, the total number number of rearrangements possible using row-swaps and column swaps is $6 \times 6 = 36$.

On the other hand, if we were allowed to freely arrange entries within the box, then there is a total of $$ 9 \times 8 \times \cdots \times 2 \times 1 = 362880. $$ So, if we take a random rearrangement (for instance, switching two entries while keeping the others fixed), then the probability that the rearrangement is possible using only row and column swaps is $\frac{36}{362880} \approx 0.00992 \%$.


We can also more rigorously prove that it's impossible to switch every pair of two entries using the above calculations: the total number of switches that we would like to attain is "9 choose 2", i.e. $\frac {9 \times 8}{2} = 36$. However, there are only $36$ permutations attainable in total, and it is clear that not all of these permutations are switches of this kind.


This answer from the same post also leads to a nice argument.

Claim: If $i$ and $j$ are in the same (row/column), then they must also be in the same (row/column) after a series of row and column switches.

I will leave it to you to convince yourself that this holds. With that established, we can see what goes wrong with swaps. If we swap two elements in the same row, $$ \pmatrix{1&2&3\\4&5&6\\7&8&9} \to \pmatrix{\color{red}3&2&\color{red}1\\4&5&6\\7&8&9} $$ then we contradict the claim. For example, $1$ was in the same column as $4$, but is not after the switch.

A similar argument can be made if the switched elements share a row.

If we swap elements that have neither a common row nor column, then the situation is slightly different but a similar argument can be made. For instance, $$ \pmatrix{1&2&3\\4&5&6\\7&8&9} \to \pmatrix{\color{red}8&2&3\\4&5&6\\7&\color{red}1&9}. $$ $1$ was in the same row as $3$, but this is no longer the case after the switch.