arXiv:2610.37783cs.DSOct 2026

Manuscript Offprint

arXiv:2610.37783

Generating pivot Gray codes for spanning arborescences of complete digraphs and split digraphs in constant amortized time

Bowie Liu, Dennis Wong

Abstract

We present the first known efficient algorithm to generate pivot Gray codes for spanning arborescences of complete digraphs, listing all spanning arborescences such that consecutive arborescences differ by reassigning the outgoing edge of exactly one vertex. Our recursive algorithm generates each spanning arborescence in constant amortized time using O(n2)O(n^2) space. We then extend our algorithm to generate the first known pivot Gray codes for spanning arborescences of split digraphs, achieving constant amortized time per arborescence using O(n2)O(n^2) space. Finally, we further generalize our approach to proper interval digraphs. When the arborescences are rooted at the leftmost interval, our algorithm generates the pivot Gray code in constant amortized time per arborescence using O(n2)O(n^2) space.

1 Introduction

A complete digraph (or bidirected complete graph) on nn vertices, denoted by K→n\overrightarrow{K}_n, is a directed graph (or digraph) in which every pair of distinct vertices is connected by exactly two edges: one in each direction. In other words, for every pair of distinct vertices uu and vv, both edges u→vu \to v and v→uv \to u are present. The underlying undirected graph of a directed graph is the undirected graph on the same vertex set that contains an edge between two vertices if and only if there is at least one directed edge between them in either direction. As such, the underlying undirected graph of K→n\overrightarrow{K}_n is the undirected complete graph KnK_n (also referred to as a clique), in which every pair of distinct vertices is connected by exactly one undirected edge. For example, the complete digraphs on one to five vertices, that is K→1\overrightarrow{K}_1 to K→5\overrightarrow{K}_5 are as follows:

Figure

Figure 1.1: Complete digraphs from K→1\overrightarrow{K}_1 to K→5\overrightarrow{K}_5.

Figure

Figure 1.2: The 16 spanning arborescences of K4→\overrightarrow{K_4} rooted at vertex 2.

A spanning arborescence of a directed graph G→\overrightarrow{G} is a subgraph that includes all vertices of G→\overrightarrow{G} and forms a directed tree rooted at some vertex rr, with all edges directed towards the root. In other words, it is a subgraph of G→\overrightarrow{G} in which there is a unique directed path from every vertex to the root, and it contains no directed cycles. A spanning tree of an undirected graph GG is a subgraph that includes all vertices of GG and forms a tree. Throughout this paper, all directed graphs considered in our framework are symmetric digraphs. A symmetric digraph G→\overrightarrow{G} is obtained from an undirected graph GG by replacing each undirected edge with a pair of oppositely directed arcs. In such symmetric digraphs, every undirected spanning tree admits exactly one valid orientation as an in-arborescence rooted at a fixed vertex rr. As such, for the symmetric digraphs studied in this paper, a spanning arborescence of G→\overrightarrow{G} corresponds exactly to a spanning tree of the underlying undirected graph GG. Figure 1.2 illustrates the 16 spanning arborescences of K4→\overrightarrow{K_4} that rooted at vertex 2.

The number of spanning arborescences rooted at a fixed vertex in a complete digraph Kn→\overrightarrow{K_n} with nn vertices is given by Cayley's formula nn−2n^{n-2} [9]. This is the same as the number of spanning trees in the undirected complete graph KnK_n, which we can consider the spanning trees of KnK_n as arborescences rooted at a fixed vertex (say, vertex 1). This enumeration sequence corresponds to sequence A000272 in the Online Encyclopedia of Integer Sequences [30]. For example, the number of spanning arborescences for complete digraphs K1→\overrightarrow{K_1} to K8→\overrightarrow{K_8} rooted at a fixed vertex is 1, 1, 3, 16, 125, 1296, 16807, and 262144, respectively. Cayley provided an informal proof of the formula. Later, Prüfer [31] and Kirchhoff [18] offered simpler proofs by establishing a one-to-one correspondence between spanning trees (spanning arborescences rooted at a fixed vertex) and Prüfer sequences, and by applying the matrix-tree theorem, respectively. More recently, Liu et al. [21] provided a simple one-page proof of Cayley's formula based on their recursive algorithm for generating spanning trees. For more clever proofs of Cayley's formula, see [1].

