# Polynomial-delay generation of functional digraphs up to isomorphism

Oscar Defrain, Antonio Porreca, Ekaterina Timofeeva

► To cite this version:

Oscar Defrain, Antonio Porreca, Ekaterina Timofeeva. Polynomial-delay generation of functional digraphs up to isomorphism. 2026. hal-05534666

HAL Id: hal-05534666

https://amu.hal.science/hal-05534666v1

Preprint submitted on 3 Mar 2026

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons CC BY 4.0 - Attribution - International License
Polynomial-delay generation of functional digraphs
up to isomorphism

Oscar Defrain, Antonio E. Porreca, and Ekaterina Timofeeva

Aix-Marseille Université, CNRS, LIS, Marseille, France
oscar.defrain@lis-lab.fr, antonio.porreca@lis-lab.fr, ekatim239@gmail.com

**Abstract.** We describe a procedure for the generation of functional digraphs up to isomorphism; these are digraphs with uniform outdegree 1, also called mapping patterns, finite endofunctions, or finite discrete-time dynamical systems. This procedure is based on a reverse search algorithm for the generation of connected functional digraphs, which is then applied as a subroutine for the generation of arbitrary ones. Both algorithms output solutions with $O(n^2)$ delay and require linear space with respect to the number $n$ of vertices.

**Keywords:** functional digraphs, algorithmic enumeration, reverse search, graph isomorphism, finite dynamical systems

# 1 Introduction and motivation

A finite, discrete-time dynamical system $(A, f)$, called in the following just a dynamical system for brevity, is simply a finite set $A$ of states together with a function $f: A \to A$ describing the evolution of the system in time. A dynamical system can be equivalently described by its transition digraph, which has $V = A$ as its set of vertices and $E = \{(a, f(a)): a \in A\}$ as its set of arcs, that is, each state has an outgoing edge pointing to the next state. Since the dynamical systems we are dealing with are deterministic, their transition digraphs are all and only the digraphs having uniform outdegree 1, that is, *functional digraphs*.

The synchronous execution of two dynamical systems $A$ and $B$ gives a dynamical system $A \otimes B$, whose transition digraph is the *direct product* [15] of the transition digraphs of $A$ and $B$. This product, together with a disjoint union operation of sum, gives a semiring structure over dynamical systems up to isomorphism [10] with some interesting algebraic properties, notably the lack of unique factorisation into irreducible digraphs. In order to develop the theory of the semiring of dynamical systems, it is useful to be able to find examples and counterexamples to our conjectures, and this often amounts to being able to efficiently generate all functional digraphs of a given number $n$ of vertices, up to isomorphism. Remark that the number of non-isomorphic functional digraphs over $n$ vertices (sequence A001372 on the OEIS [23]) is exponential, asymptotically $c \times d^n/\sqrt{n}$ for some constants $c$ and $d > 1$ [21]. As a consequence, there is no hope to devise an algorithm listing these objects in polynomial time in $n$. Rather, the kind of efficiency we must aim for is either guaranteeing small exponential time aiming at reducing as much as possible the base of the exponent, referred to as the *input-sensitive approach* [12], or the ability to generate all solutions in a time which is polynomial in the sizes of the input plus the output, known as the *output-sensitive approach* [16]. Within this second framework, algorithms running with *polynomial delay* (requiring polynomial time in the input size between consecutive outputs) are considered among the most efficient in algorithmic enumeration. In this paper, we place ourselves in the output-sensitive approach and refer the reader to [16, 24] for more details on enumeration algorithms and their complexity.
Since the outdegree of each vertex is exactly 1 and the number of vertices is finite, the general shape of a functional digraph is a disjoint union of connected components, each consisting of a limit cycle $\langle v_1, \dots, v_k \rangle$, where the vertices $v_1, \dots, v_k$ are the roots of $k$ directed unordered rooted trees (simply referred to as trees in the following), with the arcs pointing towards the root.

Enumeration problems for several classes of graphs have been analysed in the literature. For instance, efficient isomorphism-free generation algorithms for rooted, unordered trees are well known, even requiring only amortised constant time per solution [2], and there exist polynomial delay algorithms for the isomorphism-free generation of *undirected* graphs [13]. More general techniques for the generation of combinatorial objects have been described by McKay [19]. For a practical implementation of generators for several classes of graphs we refer the reader to software such as *nauty* and *Traces* [20].

However, the class of functional digraphs does not seem to have been considered yet from the point of view of efficient generation algorithms. Here we first propose a $O(n^2)$-delay, linear space algorithm for the generation of *connected* $n$-vertex functional digraphs (sequence A002861 on the OEIS [22]), based on an isomorphism code which avoids generating multiple isomorphic digraphs. This assumes the word RAM model with word size $O(\log n)$ [14]. The algorithm is based on the reverse search technique [1] that proved to be efficient for the generation of a wide range of objects, including spanning trees of a graph, triangulations in the plane, or cells in a hyperplane arrangement, to cite a few [1, 7, 27, 29]. In a nutshell, this technique amounts to traverse in a depth-first search manner an implicit solution tree where nodes are solutions, and where edges are defined by some parent-child relation between solutions. During the traversal, children are obtained by merging trees having adjacent roots along the limit cycle. A notable feature of this algorithm is that it can moreover be adapted in order to produce the successor (or predecessor) of any given solution in $O(n^2)$ time as well, and only needs linear space. This procedure is then used as a subroutine in order to generate *all*, non necessarily connected functional digraphs with the same delay and space.

## 2 Isomorphism codes for connected functional digraphs

In order to avoid generating multiple isomorphic functional digraphs, we first define a *canonical representation* based on an isomorphism code, which would also allow us to check in polynomial time whether two given functional digraphs are isomorphic when given by another representation (e.g., adjacency lists or matrices).<fnref n="1" />

