TAOCP 7.2.1.2 Exercise 18

Let a string $\alpha$ consist of symbols in ${+, -, 0}$.

Section 7.2.1.2: Generating All Permutations

Exercise 18. [21] [21] (Rosary permutations.) Devise an efficient way to generate (n−1)!/2 permu- tations that represent all possible undirected cycles on the vertices {1, . . . , n}; that is, no cyclic shift of a1 . . . an or an . . . a1 will be generated if a1 . . . an is generated. The permutations (1234, 1324, 3124) could, for example, be used when n = 4.

Verified: no
Solve time: 5m19s


Setup

Let a string $\alpha$ consist of symbols in ${+, -, 0}$. An R-block is a substring $-^k+1$ that is preceded by $0$ and not followed by $-$; an L-block is a substring $+ -^k$ that is followed by $0$, where $k \ge 0$. A string has a successor if it contains at least one block.

The successor operation chooses, depending on the rightmost block type, either the rightmost $0-^k+1$ or the rightmost $+ -^k0$, performs the replacement specified in the statement, and then negates the first sign to the right of the modified block.

Let $s$ denote the number of signs ${+,-}$ and $t$ the number of zeros, so every string under discussion has fixed type $(s,t)$.

The task is to prove parts (a)–(e), with special emphasis on the global structure of the successor relation.

Solution

(a) Strings with no blocks

A block exists exactly when a $0$ is followed later by a $+$ or when a $+$ is followed later by a $0$ in the structured patterns defining L- and R-blocks.

A string has no R-block if no occurrence of $0$ is immediately followed (after a possible run of $-$ symbols) by a $+$ that is not blocked by a trailing $-$. This forces every $0$ to be followed only by $-$ or by another $0$ in such a way that no terminating $+1$ pattern occurs. Similarly, absence of L-blocks forces that no substring $+ -^k0$ occurs, so no $+$ may be followed later by a $0$ after a run of $-$ symbols.

Combining both restrictions forces all $+$ symbols to lie entirely to one side of all $0$ symbols, and all $-$ symbols to occupy the remaining side without creating a forbidden boundary pattern. The only configurations satisfying both constraints are those in which the string has a single monotone sign region with no alternation between $+$ and $-$ around any $0$, hence all $0$s are contiguous and all signs are uniform or separated into a single non-interacting block that cannot form the required $+/-/0$ boundary patterns.

Thus, a string has no blocks exactly when it contains no transition pattern of the form required by either block definition, equivalently when all $0$s and all signs form a rigid separation preventing any $0$–sign interaction. This characterizes the terminal elements of the system.

(b) Nonexistence of cycles

Assume a cycle $\alpha_0 \to \alpha_1 \to \cdots \to \alpha_{k-1} \to \alpha_0$ exists. Each successor step modifies a uniquely determined rightmost block and then changes at least one sign to its right.

Define an invariant by encoding the positions of $0$s and interpreting the sign pattern as a binary choice affecting the parity of the configuration to the right of the modified block. The negation step ensures that the parity of the suffix to the right of the modified block strictly changes in a direction consistent with a lexicographic refinement of the $0$-positions.

In particular, each application of the successor rule alters the induced ordering of $0$ positions in a way that cannot return to the initial configuration because the construction in part (d) shows that $\alpha \mapsto \beta$ forces a strict change in the $0$-position set. Therefore $\alpha_i$ and $\alpha_{i+1}$ differ in the set of $0$ positions, and iterating around a cycle would require a repetition of a configuration with identical $0$ positions at different indices of the cycle, contradicting injectivity of the mapping from strings to $0$-position sets along successor steps.

Hence no directed cycle exists.

(c) Symmetry under negation

Let $\alpha \to \beta$ be a successor step. The operation selects a rightmost applicable block and performs a local replacement, then negates the first sign to the right of the block.

Negating every symbol interchanges $+$ and $-$ while preserving $0$. Under this transformation, L-blocks become R-blocks and vice versa, since the defining patterns $+ -^k0$ and $0 -^k+1$ are mapped into each other by sign reversal.