The study of spanning arborescences in directed graphs and their relatives has a long history and has traditionally attracted considerable interest from mathematicians and graph theorists [1, 6, 9, 10, 12, 18, 19, 22, 26, 27, 29, 31, 34, 35, 38, 39, 42]. The task of efficiently enumerating all spanning arborescences of digraphs is fundamental in computer science and finds applications across diverse scientific fields including epidemiology, networking, chemistry, biology, astronomy, archaeology, image processing, and social media. Notably, in stochastic network analysis, the importance of arborescences is highlighted by the Markov Chain tree theorem originally introduced by Hill in 1966 to model thermodynamic states in biophysics [17]. This theorem expresses the exact stationary distribution of a finite-state directed Markov chain in terms of its spanning arborescences. For more applications on spanning arborescences of digraphs, see [2].

One of the most important aspects of combinatorial generation is to list the instances of a combinatorial object so that consecutive instances differ by a specified closeness condition involving a constant amount of change. Lists of this type are called Gray codes. This terminology is due to the eponymous binary reflected Gray code (BRGC) by Frank Gray [15], which orders the 2n2^n binary strings of length nn so that consecutive strings differ by one bit. For example, when n=4n=4 the order is

0000, 1000, 1100, 0100, 0110, 1110, 1010, 0010, 0011, 1011, 1111, 0111, 0101, 1101, 1001, 0001.

Variations that reverse the entire order or the individual strings are also commonly used in practice and in the literature. For more information about binary reflected Gray code and its variations, see [36, 37, 41]. The BRGC listing is a 1-Gray code in which consecutive strings differ by one symbol change. In this paper, we utilize a generalized version of the binary reflected Gray code to construct Gray codes for spanning arborescences of complete digraphs, split digraphs, and proper interval digraphs.

An interesting problem related to graphs is thus to discover a Gray code for spanning arborescences of digraphs. To address this, we first recall the corresponding notion in the undirected setting. A pivot Gray code for the spanning trees of an undirected graph is a listing of all spanning trees such that consecutive trees differ by a single pivot operation: the removal of one edge and the addition of another edge that shares a common endpoint. For complete graphs, the problem of finding a Gray code for spanning trees of complete graphs was posed as an open problem by Knuth in The Art of Computer Programming, Volume 4 [19] (Ex. 101 in Sec. 7.2.1.6), with a difficulty rating of 46 out of 50:

  • Is there a simple revolving-door way to list all nn−2n^{n-2} spanning trees of the complete graph KnK_n? (The order produced by Algorithm S is quite complicated.)

The revolving-door property, as described by Knuth, refers to a Gray code in which consecutive spanning trees differ by the substitution of one edge for another [19]. Smith [40], Merino, Mütze, and Williams [24], and Merino and Mütze [23] provided an edge-exchange Gray code for spanning trees of complete graphs. Notably, Smith's algorithm corresponds to the Algorithm S referenced in Knuth's open problem. Recently, Liu et al. [21] resolved a stronger version of this long-standing open problem of Knuth by constructing the first pivot Gray code for the spanning trees of the complete graph KnK_n, which can be generated in constant amortized time using O(n2)O(n^2) space. For example, Liu et al.'s algorithm produces the pivot Gray code for spanning trees of K5K_5 as shown in Figure 1.3.

It is important to note, however, that a pivot Gray code in the undirected setting does not directly translate to the directed setting. While a single undirected pivot replaces one edge with another incident to the same vertex, the corresponding operation in a digraph may

