We present the first known pivot Gray code for spanning trees of complete graphs, listing all spanning trees such that consecutive trees differ by pivoting a single edge around a vertex. This pivot Gray code thus addresses an open problem posed by Knuth in The Art of Computer Programming, Volume 4 (Exercise 101, Section 7.2.1.6, [Knuth, 2011]), rated at a difficulty level of 46 out of 50, and imposes stricter conditions than existing revolving-door or edge-exchange Gray codes for spanning trees of complete graphs. Our recursive algorithm generates each spanning tree in constant amortized time using $O(n^2)$ space. In addition, we provide a novel proof of Cayley's formula, $n^{n-2}$, for the number of spanning trees in a complete graph, derived from our recursive approach. We extend the algorithm to generate edge-exchange Gray codes for general graphs with $n$ vertices, achieving $O(n^2)$ time per tree using $O(n^2)$ space. For specific graph classes, the algorithm can be optimized to generate edge-exchange Gray codes for spanning trees in constant amortized time per tree for complete bipartite graphs, $O(n)$-amortized time per tree for fan graphs, and $O(n)$-amortized time per tree for wheel graphs, all using $O(n^2)$ space.
翻译:我们首次提出了完全图生成树的枢轴格雷码,该码能够枚举所有生成树,且相邻树仅通过绕顶点枢转单条边而相互转换。此枢轴格雷码解决了Knuth在《计算机程序设计艺术》第四卷(习题101,第7.2.1.6节,[Knuth, 2011])中提出的一个开放性问题,该问题难度评级为46/50,且比现有完全图生成树的旋转门或边交换格雷码施加了更严格的条件。我们的递归算法以常数分摊时间生成每个生成树,使用$O(n^2)$空间。此外,基于递归方法,我们为完全图中生成树数量的Cayley公式$n^{n-2}$提供了一个新颖证明。我们将算法扩展至为具有$n$个顶点的一般图生成边交换格雷码,实现每棵树$O(n^2)$时间与$O(n^2)$空间。对于特定图类,算法可优化为:对完全二分图以每棵树常数分摊时间、对扇图以每棵树$O(n)$分摊时间、对轮图以每棵树$O(n)$分摊时间生成生成树的边交换格雷码,且均使用$O(n^2)$空间。