## Random trees

`dag_random_tree()`

generates a random tree. By default it generates a binary tree where all leaf terms have depth = 9.

```
library(simona)
set.seed(123)
tree1 = dag_random_tree()
tree1
```

```
## An ontology_DAG object:
## Source: dag_random_tree
## 1023 terms / 1022 relations / a tree
## Root: 1
## Terms: 1, 10, 100, 1000, ...
## Max depth: 9
## Aspect ratio: 56.89:1
```

Strictly speaking, `tree1`

is not random. The tree is growing from the root. In `dag_random_tree()`

, there are several arguments that can be used for generating random trees.

`n_children`

: Number of child terms. It can be a single value where each term will the same number of child terms. The value can also be a range, then the number of child terms will be randomly picked in that range.
`p_stop`

: A branch can stop growing based on this probability. On a certain step of the tree growing, letâ€™s denote the set of leaf terms as `L`

, then, in the next round, `floor(length(L)*p_stop)`

leaf terms will stop growing, while the remaining leaf terms will continue to grow. If a leaf term continues to grow, it will be linked to `n_children`

child terms if `n_children`

is a single value, or pick a number from the range of `[n_children[1], n_children[2]]`

.

The tree growing stops when the number of total terms exceeds `max`

.

So the default call of `dag_random_tree()`

is identical to:

`dag_random_tree(n_children = 2, p_stop = 0, max = 2^10 - 1)`

We can change these arguments to some other values, such as:

```
tree2 = dag_random_tree(n_children = c(2, 6), p_stop = 0.5, max = 2000)
tree2
```

```
## An ontology_DAG object:
## Source: dag_random_tree
## 1999 terms / 1998 relations / a tree
## Root: 1
## Terms: 1, 10, 100, 1000, ...
## Max depth: 7
## Aspect ratio: 105.71:1
```

## Random DAGs

A more general random DAG is generated based on the random tree. Taking `tree1`

which is already generated, the function `dag_add_random_children()`

adds more random children to terms in `tree1`

.

```
dag1 = dag_add_random_children(tree1)
dag1
```

```
## An ontology_DAG object:
## Source: dag_add_random_children
## 1023 terms / 1115 relations
## Root: 1
## Terms: 1, 10, 100, 1000, ...
## Max depth: 9
## Avg number of parents: 1.09
## Avg number of children: 1.03
## Aspect ratio: 56.89:1 (based on the longest distance from root)
## 52.78:1 (based on the shortest distance from root)
```