Isomorphism codes for unordered rooted trees (which can be taken as directed with the arcs pointing towards the root, as is needed in our case) are well known in the literature; for instance, *level sequences* (sequences of node depths given by a preorder traversal of the tree, arranged in lexicographic order) can be used for this purpose [2]. Here we adopt a solution due to Jack Edmonds and described by Busacker and Saaty [6, pp. 196–199] and Valiente [28, p. 118], which has the useful property that the isomorphism code of a tree directly contains as subsequences the isomorphism codes of its subtrees.

**Definition 1 (code of a tree).** Let $T = (V, E)$ be a tree. Then, the isomorphism code of $T$ is the sequence of integers

$$ \text{code } T = \langle |V| \rangle \text{ code } T_1 \text{ \dots \dots \dots code } T_k $$

<footnote n="1">¹ Since functional digraphs are planar, they can actually be checked for isomorphism in linear time [5] or in logarithmic space [8].</footnote>
<figure><img src="image_2.png" /><figcaption>**Fig. 1.** Isomorphism codes for a tree (Definition 1), a connected (Definition 2) and a disconnected functional digraph (Definition 14). Notice the different nesting depths for the angled brackets of the three codes.</figcaption></figure>

where $T_1, \dots, T_k$ are the immediate subtrees of $T$, i.e., the subtrees having as roots the predecessors of the root of $T$, arranged in lexicographically nondecreasing order of code, and ⌜ denotes sequence concatenation. In particular, if $T$ is trivial, i.e., if $|V| = 1$, then code $T = \langle 1 \rangle$.

See Fig. 1 for an example isomorphism code for a tree.

For simplicity, in the rest of the paper, we identify a tree with its own code, i.e., we often write “$T$” instead of “code $T$” where no ambiguity arises. We also denote the lexicographic order on tree isomorphism codes with the symbol $\leq$. As a consequence, a sentence such that “$T_1 \leq T_2$” is meant to be interpreted as “the isomorphism code of the tree $T_1$ is less than or equal to that of $T_2$ in lexicographic order”. This order has the trivial tree $\langle 1 \rangle$ as its minimum. In the following, we will denote by $|T|$ the length of the code of $T$; note that this value coincides with the number of vertices of $T$ as well as the first integer starting its code.

Since a connected functional digraph consists of a sequence of trees arranged along a cycle, and all rotations of the sequence are equivalent (i.e., they correspond to isomorphic digraphs), we choose a canonical one as its isomorphism code.

**Definition 2 (code of a connected functional digraph).** The isomorphism code of a connected functional digraph $C = (V, E)$ is the lexicographically minimal rotation

$$ \text{code } C = \langle \text{code } T_1, \dots, \text{code } T_k \rangle $$

of the sequence of isomorphism codes of its trees taken in order along the cycle, i.e., such that for all integer $r$ we have

$$ \langle \text{code } T_1, \dots, \text{code } T_k \rangle \leq \langle \text{code } T_{1+(1+r) \bmod k}, \dots, \text{code } T_{1+(k+r) \bmod k} \rangle. $$

Fig. 1 also contains an example of connected functional digraph code.

For brevity, we refer to connected functional digraphs as *components* and, as with trees, we identify a component $C$ with its own code. A valid code for a component $C$ is also called a *canonical form* of $C$; unless otherwise specified, in the rest of the paper we consider all components to be in canonical form. As for trees, we denote the lexicographic order on components (more precisely, on their isomorphism codes) by $\leq$, and by $|C|$ the number of trees along the cycle, i.e., the integer $k$ in the definition above.

Isomorphism codes for arbitrary (i.e., non necessarily connected) functional digraphs will be defined later, in Section 4, since they will be based on the order of generation of components.

Notice that the space required for the isomorphism code of a tree or a connected functional digraph (and later, of an arbitrary functional digraph) of $n$ vertices is linear on the word RAM model, although the actual number of bits is $O(n \log n)$, since the codes consist of $n$ integers ranging from 1 to $n$.
### 3 Generation of connected functional digraphs

We describe an algorithm based on reverse search [1] for the enumeration of connected functional digraphs. This technique is a particular case of a technique called supergraph method (or solution graph traversal) which amounts to traversing in a depth-first search (DFS) manner an implicit solution graph where nodes are solutions, and edges are defined by some reconfiguration relation between solutions [16, 18]. In the framework of reverse search, the solution graph is required to be a tree, where the edges are defined by a parent relation. The time and space complexities of the enumeration then essentially boil down to the time and space needed to generate the children of a given node and compute its parent. When finding a child of a solution, we continue the traversal on this node. When all children have been found, we backtrack. When backtracking, we find the next child and continue on this child. Thus, in general, reverse search only needs memory space that is linear in the height of the solution tree times the space needed to generate children. As of the delay, it only depends on the time needed to compute the parent and the delay needed to generate children when using folklore tricks such as the alternating output technique [26]. We refer the reader to [1, 11, 25, 27] for more details on this technique.

The parent relation we will consider will be based on the following tree merging operation, to be applied to the isomorphism codes of two adjacent trees along the cycle.

**Definition 3 (tree merging).** Let $T_1$ and $T_2$ be trees with isomorphism codes $\langle x_1, \dots, x_k \rangle$ and $\langle y_1, \dots, y_l \rangle$, respectively. Then

$$ \text{merge}(T_1, T_2) = \langle x_1 + y_1, x_2, \dots, x_k, y_1, \dots, y_l \rangle $$

that is, the merge is obtained by the concatenation of the codes of the two trees, with the first element updated in order to reflect the new total length.

The following is an immediate consequence of the definition of the merging operation:

**Remark 4.** If $T_1$ and $T_2$ are trees and merge($T_1, T_2$) is a valid tree isomorphism code, then it represents the tree obtained by connecting $T_2$ as an immediate subtree of $T_1$ (i.e., by connecting $T_2$ to the root of $T_1$).

