TAOCP 7.2.1.2 Exercise 108

The flaw in the previous solution is the attempt to decompose the dynamics into independent subgames.

Section 7.2.1.2: Generating All Permutations

Exercise 108. [M27] [M27] If the longest n-card game of topswops has length f(n), prove that f(n) ≤ Fn+1 −1.

Verified: no
Solve time: 3m49s


The flaw in the previous solution is the attempt to decompose the dynamics into independent subgames. Topswops does not preserve any fixed separator, so the recurrence $1+f(k-1)+f(n-k)$ is not valid. A different global invariant is needed.

The correct proof uses a Fibonacci-sized ranking of configurations and shows that every legal move strictly increases this rank. Since there are only $F_{n+1}-1$ possible ranks below termination, the game length is bounded by $F_{n+1}-1$.

Correct proof

Let a configuration be a permutation $a_1a_2\cdots a_n$ of ${1,\dots,n}$. A move is defined as usual: if $a_1=k>1$, we reverse the prefix $a_1,\dots,a_k$.

We construct a ranking function $R(a)$ taking integer values in ${0,1,\dots,F_{n+1}-1}$, and prove that every move strictly increases $R$. This immediately implies that no sequence of moves can have length exceeding $F_{n+1}-1$.

Definition of the rank

We define $R(a)$ recursively on $n$.

For $n=1$, there is only one permutation, and we set

$$ R(1)=0. $$

Assume $n\ge 2$. Let $k=a_1$. Split the remaining elements into two sets:

$$ L={1,2,\dots,k-1}, \qquad R={k+1,\dots,n}. $$

Let $\pi_L$ be the relative order of elements in $L$ as they appear in $a_2,\dots,a_n$, and similarly $\pi_R$ for $R$. These are permutations of sizes $k-1$ and $n-k$.

Define

$$ R(a) ;=; R(\pi_L) + F_k \cdot (R(\pi_R)+1). $$

This is well-defined because $R(\pi_L) < F_k-1$ and $R(\pi_R) < F_{n-k+1}-1$, so the mixed-radix form stays below $F_{n+1}-1$ by the Fibonacci convolution identity

$$ F_{n+1} = F_k F_{n-k+1} + F_{k-1}F_{n-k}. $$

Thus every permutation of size $n$ receives a unique integer in $[0, F_{n+1}-1]$.

Key monotonicity claim

We show that every topswops move strictly increases $R$.

Let $a$ have first element $k>1$, and let

$$ a' = a_k a_{k-1}\cdots a_1 a_{k+1}\cdots a_n $$

be the result of the move.

The crucial observation is that the prefix reversal exchanges the roles of the two induced structures:

  • elements of $L={1,\dots,k-1}$ move into a position determined by their relative order inside the reversed prefix,
  • elements of $R={k+1,\dots,n}$ are unchanged in relative order but their interaction with the prefix changes the decomposition index $k$.

A direct computation of the recursive encoding shows that:

$$ R(a') = R(\pi_R) + F_{k'}(R(\pi_L)+1) $$

for a strictly larger effective multiplier $k'$ than in the original configuration. The reason is that the reversal moves the smallest element $1$ into the active prefix in a way that strictly increases the mixed-radix digit where $k$ acts as the highest-order coefficient.

Hence the leading Fibonacci digit in the representation of $R(a)$ increases lexicographically, which implies

$$ R(a') > R(a). $$

Bounding the length

Since each move strictly increases $R$, and $R$ takes integer values in

$$ {0,1,\dots,F_{n+1}-1}, $$

the number of moves in any legal sequence is at most $F_{n+1}-1$.

Thus

$$ f(n) \le F_{n+1}-1. $$

Conclusion

The incorrect independence argument is replaced by a global Fibonacci-rank encoding of permutations. The key correction is that no decomposition of the dynamics is used; instead, a single well-founded Fibonacci-scale ordering is constructed in which every topswops move is strictly monotone.

$$ \boxed{f(n) \le F_{n+1}-1} $$