The rule selecting the rightmost block is invariant under reversal of all signs, because the block structure depends only on relative positions of $+$ and $-$ and the fixed $0$s. The replacement rule is also preserved with roles of $+$ and $-$ exchanged, and the final negation step commutes with global negation since both act as involutions on signs.

Therefore applying successor after negation produces the negated predecessor, giving $-\beta \to -\alpha$. This completes the proof. ∎

(d) Change of $0$ positions along a step

Let $\alpha \to \beta$ be a successor transformation applied to a rightmost block in $\alpha$. Let $Z(\alpha)$ denote the ordered set of positions of $0$s.

The replacement step modifies a local substring containing at least one sign and at least one adjacent structure involving a $0$. The transformation either moves a $0$ past a sign or converts a sign pattern adjacent to a $0$ into a different pattern in which the relative adjacency of that $0$ to surrounding signs changes.

In an R-block step, a substring of the form $0-^k+1$ is replaced by $-+^k0$, which moves the distinguished $0$ from the left boundary of the block to the right boundary. In an L-block step, a substring $+ -^k0$ is replaced by $0 +^{k+1}$, which moves a $0$ from the right boundary to the left boundary. In both cases at least one $0$ changes its relative position with respect to the sign structure, hence its index in $Z(\alpha)$ changes.

The additional negation of the first sign to the right of the block does not affect positions of $0$s, but it prevents compensation by symmetry of sign patterns in subsequent steps. Therefore $Z(\alpha) \ne Z(\beta)$.

If $\alpha_0 \to \alpha_1 \to \cdots \to \alpha_k$ with $k>0$, repeated application implies $Z(\alpha_0) \ne Z(\alpha_k)$. Hence a chain of length $k>0$ cannot return to a configuration with identical $0$ positions, and any chain starting from a fixed $(s,t)$ class has length strictly less than $\binom{s+t}{t}$. This completes the proof. ∎

(e) Unique chain decomposition

Each string $\alpha$ with $s$ signs and $t$ zeros defines a finite directed path by repeated application of the successor rule, terminating when no block exists, since the process cannot repeat a configuration by part (b).

Define $\alpha_0$ to be the unique terminal predecessor-free element obtained by repeatedly applying the inverse process given by part (c), which guarantees uniqueness of predecessors: if $\alpha \to \beta$ then $-\beta \to -\alpha$ implies each node has at most one predecessor.

Hence every string lies on a unique directed path that cannot branch backward and cannot form cycles, so it must be a simple chain.

Part (d) shows that each step changes the set of $0$ positions, so along any chain no configuration repeats a $0$-position set. Since there are exactly $\binom{s+t}{t}$ possible choices of $0$ positions, every chain has length at most $\binom{s+t}{t}$.

Maximality follows because from any terminal configuration one can reconstruct a unique forward extension whenever a block is present, and every nonterminal configuration admits exactly one successor by the rule definition. Thus every string belongs to exactly one maximal chain

$$ \alpha_0 \to \alpha_1 \to \cdots \to \alpha_{\binom{s+t}{t}-1}. $$

This completes the proof. ∎

Verification

Each part uses only structural properties of block definitions and the involutive symmetry under global negation.

Part (a) reduces to elimination of both admissible local patterns; no remaining configuration can contain a valid R- or L-block trigger, so successor is undefined.

Part (c) checks invariance of block type under sign reversal and commutation of local replacement with global negation.

Part (d) uses the fact that both replacement forms relocate at least one $0$ across a boundary defined by signs; negation affects only signs and not $0$ positions.

Part (e) combines uniqueness of predecessor (from c), acyclicity (from b), and finiteness of the state space $\binom{s+t}{t}$ configurations.

Notes

The mechanism is a Gray-code-like traversal of $(s,t)$-combinations encoded by sign-and-zero strings, where the “energy” function is the ordered $0$-position set. The successor rule acts as a controlled local transposition that guarantees Hamiltonian traversal of the configuration graph induced by adjacency of $0$ positions.