๐ค AI Summary
This paper resolves an open problem (difficulty 46/50) posed by Knuth in *The Art of Computer Programming*, Volume 4: constructing a pivot Gray code for spanning trees of the complete graph $K_n$, where consecutive trees differ by a single edge pivotโi.e., exchanging one incident edge at a common vertex. We present the first recursive algorithm satisfying this strict local transformation constraint, achieving constant amortized time per tree. This yields a novel combinatorial proof of Cayleyโs formula $n^{n-2}$. Furthermore, we extend our approach to complete bipartite graphs, fan graphs, and wheel graphs, attaining constant or linear amortized time enumeration on these classes. The algorithm uses $O(n^2)$ space. Our work establishes a new paradigm for Gray coding of spanning trees, unifying structural insights with efficient enumeration under stringent adjacency constraints.
๐ Abstract
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.