Generation of Orchard and Tree-Child Networks

Phylogenetic networks are an extension of phylogenetic trees that allow for the representation of reticulate evolution events. One of the classes of networks that has gained the attention of the scientific community over the last years is the class of orchard networks, that generalizes tree-child networks, one of the most studied classes of networks. In this paper we focus on the combinatorial and algorithmic problem of the generation of binary orchard networks, and also of binary tree-child networks. To this end, we use that these networks are defined as those that can be recovered by reversing a certain reduction process. Then, we show how to choose a “minimum” reduction process among all that can be applied to a network, and hence we get a unique representation of the network that, in fact, can be given in terms of sequences of pairs of integers, whose length is related to the number of leaves and reticulations of the network. Therefore, the generation of networks is reduced to the generation of such sequences of pairs. Our main result is a recursive method for the efficient generation of all minimum sequences, and hence of all orchard (or tree-child) networks with a given number of leaves and reticulations. An implementation in C of the algorithms described in this paper, along with some computational experiments, can be downloaded from the public repository https://github.com/gerardet46/OrchardGenerator. Using this implementation, we have computed the number of binary orchard networks with at most 6 leaves and 8 reticulations.


Introduction
During decades, phylogenetic trees have been the model used to represent the branching pattern for the evolution of a set of Operational Taxonomic Units (OTUs for short). From the 1980s onward, it became evident that phylogenetic networks were a more accurate framework, with the potential to cover more complex evolutionary scenarios such as hybridizations, recombinations, or lateral gene transfers.
In the broadest sense, phylogenetic networks are directed acyclic graphs whose leaves are labelled by the organisms under study. This general definition, while allowing a wide range of biological processes to be considered, lacks mathematical tractability. For this reason, some other constraints must be considered, resulting in a wide variety of classes of phylogenetic networks (see [KPKW22] for a recent review, or [Ste16,Chapter 10]). In this work we focus on the class of orchard networks [ESS19] (also called cherry-picking networks [JM21]) and tree-child networks [CRV09], a subclass of the first and one of the most explored classes of networks.
Orchard networks are networks that can be reduced to a trivial network by iteratively identifying and reducing certain substructures (namely, cherries and reticulated cherries) involving two leaves. Orchard networks are one of those classes of networks with biological significance (according to [KPKW22]) because they can be viewed as a backbone tree with additional "horizontal" arcs (see [vIJJM22] for more details).
One of the relevant problems in the study of phylogenetic networks is that of their sequential generation; that is, obtaining a method to generate them in an efficient and unique way. Generation of phylogenetic networks is useful, for example, for testing the performance of methods in phylogenetics and for testing hypotheses about the evolutionary relationships among organisms by the comparison of different network topologies.
Up to our knowledge, there exists no prior work neither on the systematic generation of orchard networks nor on its counting. Notice, however, that the identification of orchard networks as trees with extra arcs used in [vIJJM22], obviously results in an algorithm to generate them, but not uniquely, and moreover there is no prior control on the probability distribution of the generated networks. The situation for tree-child networks is slightly better, since there are previous works on the enumeration [FGM20,PB21] and generation [CPS19,CZ20] of this kind of networks, but much less efficiently than the method given here (see Section 8).
In this paper, we shall focus on the problem of the effective and injective generation of orchard and tree-child phylogenetic networks; that is, no pair of generated networks will be the same (technically, isomorphic), and we can promptly get many networks with the number of leaves and reticulations that we want. Our method of generation is based on the construction of sequences of pairs of integers that encode orchard (and, in particular, tree-child) networks as introduced in [JM21]. However, there are different sequences that generate the same network, so that we choose among them a minimum one that uniquely represents it. Hence, our strategy to generate orchard (and tree-child) networks is based on the generation of those minimum sequences.
The paper is organized as follows. In Section 2 we give basic definitions used throughout the manuscript. In Section 3 we define orchard networks and how they can be reduced by means of reducible sequences. In Section 4 we show that we can choose a minimum (in a sense to be defined) reducible sequence in order to uniquely identify an orchard network up to isomorphism. Section 5 shows how the reduction of a pair can be reverted by means of augmentations, and in Section 6 it is used to describe how to recover an orchard network by reversing the whole reduction process, and how this process, together with the unicity of the minimum reducible sequence, allows us to generate orchard networks injectively. In Section 7 we adapt our methods to generate tree-child networks, which constitute a relevant subclass of orchard networks. In Section 8 we present the implementation we have made of the methods contained in this paper and exhibit some computational experiments we have performed, including the computation of the number of orchard networks with up to 6 leaves and 8 reticulations. Finally, Section 9 contains the conclusions of the manuscript and some possible directions of future work.