However, notice that, depending on $T_1$ and $T_2$, the result of merge($T_1, T_2$) is not necessarily a valid tree isomorphism code, as it may not be lexicographically nondecreasing.

**Example 5.** Suppose $T_1 = \langle 4, 3, 2, 1 \rangle$ and $T_2 = \langle 3, 1, 1 \rangle$. Then merge($T_1, T_2$) = $\langle 7, 3, 2, 1, 3, 1, 1 \rangle$, which is not a valid isomorphism code, since the subtree code $\langle 3, 2, 1 \rangle$ is lexicographically larger than $\langle 3, 1, 1 \rangle$ which follows it (the actual code for this tree is $\langle 7, 3, 1, 1, 3, 2, 1 \rangle$).

Furthermore, even if the merge of two adjacent trees in a component in canonical form is successful, this does not necessarily produce a valid component isomorphism code either.

**Example 6.** Consider the component $C = \langle T_1, T_2, T_3 \rangle = \langle \langle 1 \rangle, \langle 1 \rangle, \langle 1 \rangle \rangle$. By merging $T_1$ and $T_2$ we obtain $\langle \text{merge}(T_1, T_2), T_3 \rangle = \langle \langle 2, 1 \rangle, \langle 1 \rangle \rangle$ which is not a valid code, since its rotation $\langle \langle 1 \rangle, \langle 2, 1 \rangle \rangle$ is strictly inferior in lexicographic order.

Thus, when merging tree codes in a component (in order to produce a new component, as we will describe later) one always needs to check whether the result is indeed a valid code.

We define an inverse of the merging operation on trees that we call unmerging.

**Definition 7 (tree unmerging).** Given a tree $T$, we say that unmerge($T$) = $(T_1, T_2)$ if and only if merge($T_1, T_2$) = $T$.
Notice that there is only one way to unmerge a tree, i.e., detaching the code of its rightmost subtree $T_2$ (which is already a valid tree isomorphism code) and updating the first element of the remaining tree code in order to reflect the new, shorter length; this is done in linear time and produces a valid tree isomorphism code $T_1$, since the remaining subtrees are still in nondecreasing order. Thus unmerge is indeed a well-defined function. Remark that unmerge($T$) = ($T_1, T_2$) implies $T_1, T_2 < T$, since $T_1, T_2$ are proper subtrees of $T$ and hence have strictly less vertices (their first elements are smaller than the first element of $T$).

Notice that a minimal lexicographic rotation always begins with a minimum element of the sequence (with respect to the ordering on elements); more generally, one of the longest subsequences of minimum elements must appear as the prefix of a minimal rotation. Furthermore, a maximal length sequence of one or more minimum elements can never appear as a proper suffix (otherwise, by rotating to the right, we would obtain a smaller rotation); nonetheless, further copies of the same minimum might appear in intermediate positions of the sequence. These basic observations will be of use in the proof of the next lemma.

**Lemma 8.** Let $C = \langle T_1, \dots, T_k \rangle$ be a component in canonical form with at least one nontrivial tree, and let $T_h$ be the leftmost such nontrivial tree. Furthermore, let $(U_1, U_2) = \text{unmerge}(T_h)$ with $V_1 = \min\{U_1, U_2\}$ and $V_2 = \max\{U_1, U_2\}$. Then

$$C^* = \langle T_1, \dots, T_{h-1}, V_1, V_2, T_{h+1}, \dots, T_k \rangle$$

is also in canonical form; we call $C^*$ the parent of $C$ and denote it by $\text{Parent}(C)$.

*Proof.* As discussed above, $U_1$ and $U_2$, and as a consequence $V_1$ and $V_2$, are always valid tree isomorphism codes. Thus, we only need to show that $C^*$ is a minimal rotation. We analyse three sub-cases.

1. If $T_1$ is the leftmost nontrivial tree of $C$ (i.e., if $h=1$), then $C^* = \langle V_1, V_2, T_{h+1}, \dots, T_k \rangle$. The tree $T_1$ is a minimal one in $C$, and $V_1 \le V_2 < T_1$, which implies that $V_1$ and $V_2$ in the first and second position are both necessary and sufficient for $C^*$ to be a minimal rotation.

2. If the leftmost nontrivial tree of $C$ is the last one (i.e., if $h=k$), then $C^* = \langle \langle 1 \rangle, \dots, \langle 1 \rangle, V_1, V_2 \rangle$. This is always a minimal rotation since $\langle 1 \rangle \le V_1 \le V_2$.

3. Finally, suppose that $1 < h < k$, i.e., the leftmost nontrivial tree is neither the first nor the last. Then $C^*$ begins with a sequence of trivial trees of length $h-1$, $h$, or $h+1$, depending on whether $V_1$ and $V_2$ are trivial, followed by a nontrivial tree (resp., $V_1, V_2$, or $T_{h+1}$). We distinguish cases accordingly:

- If $V_1$ or both $V_1$ and $V_2$ are trivial (notice that $V_2$ trivial implies $V_1$ trivial, since $V_1 \le V_2$) the maximal prefix of trivial trees of $C^*$ has length strictly larger than $h-1$. If $C^*$ is not a minimal rotation, the actual minimal one would include a sequence of at least $h$ trivial trees as a prefix, to be found as a subsequence of $\langle T_{h+1}, \dots, T_{k-1} \rangle$. But this sequence would also exist in $C$ and would lead to a smaller minimal rotation, since $C$ begins with only $h-1$ trivial trees, contradicting the hypothesis that $C$ is a canonical form.

