We provide a simple and natural solution to the problem of generating all $2^n \cdot n!$ signed permutations of $[n] = \{1,2,\ldots,n\}$. Our solution provides a pleasing generalization of the most famous ordering of permutations: plain changes (Steinhaus-Johnson-Trotter algorithm). In plain changes, the $n!$ permutations of $[n]$ are ordered so that successive permutations differ by swapping a pair of adjacent symbols, and the order is often visualized as a weaving pattern involving $n$ ropes. Here we model a signed permutation using $n$ ribbons with two distinct sides, and each successive configuration is created by twisting (i.e., swapping and turning over) two neighboring ribbons or a single ribbon. By greedily prioritizing $2$-twists of the largest symbol before $1$-twists of the largest symbol, we create a signed version of plain change's memorable zig-zag pattern. We provide a loopless algorithm (i.e., worst-case $\mathcal{O}(1)$-time per object) by extending the well-known mixed-radix Gray code algorithm.
翻译:我们针对生成 $[n] = \{1,2,\ldots,n\}$ 的所有 $2^n \cdot n!$ 个带符号排列问题,提出了一种简洁而自然的解决方案。该方案优美地推广了最著名的排列排序方法:平变序(Steinhaus-Johnson-Trotter算法)。在平变序中,$[n]$ 的 $n!$ 个排列按顺序排列,使得相邻排列仅通过交换一对相邻符号得到,这种顺序常被可视化为涉及 $n$ 条绳索的编织图案。在此,我们使用具有正反两面的 $n$ 条丝带来建模带符号排列,每个后续构型通过扭转(即交换并翻转)两条相邻丝带或单条丝带产生。通过贪心地优先对最大符号执行 $2$-扭转(双丝带扭转),再执行 $1$-扭转(单丝带扭转),我们创建了平变序中令人难忘的锯齿形图案的带符号版本。通过扩展著名的混合进制格雷码算法,我们提出了一种无循环算法(即最坏情况下每个对象生成时间为 $\mathcal{O}(1)$)。