Preliminaries
Throughout the paper, for any positive integer n, we denote by [n] the set {1, . . . , n}. The graphs N = (V, A) we shall work with are directed and acyclic. Given two nodes u, v ∈ V , if there is an arc with tail u and head v (or from u to v), we denote it as uv. In that case, u is a parent of v and v is a child of u.
Given a node u ∈ V , indeg u (resp. outdeg u) denotes the number of arcs whose head (resp. tail) is u. We say that u is elementary if indeg u = outdeg u = 1, and its simplification consists in removing it (together with its incident arcs) and connecting its single parent to its single child.
Given a set X of taxa, a (rooted binary) phylogenetic network, or simply a network, on X, is a directed acyclic graph (V, A) without parallel arcs such that any node u ∈ V is either: (1) a root, with indeg u = 0, outdeg u = 1 (and there can only be one root), or (2) a leaf, with indeg u = 1, outdeg u = 0, or (3) a tree node, with indeg u = 1, outdeg u = 2, or (4) a reticulation, with indeg u = 2, outdeg u = 1, together with a fixed bijection between X and the set of leaves.
We shall hereafter identify the set X of taxa and the set of leaves, and we shall always assume that X is formed by positive integers, and hence X ⊆ [n] for some n.
Two networks N and N ′ are isomorphic if there exists a bijection ϕ between the respective set of nodes that reflects and preserves the arcs (that is, uv is an arc in N if, and only if, ϕ(u)ϕ(v) is an arc in N ′ ), which is the identity on the leaves (that is, if l is a leaf, ϕ(l) = l). Hereafter, we shall simply say that two networks are equal if they are isomorphic.
In case that X = {l}, for some l ∈ [n], we define the trivial network on {l}, and denote it by I l , as the network that has two nodes, the root and the leaf l, connected by an arc.

Orchard networks
Let N = (V, A) be a network on X ⊆ [n] and let (i, j) ∈ X × X with i ̸ = j. Also, denote by p i , p j the parents of the leaves i and j in N , respectively. We call (i, j) a cherry if p i = p j , and we call it a reticulated-cherry if p i is a reticulation, p j is a tree node, and p j is one of the parents of p i . In either case (i, j) is a cherry or a reticulated-cherry, we say that (i, j) is a reducible pair in N . In order to identify which kind of reducible pair is (i, j) in N , we will define its character as χ N (i, j) = C if it is a cherry and χ N (i, j) = R if it is a reticulated-cherry. Notice that the conditions of being a cherry and a reticulated-cherry are clearly incompatible, which implies that χ N is well defined. If the network is clear from the context, we will simply write χ(i, j). To ease notations, if a pair (i, j) has character χ = χ N (i, j), we shall write the annotated pair as (i, j) χ .
Given a network N , we shall denote by RP(N ) the set of reducible pairs of N , by χ N the mapping RP(N ) → {C, R} that gives the character of the reducible pairs, and by ARP(N ) the set of annotated reducible pairs of N .
If (i, j) ∈ RP(N ), the reduction of (i, j) in N , denoted by N (i,j) , is the result of: • If χ(i, j) = C, then remove the leaf i (and its incoming arc) and simplify p i , which is now an elementary node. • If χ(i, j) = R, then remove the arc p j p i and then simplify p i and p j , which are now elementary nodes. Given a sequence of pairs of integers S = (s 1 , . . . , s k ) which, for brevity, we will write as S = s 1 · · · s k , with s t = (i t , j t ) and i t , j t ∈ [n], of length k ≥ 1, we say that S is reducible in N if: In such a case, we shall define the reduction of N with respect to S as (. . . (N s1 ) s2 . . . ) s k and it will be denoted by N S .
Moreover, we say that S is complete if N S = I l for some l ∈ X and, in case one such complete sequence exists, we call N an orchard network [ESS19,JM21]. We shall also consider the trivial networks I l as orchard networks, corresponding to the case when the sequence S is empty. Notice that trivial networks are the only ones that have a single leaf.
The fundamental result that allows one to classify orchard networks using complete reducible sequences is the following, which is adapted from [JM21, Corollary 1].

