TITLE: recurrence involving permutations
QUESTION [8 upvotes]: Let $K_n$ be the number of permutations $\pi$ of $\{1,2,\cdots, n\}$ so that $|i-j|=1$ implies $|\pi(i)-\pi(j)| \leq 2$ for all $i,j$ in $\{1,2,\cdots, n\}$. Show that for $n\ge 2, K_{n+5} - K_{n+4} - K_{n+3} + K_n = 4.$

I know recurrence relations that can be used to derive the answer. However, I don't understand how those recurrences were obtained, so could someone elaborate or provide a proof on that? Below are the recurrences I'm confused about.
Let $A_n$ be the number of permutations $\pi$ of $K_{n+1}$ with $\pi(1) = 1$, where $K_1 := 1$. Then
$K_n = 2(A_0+A_1+\cdots + A_{n-3}+A_{n-1})\,\forall n\ge 2$
and
$A_n = A_{n-1}+A_{n-3} + 1$ for $n\ge 3$.
Also, one has the following recurrences:
Assume $n\ge 3$. Let $U_n$ be the number of permutations counted by $K_n$ that end with $n-1,n$, let $V_n$ be the number ending in $n,n-1$, let $W_n$ be the number starting with $n-1$ and ending in $n-2,n$, let $T_n$ be the number ending in $n-2,n$ but not starting with $n-1$, and let $S_n$ be the number that has $n-1,n$ consecutively in that order but not at the beginning or end of a permutation.

(Why is it true that) every permutation $\pi$ counted by $K_n$ either lies in exactly one of the sets counted by $U_n, V_n, W_n, T_n, S_n$ or is the reverse of such a permutation.

Thus $P_n = 2(U_n + V_n + W_n+ T_n+S_n)$.
Also one has the recurrence relations $U_{n+1} = U_n + W_n + T_n, V_{n+1 } = U_n, W_{n+1} = W_n, T_{n+1} = V_n, S_{n+1} = S_n + V_n$.

I get that $W_n = 1$ for all $n$; $n-1$ must be beside $n-3$, $n-2$ must follow $n-4,$ etc. So a permutation in $W_n$ would have to be of the form $(n-1,n-3,n-5,\cdots, n-4, n-2,n)$. If $n$ is odd, in the middle of the permutation is $2,1$ and if n is even there is $1,2$.


Clearly every permutation counted by $U_n, W_n, V_n, S_n, T_n$ lies in the set of permutations counted by $K_n$. Now suppose $\pi$ is a permutation counted by $K_n$ that does not lie in $U_n, W_n, V_n, S_n, $ or $T_n$ and is not the reverse of a permutation lying in $U_n, W_n, V_n, S_n, $ or $T_n$. $n$ must be paired with $n-1$ or $n-2$ in any permutation counted by $K_n$. We cannot have $\pi(n) = n$ or $\pi(1) = n$ (otherwise $\pi$ is counted by $U_n, W_n$ or $T_n$ or is the reverse of a permutation counted by $U_n, W_n,T_n$). So $\pi(i)=n$ for some $2\leq i\leq n-1$. By the above observation, either $\pi(i-1) = n-1$ or $\pi(i-1) = n-2$. If it's the former then $\pi$ is counted by $S_n$ or is the reverse of a permutation counted by $V_n$ and if it's the latter then it's either counted by $V_n$ or is the reverse of a permutation counted by $S_n$. This covers all possibilities, so we are done the proof of this claim.


So to summarize, this question asks to justify/prove the recurrences for $K_n$ (just the first one if the proof above is right; permutations in the sets of permutations counted by $U_n, V_n, W_n, S_n, T_n$ are in bijection with their reverses), the recurrence for $A_n$ and the recurrences for $U_n, W_n, V_n, S_n, T_n$.


As an example to clarify the sets $U_n, V_n, W_n, T_n, S_n$ for $n=3$, we have $U_n = \{(1,2,3)\}, V_n=\{(1,3,2)\}, W_n=\{(2,1,3)\}, T_n=\emptyset, S_n=\emptyset$.

