# Generating all directed acyclic graphs with constraints

I am interested in listing all the unlabeled1 acyclic digraphs with n vertices which satisfy some additional constraints, such as (a) the resulting graph is connected and (b) except for R identified root vertices, all vertices have two incoming edges and zero or one outgoing edges.

There may also be additional domain-specific constraints, but they are probably easy enough to implement by simply generating and rejecting graphs that do not meet the constraints. In principle, conditions (a) and (b) above could be satisfied in this way above, but at least in the case of (b) it seems very advantageous to consider them directly in generating process since the output is likely to be quite sparse (i.e., a large majority of graphs will fail to satisfy (b) and this ration increases with increasing n).

My problem is twofold:

1. Even ignoring the constraints part, I have not be able to determine how to efficiently generate DAGs. I know there are a lot of them so I'll mostly be dealing with smallish n, let's say n < 15. There seem to be some promising papers, although may of them are focused on the closed-form counting of the number of graphs rather than actual generation, and any remaining ones are paywalled.

2. I want to apply the at least enough of the constraint part during the generation to avoid the case where nearly all generated graphs are simply rejected. That is, I hope the generation of the constrained graphs is considerably faster than the generation of the much larger number without constraints.

You can find lots of information on random generation of such graphs, but not much on exhaustive generation (which seems like the easier of the two problems).

1 That is, I want to exclude generation of isomorphic graphs.

For small $n$, the easiest solution might be to download a list of all non-isomorphic graphs and then filter them according to your condition.

You might take a look at Brendan McKay's collection, constructed using geng as part of the Nauty graph isomorphism package.

See Enumerate all non-isomorphic graphs of a certain size for more details and citations.

• I looked at nauty and geng but they didn't seem like a good fit here. In particular, geng is focused mostly on undirected graphs - the approach to get directed graphs seems to be to generate all the underlying undirected skeletons, and then to use the option in nauty to generate all orientations of the skeletons. Unfortunately, this doesn't work well at all with constraints - many of the constraints cannot be expressed at all in the undirected graph. – BeeOnRope Jul 12 '16 at 18:57
• ... the result being that you'd need to generate an intractably large space of undirected graphs and all their orientations to generate the relatively much sparser output of directed graphs. So the approach fails too early (i.e., at too small an n). – BeeOnRope Jul 12 '16 at 18:59
• Thanks also for the link to the other question - I had read it before posting this. It also focuses on the undirected case, and without constraints. The leading answer is simply to download them from McKay's collection, but as described above, this doesn't work for my case where the desired graphs are very sparse compared to the entire domain of graphs. – BeeOnRope Jul 12 '16 at 19:03
• @BeeOnRope, OK, sorry this wasn't useful! Would you like me to delete this answer, to increase the chances you get other useful answers? If so, it would help for you to edit your question to state that you trid nauty/geng and they aren't a good fit and explain what you explained here (and in general it's often a good idea to show in the question what approaches you considered and rejected, so we don't suggest something you've already come up with). – D.W. Jul 12 '16 at 19:05
• I think it makes sense to leave it here since it contains useful info such as why nauty/geng are a poor fit. As you suggest I will also integrate a note about that into the the question. – BeeOnRope Jul 12 '16 at 19:11

You can use nauty to generate all isomorphism classes of undirected graphs, then generate all acyclic orientations of each graph.

An algorithm to generate all acyclic orientations of a given undirected graph can be found here:

http://www.sciencedirect.com/science/article/pii/S0196677497908919

• This solution was discussed already above in the comments to DW's answer. The problem is that after the stage of generating all the directed orientations of the undirected skeletons, you have too many graphs to handle. I need to apply the constraints earlier to avoid generating all possible graphs in the first place. – BeeOnRope Nov 25 '16 at 22:07
• The algorithm for generating acyclic orientations works recursively: at each step, it fixes orientations of a few edges and recursively orients the remainder of the graph. You can check at each step whether the oriented part already violates your constraints, and prune a lot of AOs that way. It won't be perfect but I have done something similar in the past and it's certainly much faster than generating all acyclic orientations and checking each one. – yhy Nov 27 '16 at 22:25
• Additionally, for the constraints that you've stated, it seems your non-root vertices must have degree 2 or 3 in the undirected graph, and you can order the vertices so that edges incident to non-root vertices are oriented first. You should be able to do this without too much waste on generating bad AOs. – yhy Nov 27 '16 at 22:33