Minimum reducible sequences
As observed before, there may exist different complete reducible sequences for a given orchard network. Our goal in this section is to define a unique representative among all sequences giving the same network.
Let (i, j), (i ′ , j ′ ) be two pairs of different integers. We say that (i, j) Given two sequences of pairs of integers of the same length, S = s 1 · · · s k and S ′ = s ′ 1 · · · s ′ k , we say that S < S ′ if, for some l ∈ [k] we have that s 1 = s ′ 1 , . . . , s l−1 = s ′ l−1 and s l < s ′ l . It is easy to check that the relations just defined are total orders (on pairs and sequences of pairs of fixed length, respectively).
Notice that all the complete reducible sequences of a given orchard network have the same length, since this length is equal to |X| + r − 1, where r is the number of reticulations of N . We show that the two minimums just defined are related. Proof. Let s be the first pair in S = MCRS(N ) and s ′ = MRP(N ). Obviously, s ∈ RP(N ) and, from the definition of MRP(N ), it follows that s ′ ≤ s. Due to [ESS19, Proposition 4.1], the sequence with the single pair s ′ can be extended to give a complete sequence S ′ ∈ CRS(N ). Since the minimum complete sequence is S, we have that S ≤ S ′ , and hence s ≤ s ′ . Therefore, s = s ′ and the result is proved. □ We define S(X, r) as the set whose elements are the sequences MCRS(N ) for every orchard network N over X with exactly r reticulations.
Theorem 3. There is a bijection between S(X, r) and the set of orchard networks over X with exactly r reticulations.
Proof. The result follows from Theorem 1 and the unicity of MCRS(N ). □

Augmentation of networks
In this section, we present an augmentation construction, which is the inverse of the reduction defined before, and show how we can determine the ARP of the obtained network from that of the original network.
Throughout this section we consider that N is a network on X ⊆ [n] and (i, j) ∈ [n] × [n] is a pair of integers with i ̸ = j and j ∈ X.
We define the augmentation of (i, j) in N , denoted by (i,j) N , as the result of: • if i / ∈ X, create a new (leaf) node i, subdivide the arc ending in j creating an elementary node p j , and add the arc p j i.
• if i ∈ X, subdivide both arcs ending in i and j creating elementary nodes p i and p j , and add an arc p j p i . Similarly as in the reduction case, we shall define the augmentation of an orchard network N (which could be a trivial network I l ) with respect to a sequence S = s 1 · · · s k as s1 ( · · · ( s k−1 ( s k N ))) and it will be denoted by S N .
Notice that (i, j) is a cherry in (i,j) N , in symbols χ(i, j) = C, when i / ∈ X, and (i, j) is a reticulated cherry in (i,j) N , in symbols χ(i, j) = R, when i ∈ X. Then, the augmenting operation is the inverse of the reduction operation, in the sense that (i,j) N (i,j) ∼ = N . This leads to present an alternative definition for orchard networks as those can be obtained by an augmentation of a trivial network I l . Note also that if N = S I l (for some l ∈ [n]), then necessarily the last pair in S must be (i, l) (for some i ∈ [n]). Hence, l is determined by S and can be omitted from I l . Therefore, from now on we will simply write N = S I.
We describe now how one can compute ARP( (i,j) N ) from ARP(N ). That is, we show how the cherries and reticulated cherries of N ′ = (i,j) N can be found from the knowledge of those of N . Some remarks are due.
(1) It is clear that the augmentation is a local operation; more precisely, a cherry (resp. reticulated cherry) in N that is disjoint from (i, j) keeps being a cherry (resp. reticulated cherry) in N ′ .
(2) One only needs to check if the augmentation operation makes that some reducible pair disappears or changes its character (passes from cherry to reticulated cherry or viceversa), and if some new reducible pair appears. As for this last case, notice that the only reducible pair that can appear is (i, j).
Hence, we shall take any pair (x, y) and decide if it is a reducible pair in N ′ (that is, whether or not (x, y) ∈ RP(N ′ )) and, in such a case, if either (x, y) C ∈ ARP(N ′ ) or (x, y) R ∈ ARP(N ′ ) (equivalently, the value of χ N ′ (x, y)): Note that from now on we can restrict ourselves to pairs (x, y) in RP(N ), since no other new pairs can appear.
• Case {x, y} ∩ {i, j} = ∅: From the local character of augmentation, (x, y) ∈ RP(N ′ ) and Fig. 2): If (x, y) is a cherry in N , say that p is their common parent, then in N ′ the arc pi = px is split introducing a node which will be a reticulation; hence, (x, y) is a reticulated cherry in N ′ . If (x, y) is a reticulated cherry in N , then the parent of y will no longer be a grandparent of x = i in N ′ (since the arc leading to i is split in two). In brief, In the left, (x, y) is a cherry in N . In the right, (x, y) is a reticulated cherry in N . The dashed arrow indicates the added arc to transform N into N ′ by the augmentation operation. Arcs whose tips are not explicitly drawn go from top to bottom.
• Case x ̸ = j, y = i (see Fig. 3): The same argument as in the previous case gives that if (x, y) is a cherry in N , then (y, x) (notice the transposition) is a reticulated cherry in N ′ (and hence (x, y) / ∈ RP(N ′ )). Note that, if (x, y) is a cherry of N , so is (y, x), and hence the fact that (y, x) R belongs to ARP(N ′ ) will be covered by the application to the previous case applied to (y, x). As before, if (x, y) is a reticulated cherry of N , then it is no longer reducible in N ′ . Therefore, in either case we have that (x, y) / ∈ RP(N ′ ). • Case x ̸ = i, y = j (see Fig. 4): Let py = pj be the arc leading to y = j; this arc is split in N ′ by introducing a node that will be a tree node; this implies that (x, y) will no longer be reducible in N ′ and hence (x, y) / ∈ RP(N ′ ). • Case x = j, y ̸ = i: The same argument as in the previous case, taking now the arc leading to x = j implies that (x, y) / ∈ RP(N ′ ). We can summarize these computations in the following result. (1) (x, y) ∈ RP(N ), and {x, y} ∩ {i, j} = ∅, (2) (x, y) ∈ RP(N ), i = x, j ̸ = y, and χ(x, y) = C, (x, y) = (j, i), and i / ∈ X. Annotate these pairs with the character χ ′ (x, y) given, in each case, by: (1) χ ′ (x, y) = χ(x, y),