- If both $V_1$ and $V_2$ are nontrivial, then $C^*$ has a prefix of trivial trees of length $h-1$, followed by $V_1$. Suppose, by contradiction, that $C^*$ is not a minimal rotation. Then, a sequence of length $h$ of the form $P = \langle \langle 1 \rangle, \dots, \langle 1 \rangle, T \rangle$ exists, as a subsequence of $\langle T_{h+1}, \dots, T_k \rangle$, with the property that $P \le \langle T_1, \dots, T_{h-1}, V_1 \rangle$, i.e., $P$ is the prefix of the actual minimal rotation ($P$ and the prefix of $C^*$ could be elementwise identical, the difference only appearing later). This implies that $T \le V_1$. But $V_1 \le T_h$ and thus $P \le \langle T_1, \dots, T_h \rangle$; since $P$ is also a subsequence of $C$, this implies that $C$ is not a minimal rotation, once again a contradiction. $\square$
A key property given by Lemma 8 is the following.

**Proposition 9.** If $C, C^*$ are two components such that $C^* = \text{Parent}(C)$ then $|C^*| = |C| + 1$.

*Proof.* This is a consequence of the fact that the parent of a component is obtained by unmerging its leftmost nontrivial tree, and hence increases its number of trees by exactly one. □

Note that the $n$-vertex simple cycle is the unique $n$-vertex component maximising the length of its code, which is $n$. As a consequence, together with Proposition 9, it is easily proved that the Parent relation defines a rooted tree of height at most $n-1$, having as nodes the $n$-vertex components, with the $n$-vertex cycle as the root, and with an edge between two components $C$ and $C^*$ whenever $C^* = \text{Parent}(C)$. It now suffices to show how to generate all $C$ such that $C^* = \text{Parent}(C)$ for a given component $C^*$, in order to obtain an efficient generation algorithm by reverse search: all solutions will be obtained by a traversal of the solution tree initiated at the root being the $n$-vertex simple cycle, and duplicates are inherently avoided by the acyclicity of the solution tree; see e.g., [11] for a formalisation of the technique. In the following, we shall note $\text{Children}(C^*)$ the family of such components $C$. In order to list this family, we define the set of candidate merges of a given component, that we will show to contain every child.

**Definition 10.** Let $C^* = \langle T_1, \dots, T_k \rangle$ be a component with $k \ge 2$ and let

$$L_i(C^*) = \langle T_1, \dots, T_{i-1}, \text{merge}(T_i, T_{i+1}), T_{i+2}, \dots, T_k \rangle,$$

$$R_i(C^*) = \langle T_1, \dots, T_{i-1}, \text{merge}(T_{i+1}, T_i), T_{i+2}, \dots, T_k \rangle,$$

for $1 \le i < k$. Then, we denote the set of candidate merges of $C^*$ by

$$\text{Candidate-Merges}(C^*) = \bigcup_{1 \le i < k} \{L_i(C^*), R_i(C^*)\}.$$

The candidate merges of $C^*$ thus consist of all components obtained by merging each pair of adjacent trees in both directions. We note that the candidate merges of two distinct components could intersect, a point that will not be important in the following as we will only explore actual children among these candidates; for instance, both $\langle\langle 1 \rangle, \langle 1 \rangle, \langle 1 \rangle, \langle 3, 2, 1 \rangle\rangle$ and $\langle\langle 1 \rangle, \langle 2, 1 \rangle, \langle 1 \rangle, \langle 2, 1 \rangle\rangle$ would share an element among their candidate merges:

$$\langle\langle 1 \rangle, \langle 2, 1 \rangle, \langle 3, 2, 1 \rangle\rangle = \langle\langle 1 \rangle, \text{merge}(\langle\langle 1 \rangle, \langle 1 \rangle), \langle 3, 2, 1 \rangle\rangle \\ = \langle\langle 1 \rangle, \langle 2, 1 \rangle, \text{merge}(\langle\langle 1 \rangle, \langle 2, 1 \rangle\rangle).$$

**Lemma 11.** Given $C = \langle T_1, \dots, T_k \rangle \in \text{Candidate-Merges}(C^*)$ for some component $C^*$, it is possible to check in $O(n)$ time, where $n = |T_1| + \dots + |T_k|$, if $C$ is a valid isomorphism code for a component.

*Proof.* First of all, it is necessary to check if each $T_i$ is a valid isomorphism code for a tree. Since either $T_i = \langle 1 \rangle$, or it is obtained by merging two valid tree codes, it suffices to check that the codes of the subtrees of $T_i$ are in lexicographic order (see Example 5). The root of each subtree is the length of the corresponding subsequence, hence the subtrees can be extracted in linear time, and the resulting sequence of tree codes can also be checked for lexicographic order in linear time.

If the previous test is satisfied for all $T_i$, then it suffices to check if $C$ is a lexicographically minimal rotation (see Example 6). On “flat” sequences of integers, linear time algorithms such as Booth’s [3, 4] are well known. We can solve our problem (which involves sequences of sequences of integers) by reduction to this simpler case.
Let $P = \langle 0 \rangle \hat{T}_1 \hat{\langle 0 \rangle} \hat{T}_2 \hat{\langle} \dots \hat{\langle} 0 \rangle \hat{T}_k$ be the concatenation of the codes of the given trees, each prefixed with an extra $\langle 0 \rangle$; remark that 0 is strictly less than any integer appearing in the code of a tree. Let $P'$ be the minimal rotation of $P$; then $P'$ must begin with 0, since it is the minimum element of the sequence, and thus $P' = \langle 0 \rangle \hat{U}_1 \hat{\langle} 0 \rangle \hat{U}_2 \hat{\langle} \dots \hat{\langle} 0 \rangle \hat{U}_k$, where each $U_i$ is one of the original trees $T_j$ and $\langle U_1, \dots, U_k \rangle$ is a rotation of $\langle T_1, \dots, T_k \rangle$.

