Project Euler 367
Project Euler 367
题目
Bozo sort
Bozo sort, not to be confused with the slightly less efficient bogo sort, consists out of checking if the input sequence is sorted and if not swapping randomly two elements. This is repeated until eventually the sequence is sorted.
If we consider all permutations of the first \(4\) natural numbers as input the expectation value of the number of swaps, averaged over all \(4!\) input sequences is \(24.75\).
The already sorted sequence takes \(0\) steps.
In this problem we consider the following variant on bozo sort.
If the sequence is not in order we pick three elements at random and shuffle these three elements randomly.
All \(3!=6\) permutations of those three elements are equally likely.
The already sorted sequence will take \(0\) steps.
If we consider all permutations of the first \(4\) natural numbers as input the expectation value of the number of shuffles, averaged over all \(4!\) input sequences is \(27.5\).
Consider as input sequences the permutations of the first \(11\) natural numbers.
Averaged over all \(11!\) input sequences, what is the expected number of shuffles this sorting algorithm will perform?
Give your answer rounded to the nearest integer.
解决方案
令\(n=11\)。输入序列是 \(1,2,\dots,n\) 的一个排列。用排列 \(\pi\in S_n\) 表示“位置 \(\to\) 值”的映射:$ (i)\(表示位置\)i$上的数。已经排好序当且仅当 \(\pi=\mathrm{Id}\)。其中\(\mathrm{Id}\)是指恒等置换。
一次操作:若未排序,随机选三个位点 \(i<j<k\),并对这三个位点上的元素做等概率的 6 种重排。
把这次“重排三个位点”看成对位置施加一个排列 \(\tau\in S_n\):\(\tau\) 在 \({i,j,k}\) 上是某个 \(S_3\) 元素,在其他位置不动。由于“新位置上的值来自旧位置”,更新是\(\pi'=\pi\circ\tau.\)
因此整个算法就是:在 \(\pi\neq\mathrm{Id}\) 时,做一次随机右乘 \(\tau\);在 \(\pi=\mathrm{Id}\) 时吸收(0 步结束)。
排列 \(\pi\) 的不交循环分解给出若干个循环,其循环长度构成一个多重集(例如 \((5,4,2)\))。该多重集在共轭下保持不变:若 \(\pi_2=g^{-1}\pi_1 g\),则 \(\pi_1,\pi_2\) 的循环长度多重集相同。反过来,循环长度多重集也恰好刻画了 \(S_n\) 的共轭类。由于这些长度之和为 \(n\),并且不计顺序,因此它等价于 \(n\) 的一个整数分拆,记作 \(\lambda\vdash n\)。我们称 \(\lambda\) 为 \(\pi\) 的循环类型。
关键在于:本题每一步随机作用的 \(\tau\)(对随机选取的三个位点做等概率重排)在分布上是共轭不变的。直观地说,这个随机操作对位置的重标号完全对称:
- 任意固定的换位(2-循环)出现的概率相同;
- 任意固定的 3-循环出现的概率相同;
- 恒等置换出现的概率也相同。
因此对任意 \(g\in S_n\),都有\(g^{-1}\tau g \stackrel{d}{=} \tau\)(其中 \(\stackrel{d}{=}\) 表示两者作为随机置换具有相同的概率分布)。
从而若 \(\pi_2=g^{-1}\pi_1 g\)(即 \(\pi_1,\pi_2\) 同循环类型),则
\[\pi_2\circ\tau= g^{-1}\pi_1 g\circ\tau= g^{-1}(\pi_1\circ(g\tau g^{-1}))g. \] 由于 \(g\tau g^{-1}\stackrel{d}{=}\tau\),可知 \(\pi_1\circ\tau\) 与 \(\pi_2\circ\tau\) 的分布互为共轭,因此它们的循环类型分布完全相同。
因此有结论:下一步的循环类型只依赖于当前的循环类型,与同类型内选取的具体排列无关。于是我们可以把状态空间从 \(n!\) 个排列压缩为“循环类型”的个数,也就是 \(n\) 的整数分拆数,记为 \(p(n)\)。当 \(n=11\) 时,\(p(n)=56\),因此只需在 56 个分拆类型上建立转移并求期望。
设分拆 \(\lambda\) 中长度为 \(k\) 的循环有 \(m_k\) 个(满足 \(\sum_k k m_k=n\))。循环类型为 \(\lambda\) 的排列个数为:\(N(\lambda)=\dfrac{n!}{\prod_{k\ge 1} k^{m_k}m_k!}.\)
因为题目“对所有 \(n!\) 个输入排列等权平均”,初始在类型 \(\lambda\) 的概率为\(w(\lambda)=\dfrac{N(\lambda)}{n!}.\)已排序态对应 \(\lambda=(1,1,\dots,1)\),其期望步数为 0。
对每个分拆 \(\lambda\vdash n\),构造一个代表排列 \(\pi_\lambda\):把 \(0,1,2,\dots,n-1\) 按分块组成若干个循环,每个循环用“向后轮换 1 位”实现(例如长度 \(L\) 的块形成 \((s,s+1,\dots,s+L-1)\))。
然后对所有三元组 \({i,j,k}\)以及 \(S_3\) 的 6 种重排,生成\(\pi'=\pi_\lambda\circ\tau,\)计算其循环类型 \(\mu\),计数累加。
总的等可能一步操作数为\(6\dbinom{n}{3}.\)因此在“循环类型”这一层,若从类型 \(\lambda\) 出发,经一步操作落到类型 \(\mu\) 的次数为 \(\#(\lambda\to\mu)\),对应的转移概率为
\[P_{\mu,\lambda}=\frac{\#(\lambda\to\mu)}{6\binom{n}{3}}.\]
这里采用的矩阵约定是:列索引表示出发类型 \(\lambda\),行索引表示到达类型 \(\mu\)。
令 \(t(\lambda)\) 为“当前循环类型为 \(\lambda\) 时到吸收态的期望步数”。对吸收态 \(\lambda_\star=(1^{n})\),有 \(t(\lambda_\star)=0\)。对其他类型:
\[ t(\lambda)=1+\sum_{\mu\neq \lambda_\star} P_{\mu,\lambda}t(\mu). \]
把 \(p(n)-1\) 个非吸收态组成向量 \(t\),令 \(Q\) 为对应的 \((p(n)-1)\times (p(n)-1)\) 子转移矩阵(排除吸收态),则
\[ (I-Q^T)t=\mathbf{1}, \]
解出 \(t\) 后,总平均期望为
\[ \mathbb{E}=\sum_{\lambda\neq \lambda_\star} w(\lambda)t(\lambda). \]
代码
1 | from fractions import Fraction |