Denote by
(i,j) ARP(N ) the set of annotated pairs that is obtained by application of the procedure above.
As a result of the last theorem, in order to compute the set of annotated reducible pairs of the augmentation of a network, it is enough to traverse the annotated reducible pairs of the network.
The next proposition shows that, given S = MCRS(N ), it can be checked if (i, j)S = MCRS (i,j) N using only the information in ARP(N ), without the need for knowing N itself. Proof. It is a direct consequence of Proposition 2. □

Augmentation sequences and generation of orchard networks
The goal of this section is to present an algorithm to generate the set of orchard networks over a set [n] with exactly r reticulations. Thanks to Theorem 3, this is equivalent to compute S([n], r). Our strategy is to build these sequences starting with sequences of length one and, step by step, finding all possible pairs that can be prepended in order to get the sequences in S([n], r).
Let S = s 1 · · · s k be a sequence of pairs of integers, say s t = (i t , j t ) where t = 1, . . . , k. We call the support of S the set Supp(S) = {i 1 , j 1 , . . . , i k , j k }. For every t = 1, . . . , k, we denote by S t the suffix s t · · · s k . We say that a sequence S as above is an augmentation sequence if for each t = 1, . . . , k − 1, we have that j t ∈ Supp(S t+1 ) and i t ̸ = j t . We remark that, although the formulation is not exactly the same, what we call augmentation sequences corresponds to cherry-picking sequences in [JM21, Definition 6].
It is clear that given an augmentation sequence S, we can consider the orchard network N = S I, and also that S will be a complete reducible sequence for N . From now on, all properties that can be defined for networks (taxa, number of reticulations, . . . ) will be defined for augmentation sequences by applying them on the network that the sequence generates. For instance, we can define MRP(S) := MRP( S I) and MCRS(S) := MCRS( S I). Note also that some of the properties can be found without having to construct the network itself. For instance, the number of reticulations of S = (i 1 , j 1 ) · · · (i k , j k ), which by definition is the number of reticulations of S I can be found counting for how many indices t = 1, . . . , k − 1 we have that i t ∈ Supp(S t+1 ). Also, using Theorem 4 recursively, we can compute ARP(S).
We shall say that an augmentation sequence S is a minimum augmentation sequence if S = MCRS(N ) for some network N . It is clear that it happens exactly when S = MCRS(S), and recall that MCRS(S) = MCRS( S I). This provides an alternative definition for S(X, r) as the set of augmentation sequences that are stable under application of MCRS, with support X and with r reticulations.
These notations allow us to translate many properties that have been stated in terms of orchard networks into the language of sequences. For instance, Propositions 2 and 5 can be rewritten as follows.
Proposition 6. Let S be a minimum augmentation sequence. Then: (1) The first pair in S is MRP(S).
(2) Given a pair (i, j) with j ∈ Supp(S), (i, j)S is a minimum augmentation sequence if, and only if, (i, j) = MRP((i, j)S).
We give now two results that characterize the suffixes of minimum augmentation sequences and, in particular, show that the last pair in such a sequence has a well determined form.
Proof. It is clear that S t ∈ CRS(S t ). If there existed some S ′ t ∈ CRS(S t ) with S ′ t < S t , then the concatenation S ′ = s 1 · · · s t−1 S ′ t would be strictly smaller than S and also S ′ ∈ CRS(S), against the minimality of S. □ Proposition 8. Let (i, j) be the last pair in a minimum augmentation sequence S ∈ S([n], r). Then, j = n.
Proof. Write MCRS(N ) as S = (i 1 , j 1 ) · · · (i k , j k ), where (i k , j k ) = (i, j) and assume that j ̸ = n. Suppose first that i = n and j < n. From Lemma 7, (n, j) is a minimum complete reduction sequence (of the cherry (n,j) I), but in this case (j, n) is also a complete reduction sequence, and (j, n) < (n, j), leading to a contradiction. Now, we can assume that i, j < n. Let t < k be such that (i t , j t ) is the last pair where one of its entries is n (it exists because S ∈ S([n], r), and hence n ∈ Supp(S)). Now, S t+1 = (i t+1 , j t+1 ) · · · (i k , j k ) is a minimum augmentation sequence thanks to Lemma 7. Since n does not belong to Supp(S t+1 ), but does belong to the support of S t = (i t , j t )S t+1 , we have that i t = n and j t < n. Moreover, (n, j t ) is a cherry in N ′ = St I. Thanks again to Lemma 7, S t = (n, j t )S t+1 is a minimum augmentation sequence and, thanks to Proposition 2, (n, j t ) = MRP(N ′ ). However, since (n, j t ) is a cherry of N ′ , then (j t , n) is also a cherry, and (j t , n) < (n, j t ), leading to a contradiction. A direct consequence of Proposition 8 is the following result, that states that the computation of S([n], r) is reduced to the computation of the subsets S m ([n], r).
Proposition 9. S([n], r) = n−1 m=1 S m ([n], r). We know, from Proposition 8, the form of the last pair in a minimum augmentation sequence. It is also clear that any such pair (m, n) is a minimum augmentation sequence. Our next result shows how minimum augmentation sequences can be extended by prepending pairs of integers in order to generate other minimum augmentation sequences.
Theorem 10. Let S ′ = (i, j)S be an augmentation sequence. Then, S ′ is a minimum augmentation sequence if, and only if, S is a minimum augmentation sequence and (i, j) = MRP((i, j)S). In such a case, say that S ∈ S(X, r) and S ′ ∈ S(X ′ , r ′ ). If i ∈ Supp(X), then r ′ = r + 1 and X ′ = X; otherwise, r ′ = r and X ′ = X ∪ {i}.
Proof. The non-trivial parts of the statement follow from Proposition 6. □ Using these results, is easy to give a procedure that generates all the orchard networks over a set [n] of taxa and with a given number r of reticulations. Indeed, it is enough to generate, for each positive integer m < n, the set S m ([n], r), and the latter can be generated as follows: (1) Start with the sequence S = (m, n), of length 1, with support X = {m, n}, and whose set of annotated reducible pairs is ARP = ARP(S) = {(m, n) C , (n, m) C }. Theorem 11. The set of sequences yielded by the procedure above is S([n], r).
Proof. Let S be a sequence yielded by the procedure. The condition that S has support [n] and has r reticulations is guaranteed by the condition in step 3 of the procedure. The condition that S is a minimum augmentation sequence follows by applying recursively Theorem 10, thanks to the condition in step 2, and with the starting condition in step 1 being justified by Proposition 9. Conversely, if S = s 1 · · · s k ∈ S([n], r), then s k = (m, n) for some m (thanks to Proposition 9), and it will be considered in step 1. At each step, considering the suffix S t = s t · · · s k in step 2, the pair s t−1 will fulfill the conditions (thanks to Theorem 10), and hence S t−1 = s t−1 s t · · · s k will be considered in the next iteration. Finally, in step 3, the sequence S will be yielded. □ Some remarks are due: (1) The set s ARP in step 2 can be computed using Theorem 4, and it can be done in linear time with respect to the length of S. Also, if the pairs in ARP are stored increasingly ordered with respect to the lexicographic ordering, then the computation of s ARP can be performed so that s ARP keeps being ordered and, in particular, its minimum element can be found in constant time.
(2) Another advantage of storing the pairs in ARP ordered is that, in order to determine if (i, j) = MRP((i, j)S), one does not need to compute the whole set (i,j) ARP. Indeed, in the process of building (i,j) ARP, at most three pairs in ARP can disappear, and hence one only needs to take the first four elements in ARP, decide which of them belong to (i,j) ARP, and test if (i, j) is smaller than each of those.  4); however, none of these sequences is minimum, as can be easily checked in each case. (4) The search of extensions can be pruned. For instance, if at a given stage, the sequence S has r reticulations, the only pairs (i, j) that have to be considered are those with i / ∈ Supp(S), since otherwise the number of reticulations would be greater than r.
(5) Also in the case that we are adding a cherry (that is, when i / ∈ Supp(S)) we can restrict ourselves to the case that i < j, since otherwise (j, i) would be a reducible pair in (i,j)S I, and since (j, i) < (i, j), it is impossible that (i, j) = MRP((i, j)S). (6) The algorithm can be easily modified, so that instead of generating all the sequences with exactly r reticulations, it generates all sequences with at most r reticulations.

