Several studies were conducted regarding the inbreeding strategy of different animals. They have reported from inbreeding tolerance in for different species [2, 3] to inbreeding preference for some of them [4] . Recent studies have also found evidence of regular incest behavior in wild mammals [5] . Even more interesting is the fact that the existence of extreme inbreeding in humans was also detected when considering a European ancestry sample, taken from the contemporary UK population [1] .
Motivated by the new evidence, and based on the ideas and the model presented in previous works [6, 7, 8] , here we address the question of how animal species (including humans) may achieve diversity when having inbreeding at the same time. In our previous studies, we simulated random trees of ancestors considering inbreeding along the tree. In previous work done by some of the authors (Ref. [8] ), to build the tree, generations were added of it in a markovian way. The number of ancestors, for a given generation, was a random quantity limited to a maximum value given by the case where all ancestors were different. In that approach, the only constraint considered in the number of ancestors was random blood relationships between individuals of the same generation. Other restrictions in the number of ancestors related to culture, in the human case, ethological in the animal case, or regarding isolation of populations, were not taken into account.
From a mathematical point of view, it was possible to calculate the first two cumulants of the probability distribution of ancestor number at each generation. This was possible through the development of new methods to describe the underlying stochastic process.
In the present work, we deepen our previous studies. We now address diversity in terms of all possible links between the ancestors in the tree. First, we count how many different inbreeding cases are, in terms of tree connections, for a fixed number of available individuals at a given generation. We find that even for a few generations, this number is quite large. Motivated by this, we present an algorithm that produces different tree representations randomly, for a fixed number of individuals per generation, chosen by the user. The number of individuals in each generation may also be previously fixed by the user. The algorithm, in this case, takes into account the gender of the individuals, supposing a binary female/male reproduction scheme (which can be changed by the user), ensuring that each individual has one female and one male direct ancestor. We also present a mapping from the tree representation to an adjacency matrix. We show how these matrices capture the diversity from the trees and are very useful tools to represent the links between generations. The code will be available online, and it can be easily used and modified.
The manuscript is organized as follows. In Section 2, we present the algorithmic representation of the tree and introduce the necessary notation. The mathematical treatment of the number of possible tree representations is presented in Section 3. Section 4 shows the description of the algorithm and computational implementation. The adjacency matrix representation is discussed in Section 5. In that section, we profit from the flexibility of the code presented in this work to generate different cases of trees and comment on their different characteristics in the adjacency matrix representation. Finally, we close in Section 6 with conclusions and discuss future work.
In this section, we will describe how we consider and build different representations of trees with inbreeding, and mention the notation we will use in the rest of this work. We will consider only binary animal reproduction (although the algorithm that will be described in Section 4 can be adapted to consider other types of interaction). In our case, we will distinguish between two reproductory genders, male and female, and for each individual in each generation, a female and a male direct ancestors are required. We call a "full binary tree" that one without endogamy and where there are no "siblings", individuals have no common ancestors. An example of a 7 generation tree is shown in Figure 1 . Now, we proceed to construct one possible tree representation. To do this, we start from the full binary tree. To uniquely identify each individual, we assign each of them a number and distinguish males and females with odd and even numbers, respectively. We then proceed to remove in each generation as many nodes as indicated by the simulation presented in our previous work [8] . The labels to be removed, in each generation, are chosen at random. The gender of the removed individuals is chosen in such a way that we keep close to 50% individuals of each gender (this may be changed in the code presented here if necessary).
Regarding notation used in present work: we call A n the totalnumber of ancestors at the n−generation given by the simulation presented in [8] . The removed ancestors at n−generation will be called A n [9] , in the way described by the following equation
where n = 1, · · · , N , 2 n is the number of individuals at the n − th generation in a full binary tree, and A 1 = 2, or equivalently A 1 = 0.
In Figure 2 , we show the resulting tree, obtained after subtracting the nodes from the binary graph. Due to this process, some of the individuals will lose the links which represent the "progenitor-offspring" relationship between generations. In Figure 2 we indicate different types of missing links in the nodes. The goal now is to distribute all the missing links between generations among the current nodes. These nodes will be the progenitors of those members who lost a link in the tree when removing individuals. These new links represent inbreeding relationships, since they will not follow the full binary tree. Figure 1 . Pink nodes labeled with even numbers represent females, green nodes with odd numbers represent males. This is the first step to build the inbreeding tree representation. Nodes missing different types of links are indicated.
Therefore, the next step to obtain one realization of the ancestor tree is to generate an algorithm that includes a criterion to distribute the nodes in such a way that each member of the tree has both a female and a male direct ancestor (since we consider only binary reproduction). In this work, the criterion that we applied was to choose the remaining nodes (ancestors) at random to be linked to the nodes in the following generation. We take into account the label of the ancestor to complete for each node one direct ancestor of each type, and in such a way that every node is connected to both the previous and the next generation (except of course those from the first and the last generations). This criterion may be changed in the code if needed. As a result, we obtained a graph like the one shown in Figure 3 . Clearly, this is only one possible realization of a tree with seven generations, and the numbers of individuals per generation shown in the figure. In the following section, we address the question of how many possible trees can be constructed fixing the number of members at each level. Figure 3 : Example of a tree where some ancestors (nodes) have been removed, and the links between the remaining ancestors were added. As before, pink nodes labeled with even numbers represent females, green nodes with odd numbers represent males. This graph is one possible representation of inbreeding between individuals.
In section 2, we built one of the possible trees given the number of male and female ancestors in each generation. Now we will count all possible and different trees, meaning all the different kin relations between the individuals of the tree. In terms of Figure 2 , this means all the possible graphs (such as Figure 3 ) that we can obtain with labeled ancestors with all the different links distribution.
To start the procedure let's suppose that this tree has N generations, we remove from the tree the value of A n ancestors in each generation n, including male M n and female F n , in a way that:
Let's also remember that A n = 2 n − A n , where this equation allows us to compare the value of removed ancestors with the binary tree. Now we considered the n−generation. We call µ n the number of individuals in the tree without mother, ϕ n the number of individuals without a father, and π n the number of individuals lacking both, mother and father. We know that if we count the number of individuals without one or two direct ancestors, we have the following constraints defined by Equation (3) and (4). Let's consider first Equation (3):
this is because the number of individuals in the tree without a female direct ancestor, or lacking both direct ancestors must be equivalent to all the links that were lost when subtracting F n+1 female nodes in the previous generation.
In other words, the number of individuals without mother in the n−generation plus the number of individuals lacing of mother and father is equal to the number of females substracted in the (n+1)−generation. Following the same reasoning for males, we obtain:
Also equivalent to all the links that were lost, when subtracting M n+1 male nodes in the previous generation.
If we want to compare with a binary tree, meaning one ancestors tree without inbreeding, we know that the number of female and male ancestors in a given n generation is half of each one, as shown in Equation (5).
Now we use Equation (1) with equation (2) to split and count how many available male (M n ) and female (F n ) we have, to distribute kinship represented by the links in the graph.
where
We now have all that we need to count how many different ways we have to distribute the link between the actual available males and females.
Let's consider first the case where we distribute all ancestors in the (n + 1)−generation in a way that every ancestor in the n−generation is linked to different nodes till the point that we have no more different nodes in the (n + 1)−generation to assign. Now, we count how many different ways we have to distribute the missing links between the consecutive generations, repeating direct ancestors (i.e. an individual from the (n + 1)-generation has more than one link to the next generation). As a consequence, some individuals will be "siblings" or "half-siblings".
The total number of different ways to distribute all the females at the (n + 1)-generation between the individuals without a female direct ancestor µ at the n−generation, T µ (n), comes from Equation (8).
where C(F n+1 , 1) denotes the combinatorial number of 1 element (one female progenitor) from a given set of F n+1 elements (potential female progenitors) and the index i corresponds to the individuals in n−generation without a female direct ancestor. Now, we proceed in the same way for the males: we count the ways to distribute all the males at the (n + 1)-generation between the individuals without a male direct ancestor ϕ at the n−generation, T ϕ (n), given by equation (9) .
where C(M n+1 , 1) denotes the number of the combination of one element (one male) from a given set of F n+1 elements (potential male progenitors) and the index j corresponds to the individuals in n−generation without a male direct ancestor. Finally, we distribute the female and male potential direct ancestors between the individuals without any progenitor in equation (10):
where the index k corresponds to the individuals in n−generation without without any links to the previous generation π. In this way, the full problem consists of counting with repetitions all the possible combinations to distribute the links between all the individuals without one or two progenitors, by the following simple multiplication:
In this way, Equation (11) shows all the possible different combinations for inbreeding links between two generations (a sub-graph). For every possible combination in one generation, there is a different path in the consecutive generation n + 1. For a tree whose total number of generations equals to N , the different possible inbreeding trees, T N , is given by Equation (12):
We know that for x ∈ N : C(x, 1) is equal to x, thus we can replace in Equation (12) the combinatorial number in each term:
And also we know that L l=1 S = S L , if S and l are independent, thus the Equation (13) can be rewritten as:
Rearranging we obtain:
It is useful to write Equation (15) in terms of the number of subtracted males and females, and also in terms of the binary tree. We can use use the relations given by Equations (6) and (7):
Finally if we use the constraint given by Equations (3) and (4) we obtain:
Equation (17) gives us the analytical way to count all possible inbreeding trees (graphs) for a fixed number of ancestors, given that we know how many males and females are not present in the ancestor's tree (compared to the full binary tree) for a tree with N generation.
If we want to consider what happens when we change the rate of subtracted male and female, we have to add all possible combinations of M k+1 + F k+1 that gives the same value of subtracted ancestors.
As we can see from Equation (17), this number could be very large. We can observe from the equation clearly that this number diverges when we increase the number of generations of the tree.
For example for a tree of 7 generations, as the one that we considered in the previous section, we can obtain the order of 1.99 × 10 78 different representations. Clearly, to generate and explore different representations a numerical method is needed. In this direction, we introduce an algorithm described in the next section to randomly generate representations for a tree with N generations. As we will discuss later, this code is flexible and allows different possibilities: one can build possible representations for an N generation tree with a fixed number of trees per generation, or fix the number of individuals and distribute them among N generations, or among more (or less) generations, etc., depending on the interest of the user.
The algorithm to construct one realization for the tree of ancestors with inbreeding starts with a full binary tree first (in terms of code, it is a list of nodes and links). In this tree, each member has an associated tag (a number).
We go over the description from Section 2 in detail, which we represent in Figure 4: 1. We start from a full binary tree with a given number of generations 2. From the full binary tree, we remove as many individuals as our simulation indicates [8] . Our first models had no distinction (here in the case of binary reproduction that distinction is the gender, but the code can be adapted for other situations). Now, we have to take a criterion like the one described in the previous sections to take into account how many labels or tags representing each gender we will extract. Here we choose this randomly, with the constraint that the number of both types of individuals remains the same. 3. After choosing a criterion, as some nodes were extracted, the links involving them disappear. Therefore, we must also take a criterion to distribute the missing links, to obtain one of all the possible representations. The criteria used to ensure that each node has a direct ancestor of each gender is as follows: we go over each of the generations of the tree except the last one. In each generation, we analyze one by one each node. In each node of the generation, we observe how many links arrive, and if these links came from a female or male gender label. With this information we decide what to do:
(a) If there is no link arriving at the node from the previous generation we randomly choose a node of the previous one to be its ancestor. (b) If there is a link from the node of the previous generation, we check the label from this ancestor and randomly choose an individual from that generation among those with the opposite label. (c) If the node considered already has the 2 links, we do not add anything.
When we finish, we move to the next generation and repeat the process, thus going through the entire tree.
In this way, we can obtain one representation, like the one shown at the left of Figure 4 . As we have mentioned, there are multiple ways to distribute the links between generations among the existing nodes. In this way, trees with very different kinship relationships can be created, keeping the same amount of ancestors.
To build the representation the program needs as input the number of ancestors per generation. This number may be obtained using the code presented in Ref. [8] , or fixed at will. With this information, the program plots one representation. The output of the program is: i) one of all possible graphs for the tree ii) its corresponding adjacency matrix, and node distributions [10] , which we present and discuss in the next section. Also, with the number of ancestors, the program estimates the number of possible trees configurations following the estimation explained in Section 3.
Our representation depends on the number of ancestors in each generation and gender labels, also depend on the seed we put in the random number generator. Different seed values give rise to different representations like those observed in Figure 5 .
Code to obtain the tree representation was developed using Python [11] and Networkx [12] . It will be available at the Github repository to be used and modified freely. 
Let us consider the adjacency matrix for a simple graph with vertex set V. It is a square |V| × |V| matrix M M M where the element M ij is 1 when there is a edge from the i−vertex to the j−vertex, and 0 when there is no edge [10] , where |V| is the number of elements of V. We use this definition with the matrix of each directed graph representing the tree in such a way that columns represent the relationships between generations. Each column always has two non-null elements representing each progenitor. Each row indicated how many nodes from the next generation are linked to a certain node. The vertical axis represents the nodes as ancestors and the horizontal axis as offspring. The case of a binary tree is represented at the left of Figure 6 . Let us compare it with what happens when we have an inbreeding tree, such as the one obtained in Figure 3 , which we show at the right of 6. In the first case, no inbreeding is reflected by the yellow line (which has slope 2), where none of the nodes share a progenitor. In the second case, it is interesting to observe that the yellow points separate from the slope corresponding to the di-graph, creating a unique distribution pattern. Adjacency matrix could be used as a useful measure of the degree of inbreeding with respect to the full binary tree.
The adjacency matrix (lower panel of Figure 6 ) acts as a fingerprint of each of our tree realizations. For each graph, the plot of the adjacency matrix is unique. To illustrate this point, Figure 7 shows the adjacency matrices corresponding to the trees presented in Figure 5 . Each of them is different, and so are their corresponding graphs. In all the matrices, the number of ancestors at the beginning of the tree is low. As a result, a flattening of the curve appears as the number of the generation increases. The red rectangles indicate yellow points that must necessarily belong to the same generation. The cyan rectangles at the bottom of the graphs indicate the first and second generation from the trees, using the fact that we started from a binary tree. These show that these graphs also indicate the number of generations from Figure 5 . In these plots, the distinctive imprint of each tree can be seen more clearly in the distributions of yellow dots on the dark background. The red rectangles indicate dots that must be in the same generation. The cyan rectangles, at the bottom fo the matrices, belong to the first and second generation. each tree: it is simply the number of rectangles. It can be seen more clearly from these plots that, even though the number of ancestors is the same, the edges in the tree are distributed differently. This is reflected in the different distribution of yellow points in the rectangles.
In order to further explore the information displayed in the adjacency matrices, we compare three cases: one of the trees we have already presented, a 7-generation tree with the same total number of nodes but a different distribution, and a 9-generation tree with the same number of nodes. The matrices and the trees are shown in Figure 8 . First, it can be clearly seen that the graph from panel (c) corresponds to a tree with more generations. Then, comparing panels (a) and (b) we can see that, for example, the tree associated with the matrix in panel (b) must have more nodes in the last generation. This further illustrates the usefulness of this graphic representation. Finally, it is interesting to study other properties of the graph, such as the degree of connectivity of the nodes. By studying the distribution of the output degree of the nodes, we can observe that there is inbreeding, as it is shown in Figure 9 . Here, we plot a histogram of the number of descendants associated with each node for two cases: on the left, the tree presented in Figure 3 , and on the right, for the 9-generation tree with the same number of nodes shown in Figure 8 . From the Figure, we can see that in the case of the 7-generation tree, the majority of the nodes have one child, some of them have two, and only a minority have more than 5. For the 9-generation tree, the histogram is flatter and broader. This is associated with the fact that since this tree has the same number of nodes than a 7-generation tree, more edges are missing from a full binary tree, and therefore more nodes need to be linked to more than one node from the next generation.
In this way, we have shown how this algorithm makes it possible to obtain different tree representations and explore the connectivity information in each realization in different manners: either plotting the tree, studying the adjacency matrix or analyzing the connectivity between nodes. 
In the present work, we have presented an algorithm with a software implementation to build one possible ancestors' tree with its corresponding graph. On the other hand, we analytically estimated the number of different trees that can be obtained for the same number of ancestors then applied our estimation to a seven-generation model. We have shown that such a number is really large, which supports the need to use an algorithm to construct and analyze different trees.
Regarding the software implementation, this algorithm provides different tools to study the properties of the trees and their degree of inbreeding: it produces the tree itself, the adjacency matrix (which we have shown is a useful tool to visualize and distinguish trees), and a histogram illustrating the degree of connectivity of the nodes. The code gives the possibility of extending these tools to perform different types of simulations and analysis. For example, our next step would be to add attributes to the nodes that could be considered as genes, or maybe diseases, study the propagation of this feature along the tree, and compare it with the degree of inbreeding.
Concerning the analytical estimation, similar reasoning can be useful for accounting in other cases where nodes with some attributes are subtracted.
In this way, we have shown how, from a simple ancestor distribution model, we can study the family of corresponding graphs with different edges for the trees. This situation constitutes an interesting example of how, from a small set of elements or individuals, an enormous diversity of cases can be obtained. In this way, we suggest that even small inbred groups could still give rise to different genetic variability.
Finally, the code presented in this work is open source and easily adapted to study other situations relevant to current studies such as opinion spread models, disease propagation, etc. We are thus certain that it will prove to be a useful tool for future studies.