REPLY [9 votes]: (Apart from answering pings I am no longer active at MSE, but I was specifically asked by the OP to look at this question and have found the time to do so.)
I will use $K_n$ for the set of permutations of $[n]$ defined in the question and $k_n=|K_n|$ for the number of such permutations. Similarly, I will use $U_n$ for the set of permutations of $[n]$ ending in $n-1,n$ and $u_n=|U_n|$ for the number of such permutations, and so on.
You’ve already shown that
$$k_n=2(u_n+v_n+w_n+s_n+t_n)\,.\tag{1}$$
The arguments for the next five recurrences are similar.
Suppose that $\pi\in U_{n+1}$. Then $\pi$ ends in $n,n+1$, and there are three possibilities:

$\pi$ ends in $n-1,n,n+1$; then $\pi\upharpoonright[n]\in U_n$.
$\pi$ ends in $n-2,n,n+1$, and $\pi(1)=n-1$; then $\pi\upharpoonright[n]\in W_n$.
$\pi$ ends in $n-2,n,n+1$, and $\pi(1)\ne n-1$; then $\pi\upharpoonright[n]\in T_n$.

Conversely, appending $n+1$ to any permutation in $U_n\cup W_n\cup T_n$ yields a permutation in $U_{n+1}$, so $$u_{n+1}=u_n+w_n+t_n\,.\tag{2}$$
Suppose that $\pi\in V_{n+1}$; then $\pi$ ends in $n+1,n$. The only numbers that may be adjacent to $n+1$ are $n$ and $n-1$, so $\pi$ must end in $n-1,n+1,n$. Define a permutation $\pi'$ of $[n]$ by setting $\pi'(n)=n$, $\pi'(n-1)=n-1$, and $\pi'\upharpoonright[n-2]=\pi\upharpoonright[n-2]$. (In other words, $\pi'$ is obtained from $\pi$ by deleting $n+1$.) The map $\pi\mapsto\pi'$ is a bijection from $V_{n+1}$ to $U_n$, so $$v_{n+1}=u_n\,.\tag{3}$$
Suppose that $\pi\in W_{n+1}$; then $\pi$ begins with $n$ and ends in $n-1,n+1$. The only numbers that may be adjacent to $n$ are $n-2,n-1$, and $n+1$, so in fact $\pi$ must begin with $n,n-2$, and $\pi\upharpoonright[n]$ is the reversal of a permutation in $W_n$. Conversely, if $\pi\in W_n$, then reversing $\pi$ and appending $n+1$ yields a permutation in $W_{n+1}$ so $$w_{n+1}=w_n\,.\tag{4}$$
Suppose that $\pi\in T_{n+1}$; then $\pi$ ends in $n-1,n+1$ and does not start with $n$. Since $\pi$ does not start with $n$, $n$ has two neighbors in $\pi$, and clearly neither is $n+1$, so $n$ must be between $n-1$ and $n-2$. Thus, $\pi$ must end in $n-2,n,n-1,n+1$, and $\pi\upharpoonright[n]\in V_n$. Now suppose that $\pi\in V_n$, so that $\pi$ ends in $n,n-1$. The other neighbor of $n$ must be $n-2$, so $\pi$ ends in $n-2,n,n-1$, and appending $n+1$ yields a member of $T_{n+1}$. Thus, $$t_{n+1}=v_n\,.\tag{5}$$
Suppose that $\pi\in S_{n+1}$; then there is an index $\ell$ such that $2\le\ell\le n-1$, $\pi(\ell)=n$, and $\pi(\ell+1)=n+1$. Then $\pi(\ell+2)=n-1$, since the only possible neighbors of $n+1$ are $n$ and $n-1$. There are two possibilities:

If $\ell=n-1$, then $\pi$ ends in $n,n+1,n-1$. Define a permutation $\pi'$ of $[n]$ by setting $\pi'(n)=n-1$ and $\pi'\upharpoonright[n-1]=\pi\upharpoonright[n-1]$; then $\pi'\in V_n$.
If $\ell<n-1$, then deleting $n+1$ and reversing the result produces a member of $S_n$.

Clearly this defines a bijection from $S_{n+1}$ to $S_n\cup V_n$, so $$s_{n+1}=s_n+v_n\,.\tag{6}$$
Clearly $w_3=1$, so $(4)$ implies that $w_n=1$ for all $n\ge 3$. From $(3)$ and $(6)$ we see that $$s_{n+1}=s_n+u_{n-1}\,,\tag{7}$$ and from $(3)$ and $(5)$ that $$t_{n+1}=u_{n-1}\,,\tag{8}$$ and hence from $(2)$ that $$u_{n+1}=u_n+u_{n-2}+1\,.\tag{9}$$
Note that the definition of $U_n$ actually makes sense for $n=$ and $n=2$, with $u_1=0$ and $u_2=1$, and it’s easy to check that $(7),(8)$, and $(9)$ then hold for all $n\ge 3$. In fact we can also set $s_2=0$, which is consistent with the definition of $S_n$, and see that $(7)$ holds for $n\ge 2$.

It follows from $(9)$ that $u_{n+5}-u_{n+4}=u_{n+2}+1$ and hence that
$$\begin{align*}
u_{n+5}-u_{n+4}-u_{n+3}+u_n&=u_{n+2}-u_{n+3}+u_n+1\\
&=(u_{n+2}+u_n+1)-u_{n+3}\\
&=u_{n+3}-u_{n+3}\\
&=0
\end{align*}\tag{10}$$
for all $n\ge 1$. Then
$$\begin{align*}
v_{n+5}-v_{n+4}-v_{n+3}+v_n&=u_{n+4}-u_{n+3}-u_{n+2}+u_{n-1}=0\,,\\
w_{n+5}-w_{n+4}-w_{n+3}+w_n&=1-1-1+1=0\,,\text{ and}\\
t_{n+5}-t_{n+4}-t_{n+3}+t_n=&u_{n+3}-u_{n+2}-u_{n+1}+u_{n-2}=0
\end{align*}\tag{11}$$
for all $n\ge 3$. Moreover,
$$\begin{align*}
s_{n+5}-s_{n+4}-s_{n+3}+s_n&=(s_{n+4}+s_{n+3}+s_{n+2}+s_{n-1})+(u_{n+4}+u_{n+3}+u_{n+2}+u_{n-1})\\
&=s_{n+4}+s_{n+3}+s_{n+2}+s_{n-1}
\end{align*}$$
for all $n\ge 2$.
Using $(7)$ and $(9)$ we can calculate directly that $s_3=0$, $s_4=1$, $s_5=2$, $s_6=4$, and $s_7=8$, so that
$$s_7-s_6-s_5+s_2=8-4-2+0=2\,,$$
and hence $$s_{n+5}-s_{n+4}-s_{n+3}+s_n=2\tag{12}$$ for all $n\ge 2$. $(1),(10),(11)$, and $(12)$ then imply that
$$k_{n+5}-k_{n+4}-k_{n+3}+k_n=2(s_{n+4}-s_{n+3}-s_{n+2}+s_{n-1})=4$$
for all $n\ge 3$. The simplest way to verify that $k_{n+5}-k_{n+4}-k_{n+3}+k_n=4$ also holds when $n=2$ is to use the recurrences already proved to calculate $k_2$ through $k_7$, as shown in the following table:
$$\begin{array}{c|cc}
n&u_n&v_n&w_n&s_n&t_n&k_n\\\hline
1&0&&&&&1\\
2&1&&&0&&2\\
3&1&1&1&0&0&6\\
4&2&1&1&1&1&12\\
5&4&2&1&2&1&20\\
6&6&4&1&4&2&34\\
7&9&6&1&8&4&56
\end{array}$$
Thus, $k_7-k_6-k_4+k_2=56-34-20+2=4$, as desired.

This leaves only the two identities involving the sets $A_n$:
$$a_n=a_{n-1}+a_{n-3}+1\tag{13}$$
for $n\ge 3$, and
$$k_n=2\left(a_{n-1}+\sum_{i=0}^{n-3}a_i\right)\tag{14}$$
for $n\ge 2$.
To prove $(13)$, fix $n\ge 3$. Suppose that $\pi\in A_n$; then $\pi(1)=1$, and $\pi(2)$ must be either $2$ or $3$. If $\pi(2)=2$, define a permutation $\pi'$ of $[n-1]$ by setting $\pi'(i)=\pi(i+1)-1$ for each $i\in[n]$. Then it’s straightforward to check that $\pi'\in A_{n-1}$, and that the map $$\{\pi\in A_n:\pi(2)=2\}\to A_{n-1}:\pi\mapsto\pi'$$ is a bijection. This accounts for $a_{n-1}$ elements of $A_n$.
Now suppose that $\pi(2)=3$; there are two cases. If $\pi(3)=2$, so that $\pi$ begins with $1,3,2$, define a permutation $\pi'$ of $[n-3]$ by setting $\pi'(i)=\pi(i+3)-3$ for each $i\in[n-3]$. As in the previous case it’s straightforward to check that $\pi'\in A_{n-3}$, and that the map $$\{\pi\in A_n:\pi(2)=2\}\to A_{n-3}:\pi\mapsto\pi'$$ is a bijection. This accounts for another $a_{n-3}$ elements of $A_n$.
The only possible neighbors of $2$ are $1,3$, and $4$, so if $\pi(3)\ne 2$, then $2$ must have only the single neighbor $4$, and $\pi$ must end in $4,2$. In particular, $\pi$ must be $1,3,4,2$ if $n=3$ and $1,3,5,4,2$ if $n=4$. At this point it should be clear that if $n=2m$, then $\pi$ must be $1,3,\ldots,2m-1,2m,\ldots,4,2$, and if $n=2m+1$, then $\pi$ must be $1,3,\ldots,2m+1,2m,\ldots,4,2$. I’ll leave the proof by induction on $n$ to you; it’s a little tricky to write up but is not actually hard.
This proves $(13)$; that last case accounts for one last element of $A_n$, and the three cases are exhaustive and mutually exclusive. (Note that $(13)$ and $(9)$ are essentially the same recurrence; if you calculate a few values of $a_n$, you’ll find that $a_n=u_{n+2}$ for all $n\ge 0$.)
To prove $(14)$, for $n\ge 2$ let
$$b_n=a_{n-1}+\sum_{i=0}^{n-3}a_i\,.$$
Then
$$\begin{align*}
b_{n+5}-b_{n+4}-b_{n+3}+b_n&=a_{n+4}-a_{n+3}-a_{n+2}+a_{n-1}\\
&\quad+\sum_{i=0}^{n+2}a_i-\sum_{i=0}^{n+1}a_i-\sum_{i=0}^na_i+\sum_{i=0}^{n-3}a_i\\
&=a_{n+4}-a_{n+3}-a_{n+2}+a_{n-1}\\
&\quad+\color{red}{\sum_{i=n-2}^{n+2}a_i-\sum_{i=n-2}^{n+1}a_i}-\color{blue}{\sum_{i=n-2}^na_i}\\
&=a_{n+4}-a_{n+3}-a_{n+2}+a_{n-1}+\color{red}{a_{n+2}}-\color{blue}{\sum_{i=n-2}^na_i}\\
&=a_{n+4}-a_{n+3}+a_{n-1}-a_n-a_{n-1}-a_{n-2}\\
&=(a_{n+4}-a_{n+3})-a_n-a_{n-2}\\
&=(a_{n+1}+1)-a_n-a_{n-2}\tag{by (13)}\\
&=(a_{n+1}-a_n)-a_{n-2}+1\\
&=(a_{n-2}+1)-a_{n-2}+1\\
&=2\,,
\end{align*}$$
so if we set $c_n=2b_n$ for $n\ge 2$, we have $c_{n+5}-c_{n+4}-c_{n+3}+c_n=4$ for $n\ge 2$. Thus, the sequences $\langle k_n:n\ge 2\rangle$ and $\langle c_n:n\ge 2\rangle$ satisfy the same fifth order recurrence. It is now straightforward to verify by direct calculation that $c_n=k_n$ for $n=2,\ldots,6$ and hence that $(14)$ holds.