We claim that $\langle U_1, \dots, U_k \rangle$ is, more specifically, the minimal rotation of $\langle T_1, \dots, T_k \rangle$. Otherwise, by contradiction, there would exist another rotation $\langle V_1, \dots, V_k \rangle < \langle U_1, \dots, U_k \rangle$. Let $V_i$ and $U_i$ be the leftmost trees such that $V_i \neq U_i$. Then

$$ \underbrace{\langle 0 \rangle \hat{V}_1 \dots \hat{\langle} 0 \rangle}_{V} \hat{V}_i \dots \hat{\langle} 0 \rangle \hat{V}_k < \underbrace{\langle 0 \rangle \hat{U}_1 \dots \hat{\langle} 0 \rangle}_{U} \hat{U}_i \dots \hat{\langle} 0 \rangle \hat{U}_k = P' $$

since the two prefixes $U$ and $V$ are identical and $V_i < U_i$. But this contradicts the assumption that $P'$ is the minimal rotation of $P$.

Then $P$ is its own minimal rotation if and only if $\langle T_1, \dots, T_k \rangle$ is a minimal rotation. Since $P$ is a sequence of integers, the former property can be checked in linear time as mentioned above. $\square$

We are now ready to describe how to efficiently compute the children of a component.

**Lemma 12.** Let $C^*$ be a component. Then every $C \in \text{Children}(C^*)$ is a candidate merge of $C^*$. Furthermore, the set $\text{Children}(C^*)$ can be enumerated with delay $O(n^2)$ and linear space.

*Proof.* Let us first assume that $\text{Children}(C^*)$ is nonempty and let $C \in \text{Children}(C^*)$. Suppose that $C = \langle T_1, \dots, T_k \rangle$ for some integer $k \ge 1$. By definition, $C^* = \langle T_1, \dots, T_{h-1}, V_1, V_2, T_{h+1}, \dots, T_k \rangle$ for $T_h$ the leftmost nontrivial tree of $C$, and where $(U_1, U_2) = \text{unmerge}(T_h)$, $V_1 = \min\{U_1, U_2\}$, and $V_2 = \max\{U_1, U_2\}$. By definition of unmerge, we have that at least one of

$$ C = \langle T_1, \dots, T_{h-1}, \text{merge}(U_1, U_2), T_{h+1}, \dots, T_k \rangle $$

or

$$ C = \langle T_1, \dots, T_{h-1}, \text{merge}(U_2, U_1), T_{h+1}, \dots, T_k \rangle $$

holds depending on whether $U_1$ or $U_2$ is minimal. We conclude that at least one of $L_i(C^*)$ or $R_i(C^*)$ equals $C$ for $i = h$, hence that $C$ belongs to Candidate-Merges($C^*$). We note that $L_i(C^*) = R_i(C^*)$ whenever $U_1 = U_2$; even though this has no incidence in the proof as Candidate-Merges($C^*$) is defined as a set, one should take care of this situation by considering only one of $L_i(C^*)$ or $R_i(C^*)$ in the implementation.

Regarding the delay, note that the number of merge operations to perform in order to generate Candidate-Merges($C^*$) is $2(k-1)$, and that each merge takes $O(n)$ time. When performing a merge, we additionally check whether the obtained code is valid, which can be done in $O(n)$ time by Lemma 11, and whether it is a child of $C^*$, by performing one unmerge operation once again in $O(n)$ time. Since $k \le n$, we obtain a $O(n^2)$-delay algorithm generating all children of $C^*$ as desired. The space is linear since we only store a constant number of components.

Now if $\text{Children}(C^*)$ is empty, then the first part of the statement trivially holds, and the enumeration amounts to decide whether $\text{Children}(C^*) = \emptyset$ which is done by seeking for children as described above, in $O(n^2)$ time and linear space as well. $\square$

Let us mention for completeness that situations can occur where for a given component $C^*$ and integer $i$, each of the components $L_i(C^*)$ and $R_i(C^*)$ define a distinct child; for example, this happens for $C^* = \langle \langle 2, 1 \rangle, \langle 3, 2, 1 \rangle \rangle$, $n = 5$ and $i = 1$. On the other hand, root-to-leaf paths of length less than $n-1$ can occur in the solution tree, with components not having any children
despite being of length more than 1; an example is given by $C^* = \langle\langle 2, 1\rangle, \langle 2, 1\rangle, \langle 2, 1\rangle\rangle$ for $n = 6$. In situations of this kind, every valid candidate merge is actually the child of another component.

We note that the general framework of reverse search [1], equipped with the alternating output technique [26], yields a natural polynomial-time algorithm to produce the solution that comes after any given solution $C$ in the enumeration, provided that we are able to decide in polynomial time whether $C$ lies at even or odd depth in the solution tree, and to resume the children enumeration of an arbitrary node at child $i$ for any integer $i$. This comes from the fact that the next solution is at distance at most 3 from $C$ in the solution tree, hence that resuming the DFS on $C$, we obtain the next solution after at most 3 recursive calls and/or backtracks, in a time which is bounded by 3 times the delay needed to produce the next child. In our case, as the worst case delay to generate a child is the same as of generating all children, we do not even need to argue that we can resume the enumeration from the $i$-th child as long as we are interested in the asymptotic delay, though this can be done to speed up the implementation. As of deciding whether the current solution $C$ lies at even or odd depth in the solution tree, it can be done by checking the parity of $n - |C|$, that is, the number of merges performed starting from the root in order to obtain $C$. By the same arguments, it is easily seen that we can compute the solution that comes right before any given solution $C$ within the same time and space. In other words, our implementation of reverse search can be made so that it only uses the memory of the current node during the DFS of the solution tree, within the same delay. We conclude to the following.

**Theorem 13.** There is a $O(n^2)$-delay and linear space algorithm generating all connected $n$-vertex functional digraphs. Moreover, given any such functional digraph, we can generate its successor (resp., predecessor) in the enumeration in $O(n^2)$ time and using linear space. □

As an example, the generation of all components of 4 vertices is depicted in Fig. 2.