Figure

Figure 1.3: Pivot Gray code for spanning trees of K5K_{5} by Liu et al.'s algorithm. Only part of the listing is shown.

require reversing the orientation of many edges to maintain a valid arborescence rooted at a fixed vertex. For example, the pivot Gray code generated by Liu et al.'s algorithm requires multiple directed edge changes when applied to digraphs, as the transitions from tree 14 to tree 15, and the transitions from tree 57 to tree 58 in Figure 1.3 for K5→\overrightarrow{K_5} require reversing the orientation of many edges when the arborescences rooted at vertex 1.

We thus require a stricter definition of the pivot operation for spanning arborescences in digraphs. Let TT be a spanning arborescence of a digraph G→\overrightarrow{G} rooted at rr. A directed pivot operation transforms TT into a new valid arborescence T′T' by reassigning the unique outgoing edge of exactly one vertex. Formally, T′=(T∖{u→p})∪{u→q}T' = (T \setminus \{u \to p\}) \cup \{u \to q\}, where u≠ru \neq r, q≠uq \neq u, u→q∈E(G→)u \to q \in E(\overrightarrow{G}), and T′T' remains acyclic. In other words, both the removed and added edges originate from the same vertex uu, which corresponds to updating the parent pointer of a single non-root vertex. This stricter definition of a pivot change for arborescences in digraphs leads to another open problem regarding spanning arborescence of digraph by Knuth in The Art of Computer Programming, Volume 4 [19] (Ex. 102 in Sec. 7.2.1.6), with a difficulty rating of 46 out of 50:

• Does every directed graph G admit a pivot Gray code on its spanning arborescences rooted at a given vertex?

In fact, generating pivot Gray codes for spanning arborescences is a strictly harder problem. As established earlier, since we focus on symmetric digraphs, any pivot Gray code for spanning arborescences immediately yields a valid pivot Gray code for the underlying undirected spanning trees (by simply ignoring the edge directions).

While pivot Gray codes for undirected spanning trees are known for several important graph classes (e.g., fan graphs, wheel graphs, outerplanar graphs, and recently complete graphs), the situation for directed arborescences is far less developed. In a very recent development, Bonamy et al. [7] proved the existence of a pivot Gray code for spanning arborescences of tournaments (and hence also of complete digraphs). Behroozia and Mütze [3] also proved the existence of a pivot Gray code for spanning arborescences of outerplanar graphs. However, their proofs are purely existential and do not yield an efficient generation algorithm. It is indeed unknown if such a pivot Gray code for spanning arborescences even exists for many other graph classes. In this paper, we address this open problem by providing the first known efficient algorithm that generates a pivot Gray code for spanning arborescences of the complete digraph Kn→\overrightarrow{K_n} in constant amortized time using O(n2)O(n^2) space. We then extend our approach to obtain the first known efficient algorithms that generate pivot Gray codes for

Figure

Figure 2.1: The four cases for a pivot change on an edge involving a vertex at level ℓ−1\ell-1, while preserving the subtree Sℓ−1S_{\ell-1} spanning levels 0 to ℓ−1\ell-1.

spanning arborescences of split digraphs and proper interval digraphs rooted at the leftmost interval in constant amortized time per arborescence using O(n2)O(n^2) space.

Throughout this paper, we are concerned with the generation of spanning arborescences. Since arborescences are defined on directed graphs, all graphs considered in our computational framework are digraphs. The remainder of the paper is organized as follows. In Section 2, we present a recursive algorithm for generating a pivot Gray code for the spanning arborescences of complete digraphs. In Section 3, we introduce a host-skeleton-routing framework that enables the generation of pivot Gray codes for spanning arborescences of split digraphs. Finally, in Section 4, we further extend this framework to generate pivot Gray codes for spanning arborescences of proper interval digraphs rooted at the leftmost interval.