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)$时间)。