# 4 Generation of arbitrary functional digraphs

We can now exploit the algorithms of Theorem 13, and more specifically our ability to generate the successor of a given component, as a subroutine for the efficient generation of arbitrary (non necessarily connected) functional digraphs. In order to avoid generating multiple isomorphic digraphs, we first define an appropriate isomorphism code.

**Definition 14 (code of a functional digraph).** Let $G = (V, E)$ be an arbitrary functional digraph having $m$ connected components. Then, the isomorphism code of $G$ is the sequence

$$ \text{code } G = \langle \text{code } C_1, \dots, \text{code } C_m \rangle. $$

where, for each pair of consecutive components $C_i$ and $C_{i+1}$, either $C_i$ has less vertices than $C_{i+1}$, or both $C_i$ and $C_{i+1}$ have the same number of vertices and their codes are in increasing order of generation by the algorithm of Theorem 13.

An example code for a disconnected functional digraph is depicted in Fig. 1.

As usual, we identify a functional digraph $G$ with its own code and refer to it as a canonical form for $G$.

Notice how the isomorphism code for a functional digraph resembles a PQ-tree, a data structure representing permutations of a given set of elements which, incidentally, is used to efficiently check isomorphic graphs of certain classes [5]. However, for our application we need to represent the equivalence of all permutations of the components $C_1, \dots, C_m$, as well as the equivalence of all rotations of the trees $T_1, \dots, T_k$ of a component $C_i$, and this latter condition is not represented directly in a PQ-tree.
<figure><img src="image_3.png" /><figcaption>**Fig. 2.** Reverse search tree for the generation of components of 4 vertices, represented both in graphical form (top) and as isomorphism codes (bottom); the order of generation by the algorithm of Theorem 13 is displayed on the top left. Note that the actual ordering of children of each node depends on the (arbitrary) order we chose for the generation of the candidate merges; in the picture we first compute $L_i(C^*)$ for $i$ from 1 to $k-1$, then the same for the $R_i(C^*)$: this is the ordering that has been adopted in our implementation and that will be considered in the remaining of the paper.</figcaption></figure>

**Definition 15.** We denote by $C_n$ the set of components of $n$ vertices, and by $C_n^m$ the set of functional digraphs having $m$ components of $n$ vertices each.

In terms of isomorphism codes, the elements of $C_n^m$ have the form $\langle C_1, \dots, C_m \rangle$ with all $C_i \in C_n$ and $C_1 \le \dots \le C_m$ in generation order. Remark that $C_n^0 = \{\langle \rangle\}$ as a base case, i.e., it only contains the empty functional digraph, and that $C_n^m$ is not simply the Cartesian product of $m$ copies of $C_n$, due to the ordering requirement, but rather the set of multisets of $m$ elements of $C_n$. This set can be generated efficiently as follows.

**Lemma 16.** $C_n^m$ can be generated with delay $O(mn^2)$ and in space $O(mn)$.

*Proof.* We generate $C_n^m$ starting from the functional digraph

$$G_0 = \underbrace{\langle\langle 1 \rangle, \dots, \langle 1 \rangle\rangle}_{n \text{ times}}, \dots, \underbrace{\langle\langle 1 \rangle, \dots, \langle 1 \rangle\rangle}_{n \text{ times}} \\ \overline{\hspace{2.5cm}} \\ \underbrace{\hspace{1cm}}_{m \text{ times}}$$
that is, the functional digraph consisting of $m$ cycles of length $n$, which are the first elements (the roots of the solution trees) in the generation order of components.

Now let $G = \langle C_1, \dots, C_m \rangle$ be an arbitrary element of $C_n^m$. The successor $G'$ of $G$ is computed by taking the rightmost element $C_i$ that possesses a successor component $C'_i$, if any, and letting

$$G' = \langle C_1, \dots, C_{i-1}, \underbrace{C'_i, \dots, C'_i}_{m-i+1 \text{ times}} \rangle$$

that is, by replacing $C_i$ and every other component on its right by $C'_i$.

The resulting functional digraph code $G'$ is still in nondecreasing order of generation of components, thus a valid isomorphism code, and $G' > G$ in the induced lexicographical order, which avoids generating the same code multiple times. Furthermore, any valid functional digraph code can be constructed right-to-left from $G_0$ by applying the successor operation enough times to the cycle of length $n$. This is thus an exhaustive generation without duplicates. Intuitively, this amounts to generating all nondecreasing words of length $m$ on the alphabet $C_n$, where nondecreasing is to be interpreted with respect to the order of generation of $C_n$.

Computing $G'$ requires applying the successor operation to at most $m$ components, and replicating at most $m$ times the successor component $C'_i$ thus obtained, which can be carried out in $O(mn^2)$ time. The space bound is due to the fact that we only store one functional digraph. $\square$

Recall that an integer partition of a natural number $n$ is an (unordered) multiset of positive integers having sum $n$. Partitions can be alternatively represented as a vector of $n$ natural numbers (including zero) representing the multiplicities of each term of the sum.

**Definition 17.** A partition of the natural number $n$ is an $n$-tuple $p = (p_1, \dots, p_n)$ of natural numbers such that $\sum_{i=1}^n p_i = n$. We denote by $\mathcal{P}_n$ the set of partitions of $n$.

**Definition 18.** We denote by $\mathcal{G}_n$ the whole set of functional digraphs over $n$ vertices. Furthermore, for each partition $p = (p_1, \dots, p_n) \in \mathcal{P}_n$, we denote by $\mathcal{G}_p$ the set of functional digraphs having exactly $p_i$ components of $i$ vertices for all $1 \le p \le n$.