Generation of tree-child networks
A network is tree-child if every node that is not a leaf has a child that is a tree node [CRV09]. For brevity, we shall simply say that each interior node has a tree child.
The same procedure we have described to generate orchard networks can be adapted to generate all tree-child networks over [n], by adding some conditions to ensure that the generated sequences correspond to tree-child networks.
First, we need to decide when the reductions and augmentations defined in the previous sections produce tree-child networks.
We start with the following result, adapted from [BS16, Lemma 4.1], that states that reductions of tree-child networks are tree-child networks.
Lemma 12. Let N be a tree-child network. Then, N is an orchard network and, if (i, j) ∈ RP(N ), then N (i,j) is also a tree-child network.
In order to decide whether or not an augmentation of a tree-child network is tree-child, we need to introduce new terminology. Let N be a network over X ⊆ [n]. Then, we define the state σ N (i) of i ∈ [n] as follows: If the network is clear from the context we shall simply write σ(i) instead of σ N (i). Then, σ N : [n] → {N, P, S, T} is a mapping that gives the state of each i ∈ [n] in N . We also define the state of a network N as σ(N ) = (σ N (1), . . . , σ N (n)). Finally, if S is an augmentation sequence, we shall denote σ(S) = σ( S I).
The following result gives the conditions under which an augmentation produces a tree-child network.
Theorem 13. Let N be an orchard network. Then, (i,j) N is a tree-child network if, and only if, N is tree-child and σ N (i) ∈ {N, T}.
Proof. Let N ′ = (i,j) N . From Lemma 12 we know that if N ′ is tree-child, N is also tree-child. Now, suppose that σ N (i) = P. Then, i is a leaf in N and its parent p i is a reticulation. When applying the augmentation (i, j), the arc p i i is split introducing a new node v that shall become a reticulation. Then, in N ′ , the only child of p i is v, which is a reticulation. Therefore, N ′ is not tree-child.
Similarly, suppose that σ N (i) = S. Then, i is a leaf in N , its parent p i is a tree node and its sibling s i is a reticulation. Again, in the process of applying the augmentation (i, j), the arc p i i is subdivided introducing a new reticulation v. Thus, the children of p i in N ′ are v and s i , both reticulations, so N ′ is not tree-child, against the hypothesis. Therefore, σ N (i) / ∈ {P, S}, which is equivalent to σ N (i) ∈ {N, T}.
Conversely, assume that N is tree-child. Due to the local nature of the augmentation processes, the condition that each node (other than a leaf) in N ′ has a tree child needs only to be tested for the nodes that are adjacent to the leaves involved in the augmentation.
First, assume that σ N (i) = N, and let p j be the parent of j in N . The augmentation process creates a tree node v in N ′ with children i, j and parent p j . Now, p j keeps having a tree child (the node v), and the new internal node v has both children that are tree nodes (the leaves i and j). Hence, the condition of being tree-child is preserved.
Second, assume that σ N (i) = T, which implies that i ∈ X and hence the augmentation process creates two elementary nodes: u (a tree node) between j and its parent p j , and v (a reticulation) between i and its parent p i . Also, since σ N (i) = T, we have that the sibling s i of i in N (that is, the child of p i in N different from i) is a tree node. In N ′ , p j has u as a tree child, p i has s i , u has j, and v has i. Hence, the condition of being tree-child is preserved. □ We describe now how to compute σ( (i,j) N ) from σ(N ). For simplicity, we write N ′ = (i,j) N , σ = σ N and σ ′ = σ N ′ , and we will restrict to the cases of interest that σ(i) ∈ {N, T}.
• Case σ(i) = N. In this case, (i, j) is a cherry in N ′ , and therefore σ ′ (i) = σ ′ (j) = T. From the local behavior of the augmentation, for any other leaf l in N , its parent (and its sibling, in case it has one) remains the same. Therefore, we conclude that σ ′ (i) = σ ′ (j) = T and σ ′ (l) = σ(l) for all l ∈ [n] \ {i, j}. • Case σ(i) = T. In this case, (i, j) is a reticulated cherry in N ′ , hence σ ′ (i) = P and σ ′ (j) = S.
Thanks again to the local behaviour of the augmentation, the state of a leaf l in N ′ can only differ from its state in N if its parent or sibling change from being a tree node to a reticulation (or viceversa). Hence, only siblings of i and j have to be taken into consideration. If j was the sibling of another leaf l in N , then l would still have a sibling in N ′ that is a tree node (namely, the parent of j in N ′ ) and hence the state of l would not change. If i was the sibling of another leaf l in N , which can be written as (i, l) C ∈ ARP(N ), then l would change from having a sibling that is a tree node (the leaf i) to having a sibling that is a reticulation (the parent of i in N ′ ). Hence σ(l) = T but σ ′ (l) = S. We can summarize these computations in the following result. • If σ(i) = N, for all l ∈ [n] \ {i, j}.
Then (i,j) N is a tree-child network over X ∪ {i} with state function (i,j) σ.
We denote by T (X, r) the subset of S(X, r) formed by sequences S such that S I is a tree-child network. Thanks to Lemma 12, the set T (X, r) is in bijection with the set of tree-child networks over the set of taxa X and with r reticulations. Also, notice that T ([n], r) = Then, we can modify the procedure that generates all orchard networks over [n] with r reticulations to generate all tree-child networks over [n] with r reticulations, provided that r < n.
Indeed, for every positive integer m < n, we can generate the sets T m ([n], r) as follows: (1) Start with the sequence S = (m, n), of length 1, with support X = {m, n}, with set of annotated reducible pairs ARP = ARP(S) = {(m, n) C , (n, m) C } and with state σ = σ(S) whose entries are all N except the m-th and n-th entry which are T.
(2) Recursively, given a sequence of pairs S (and assuming that S I is tree-child), with support X, and given also the set ARP = ARP(S) and the state σ = σ(S), find all possible pairs (i, j) such that σ(i) ∈ {N, T} and (i, j) = MRP((i, j)S). For each such (i, j), consider the extended sequence S ′ = (i, j)S with support X ′ = S ∪ {i}, with set of annotated reducible pairs ARP ′ = s ARP and with state σ ′ = s σ.
(3) If X = [n] and the length of the obtained sequence is |S| = n + r − 1, then yield the sequence S.
Theorem 15. The set of sequences yielded by the procedure above is T ([n], r).
Proof. The result follows using the same reasoning as in Theorem 11, using now Theorem 13 to ensure that the yielded networks are tree-child. □ Some remarks follow: (1) The state function σ ′ in step 2. can be computed using Theorem 14, and notice that the information in ARP is also needed.
(2) As in the case of orchard networks, the procedure can be adapted to yield all tree-child networks over [n] with at most r reticulations. In particular, since tree-child networks over [n] have at most n − 1 reticulations [CRV09, Proposition 1], we can generate all of them. (3) The procedure given for generating tree-child networks can be adapted to generate all stackfree [SS18] orchard networks, simply checking if σ(i) ̸ = P instead of checking if σ(i) ∈ {N, T}.

