Skip to main content

Generate a uniformly random DAG

Source: R/construction.R

Description

Generates a random DAG on n nodes named V1, ..., Vn, sampled uniformly at random from the space of all labelled DAGs on n nodes, using a recursive-enumeration algorithm.

Usage

uniform_dag(n, seed = NULL, counts = NULL)

uniform_dag_counts(n)

Arguments

n Number of nodes. For uniform_dag_counts(), the largest number of nodes the table can be used for.
seed Optional integer seed for reproducibility.
counts Optional table from uniform_dag_counts(), computed for at least n nodes.

Details

Unlike generate_graph(), which samples edges independently (uniform over graphs of a given size/density but not uniform over the space of DAGs), this samples uniformly over the full space of labelled DAGs on n nodes.

Every draw has to compute a table whose cost grows quickly with n. To draw many DAGs, compute it once with uniform_dag_counts() and pass it as counts.

Value

For uniform_dag(), a cgraph. For uniform_dag_counts(), an external pointer of class uniform_dag_counts to the table, which is held by the compiled library, so it does not survive saving and reloading or a restart of R.

References

Kuipers J, Moffa G (2015). “Uniform random generation of large acyclic digraphs.” Statistics and Computing, 25(2), 227–242.

Examples

dag <- uniform_dag(6, seed = 1405)
dag
#> DAG with 6 nodes and 11 edges:
#>   nodes: V1, V2, V3, V4, V5, V6
#>   edges:
#>     V6 --> V5, V2 --> V5, V1 --> V5, V2 --> V6, V1 --> V6, … (6 more)

# Reuse the table across many draws
table <- uniform_dag_counts(20)
dags <- lapply(1:5, function(i) uniform_dag(20, seed = i, counts = table))