By definition we have $\mathcal{G}_n = \bigcup_{p \in \mathcal{P}_n} \mathcal{G}_p$, and $\mathcal{G}_p$ and $\mathcal{G}_q$ are disjoint whenever $p$ and $q$ are distinct partitions of $n$; in other words, $\{\mathcal{G}_p : p \in \mathcal{P}_n\}$ is a (set) partition of $\mathcal{G}_n$. Since integer partitions can be generated efficiently,<fnref n="2" /> we only need to show that each $\mathcal{G}_p$ can be generated efficiently. By “grouping together” all components of the same size, we can analyse an arbitrary functional digraph $G \in \mathcal{G}_p$ as the disjoint union of (possibly empty) digraphs $G_i \in C_i^{p_i}$ for $1 \le i \le n$. Since the components of $G_i$ are smaller than those of $G_{i+1}$ for all $1 \le i < n$, we have $G = G_1 \frown \dots \frown G_n$ in terms of isomorphism codes.

**Theorem 19.** The set of functional digraphs over $n$ vertices up to isomorphism can be generated with delay $O(n^2)$ and using linear space.

*Proof.* Since integer partitions can be generated with linear delay [17], it suffices to show that each set $\mathcal{G}_p$ can be generated with delay $O(n^2)$ in order to prove the theorem.

The set $\mathcal{G}_p$ can be generated by enumerating the $n$-tuples $\langle G_1, \dots, G_n \rangle$ of the Cartesian product $\prod_{i=1}^n C_i^{p_i}$ and then outputting $G_1 \frown \dots \frown G_n$. Generating the next element of a Cartesian product may require, in the worst case, trying to compute the successor of each coordinate $C_i^{p_i}$, and resetting that coordinate to the first element of $C_i^{p_i}$ (that is, the functional digraph consisting in $p_i$ cycles of length $i$) if there is no successor. By Lemma 16, each $C_i^{p_i}$ can be generated with delay $O(p_i i^2)$, which means that the generation delay for $\mathcal{G}_p$ is asymptotically proportional

<footnote n="2">² With a suitable representation of partitions, an amortised constant delay is even possible [17].</footnote>
<figure><img src="image_4.png" /><figcaption>**Fig. 3.** Generation (top to bottom, left to right) of the set of functional digraphs $\mathcal{G}_p$ over 8 vertices with partition $p = (0, 1, 2, 0, 0, 0, 0)$, corresponding to components of 2, 3, 3 vertices respectively. Each component is labelled by its generation order according to the algorithm of Theorem 13. As an example, the functional digraph in grey (the 7th generated one in $\mathcal{G}_p$) has isomorphism code $\langle\langle(1), (1)\rangle\rangle, \langle\langle(3, 2, 1)\rangle\rangle, \langle\langle(1), (2, 1)\rangle\rangle$.</figcaption></figure>

to $\sum_{i=1}^n p_i i^2 \le n \sum_{i=1}^n p_i i$. By recalling that $\sum_{i=1}^n p_i i = n$ (Definition 17) we obtain the required quadratic upper bound on the delay. Since we never store more than one functional digraph at a time, and since the successor of each component can be computed in linear space (Theorem 13), the overall space requirement for this algorithm is also linear. $\square$

Fig. 3 shows an example generation of disconnected functional digraphs with a fixed partition of vertices into components, and highlights the similarity with the increment operation on a mixed radix integer (with the extra constraint that components of the same size must be in nondecreasing order of generation).
# 5 Conclusions

We have described the first polynomial-delay generation algorithm for the class of functional digraphs, both connected and arbitrary, which proves that these classes of graphs can be generated with an $O(n^2)$ delay and linear space.<fnref n="3" />

It is, of course, an open problem to establish if functional digraphs can be generated with a smaller delay. That would require us to somehow avoid testing $O(n)$ possible merges in order to construct the next candidate digraph, or to avoid spending linear time in order to check for valid isomorphism codes. Otherwise, is an amortised constant time delay possible?

Another interesting line of research is to find variations of the tree-merging approach suitable for the efficient generation either of restricted classes of functional digraphs (for instance, with cycles of given lengths or trees of given heights, which is sometimes useful in applications related to the decomposition of dynamical systems [10]), or of more general classes of graphs, without the uniform outdegree 1 constraint, possibly by means of a “functional digraph decomposition”.

**Acknowledgements** Ekaterina Timofeeva was funded by the undergraduate internship program “Incubateurs de jeunes scientifiques” of the Faculty of Sciences at Aix-Marseille Université and by the Agence National de la Recherche project ANR-18-CE40-0002 FANs.

We would like to thank Kellogg S. Booth, Jerome Kelleher, Brendan D. McKay, and Kévin Perrot for some useful discussions and for providing some bibliographic references. We are also grateful to an anonymous reviewer for suggesting the explicit use of the supergraph method, which allowed us to simplify the proofs and to identify a bottleneck in a previous version of our component generation algorithm, thus improving its efficiency.

# References

1. Avis, D., Fukuda, K.: Reverse search for enumeration. Discrete Applied Mathematics 65(1-3), 21–46 (1996), https://doi.org/10.1016/0166-218X(95)00026-N

2. Beyer, T., Mitchell Hedetniemi, S.: Constant time generation of rooted trees. SIAM Journal on Computing 9(4), 706–712 (1980), https://doi.org/10.1137/0209055

3. Booth, K.S.: Lexicographically least circular substrings. Information Processing Letters 10(4–5), 240–242 (1980), https://doi.org/10.1016/0020-0190(80)90149-0

4. Booth, K.S.: Lexicographically least circular substrings. https://www.cs.ubc.ca/~ksbooth/PUB/LCS.shtml (2019), accessed: 2022-10-31, archived at http://web.archive.org/web/20220725150720/https://www.cs.ubc.ca/~ksbooth/PUB/LCS.shtml

5. Booth, K.S., Lueker, G.S.: Testing for the consecutive ones property, interval graphs, and graph planarity using PQ-tree algorithms. Journal of Computer and System Sciences 13(3), 335–379 (1976), https://doi.org/10.1016/S0022-0000(76)80045-1