Computational experiments
The procedure to generate orchard and tree-child networks described in this paper has been implemented in C. Source files, documentation and examples are available in the repository https: //github.com/gerardet46/OrchardGenerator. Notice that the output of the implementation are complete reducible sequences, given as strings, and that they can be used as input to build and manipulate networks using the Python package PhyloNetworks [Car23].
There are some interesting details to comment. First, as we said, the set ARP is kept ordered, and the cherries (i, j) with i > j are ignored. Taking this into account, notice that if N is an orchard network on X ⊆ [n], it holds that | ARP(N )| ≤ 2 3 |X| ≤ 2 3 n (and there is always an orchard network such that the equality holds). Therefore, the set ARP can be implemented as an static array, which is much faster than a dynamic one. Also, given a sequence S = s 1 . . . s k , we store the set {ARP(s k ), ARP(s k−1 s k ), . . . , ARP(S)} for faster access when trying different candidate extensions.
Notice also that the only data needed to store the networks is X, S and ARP (and σ for tree-child networks), but there is no need to store the network N itself. Also, the operations involved in the algorithm are very simple, so they could be easily implemented in C, optimizing the performance.
We have also implemented a random orchard network generator that follows the same lines of the procedure to generate all the networks, but choosing a random pair at each step in order to produce a sequence, instead of trying all the candidates. Notice however that this generator does not generate networks uniformly. Indeed, even at the first step, the number of MCRS ending in (n − 1, n) is greater than the number of those ending in (1, n).
Finally, the algorithm can be parallelized, considering a partition of suffixes and creating a process for each subset of suffixes, which generates all sequences ending in a suffix from the corresponding subset.
Using this implementation, we have computed the number of orchard networks for small number of leaves and reticulations, shown in Table 1. As for the generation of tree-child networks, it is worth to mention the speed of the computation compared to previously implemented methods. Indeed, Table 2 shows the time of execution for the generation of all tree-child networks with n = 5 leaves using the implementations of the results in this paper compared to those in [CPS19] and [CZ20].  Table 1. Number of orchard networks with n leaves and r reticulations, for 2 ≤ n ≤ 6 and 0 ≤ r ≤ 8, together with the total time used to compute these numbers, for each value of n.
Implementation in Python from [CZ20] 9m19.249s Implementation in Python from [CPS19] 7m23.162s Implementation in C of the current paper 0m00.056s Table 2. Time needed for the generation of all tree-child networks with n = 5 leaves using different implementations.