6. Busacker, R.G., Saaty, T.L.: Finite Graphs and Networks: An Introduction with Applications. McGraw-Hill (1965)

7. Conte, A., Grossi, R., Marino, A., Versari, L.: Sublinear-space bounded-delay enumeration for massive network analytics: Maximal cliques. In: 43rd International Colloquium on Automata, Languages, and Programming (ICALP 2016). vol. 148, pp. 1–148 (2016), https://doi.org/10.4230/LIPIcs.ICALP.2016.148

8. Datta, S., Limaye, N., Nimbhorkar, P., Thierauf, T., Wagner, F.: Planar graph isomorphism is in log-space. In: 2009 24th Annual IEEE Conference on Computational Complexity. pp. 203–214 (2009), https://doi.org/10.1109/CCC.2009.16

<footnote n="3">³ A proof-of-concept implementation of the algorithms described in this paper, the **funkdigen** command-line tool, is also available [9].</footnote>
9. Defrain, O., Porreca, A.E., Timofeeva, E.: **funkdigen**: A (proof of concept) generator of functional digraphs up to isomorphism. https://github.com/aeporreca/funkdigen (2024)

10. Dennunzio, A., Dorigatti, V., Formenti, E., Manzoni, L., Porreca, A.E.: Polynomial equations over finite, discrete-time dynamical systems. In: Mauri, G., El Yacoubi, S., Dennunzio, A., Nishinari, K., Manzoni, L. (eds.) Cellular Automata, 13th International Conference on Cellular Automata for Research and Industry, ACRI 2018. Lecture Notes in Computer Science, vol. 11115, pp. 298–306. Springer (2018), https://doi.org/10.1007/978-3-319-99813-8_27

11. Elbassioni, K.: A polynomial delay algorithm for generating connected induced subgraphs of a given cardinality. Journal of Graph Algorithms and Applications 19(1), 273–280 (2015), https://doi.org/10.7155/jgaa.00357

12. Fomin, F.V., Kaski, P.: Exact exponential algorithms. Communications of the ACM 56(3), 80–88 (2013)

13. Goldberg, L.A.: Efficient algorithms for listing unlabeled graphs. Journal of Algorithms 13(1), 128–143 (1992), https://doi.org/10.1016/0196-6774(92)90009-2

14. Hagerup, T.: Sorting and searching on the word RAM. In: Morvan, M., Meinel, C., Krob, D. (eds.) STACS 98. pp. 366–398. Springer Berlin Heidelberg, Berlin, Heidelberg (1998), https://doi.org/10.1007/BFb0028575

15. Hammack, R., Imrich, W., Klavžar, S.: Handbook of Product Graphs. Discrete Mathematics and Its Applications, CRC Press, second edn. (2011), https://doi.org/10.1201/b10959

16. Johnson, D.S., Yannakakis, M., Papadimitriou, C.H.: On generating all maximal independent sets. Information Processing Letters 27(3), 119–123 (1988), https://doi.org/10.1016/0020-0190(88)90065-8

17. Kelleher, J., O'Sullivan, B.: Generating all partitions: A comparison of two encodings. arXiv:0909.2331v2 [cs.DS] (2009), https://doi.org/10.48550/arXiv:0909.2331

18. Khachiyan, L., Boros, E., Elbassioni, K., Gurvich, V.: On enumerating minimal dicuts and strongly connected subgraphs. Algorithmica 50(1), 159–172 (2008), https://doi.org/10.1007/s00453-007-9074-x

19. McKay, B.D.: Isomorph-free exhaustive generation. Journal of Algorithms 26(2), 306–324 (1998), https://doi.org/10.1006/jagm.1997.0898

20. McKay, B.D., Piperno, A.: Practical graph isomorphism, II. Journal of Symbolic Computation 60, 94–112 (2014), https://doi.org/10.1016/j.jsc.2013.09.003

21. Meir, A., Moon, J.W.: On random mapping patterns. Combinatorica 4, 61–70 (1984), https://doi.org/10.1007/BF02579158

22. OEIS Foundation Inc.: Number of connected functions (or mapping patterns) on *n* unlabeled points, or number of rings and branches with *n* edges. Entry A002861 in The On-Line Encyclopedia of Integer Sequences (2024), https://oeis.org/A002861

23. OEIS Foundation Inc.: Number of mappings (or mapping patterns) from *n* points to themselves; number of endofunctions. Entry A001372 in The On-Line Encyclopedia of Integer Sequences (2024), https://oeis.org/A001372

24. Strozecki, Y.: Enumeration complexity. Bulletin of EATCS 129 (2019), http://bulletin.eatcs.org/index.php/beatcs/article/view/596

25. Uno, T.: A fast algorithm for enumerating non-bipartite maximal matchings. Journal of National Institute of Informatics 3, 89–97 (2001)

26. Uno, T.: Two general methods to reduce delay and change of enumeration algorithms. Tech. Rep. NII-2003-004E, National Institute of Informatics (2003), https://miar.ub.edu/issn/1346-5597

27. Uno, T.: An efficient algorithm for solving pseudo clique enumeration problem. Algorithmica 56, 3–16 (2010), https://doi.org/10.1007/s00453-008-9238-3

28. Valiente, G.: Algorithms on Trees and Graphs. Texts in Computer Science, Springer, 2nd edn. (2021), https://doi.org/10.1007/978-3-030-81885-2

29. Wasa, K., Kaneta, Y., Uno, T., Arimura, H.: Constant time enumeration of bounded-size subtrees in trees and its application. In: Computing and Combinatorics: 18th Annual International Conference, COCOON 2012, Sydney, Australia, August 20–22, 2012. Proceedings 18. pp. 347–359. Springer (2012), https://doi.org/10.1007/978-3-642-32241-9_30