Conclusions
Phylogenetic networks model evolutionary relationships among organisms and overcome the limitations of using phylogenetic trees by allowing the representation of reticulate processes.
In this paper, we have considered the problem of the efficient and injective generation of all orchard and tree-child networks (with a given number of leaves and reticulations), two special classes of phylogenetic networks with biological relevance [KPKW22]. Our method is based on considering sequences of pairs of integers that characterize those networks [JM21] and finding a subset of those (called minimum complete reducible sequences) that characterize the networks injectively.
To this end, we have first shown that such a sequence must end in a pair (m, n), where n is the desired number of leaves and m < n, and that we can iteratively extend the sequences by prepending new pairs to generate the sequences that encode the networks. This method is efficient since there is no need to construct the network itself in order to check if the candidate sequence effectively corresponds to an orchard (or tree-child) network.
The implementation of the algorithms described in the paper allows a fast generation of the sequences (and implicitly of the networks). For example, our implementation is capable of generating all orchard networks with 4 leaves and at most 8 reticulations, of which there are about 6 billions of them, in approximately 6 seconds. 1 For tree-child networks, we have shown that our method is much faster than other methods previously published and implemented.
There are some natural questions that arise as a possible future work, mainly in the direction of extending our results to the generation of other classes of phylogenetic networks. One of the possible generalizations is getting rid of the binary condition and generating semi-binary and non-binary (orchard and tree-child) networks. In this sense, the results in [JM21] could be applied, considering more possible annotations of pairs, in order to cover the six different reductions that this paper considers. Another direction could be trying to use other topological conditions on the networks to be generated. For instance, and as we have commented at the end of Section 7, only a small change in our method is needed in order to generate stack-free orchard networks. Another potential subclass of networks where our methods could apply is the class of normal networks [Wil10], which is a subclass of tree-child networks where shortcuts are not allowed (that is, if two nodes are linked by an arc, then they cannot be connected by another path).