title: Python code to enumerate over all acyclic digraphs with 5 nodes
body: <p>So I was recently studying this course on Probabilistic Graphical Models and I wanted to try a hands-on example. In the example, I want to loop over all possible combinations (29,281) acyclic digraphs (or DAGs) and see how they fit the data.</p>
  
  <p>I know that the number of all possible graphs is given by </p>
  
  <pre><code>from scipy.misc import comb
  import numpy as np
  
  def a(n):
      if n == 0:
          return 1
      else:
          sum = 0
          for k in range(1,n+1):
              sum += np.power(-1,k+1)    * \
                     comb(n,k)           * \
                     np.power(2,k*(n-k)) * \
                     a(n-k)
          return sum
  </code></pre>
  
  <p>This gives us the series <a href="https://oeis.org/A003024" rel="nofollow noreferrer">A003024</a> </p>
  
  <p>But I'd like to find the algorithm to be able to loop over all these possible graphs and not just count them.</p>
  
  <p>I know there is some code available for undirected graphs, but I couldn't get them to work for me.</p>
  
  <p>I'm open to any form of representation of the graph, be it a library, a custom function or a list of lists.</p>
  
  <p>Example- when you have two labels, there are 3 possible graphs:</p>
  
  <pre><code>[[A:{}], [B:{}]]  # A    B no connection P(A,B) = P(A)P(B)
  [[A:{B}], [B:{}]] # A -&gt; B               P(A,B) = P(A)P(B|A)
  [[A:{}], [B:{A}]] # A &lt;- B               P(A,B) = P(B)P(A|B)
  </code></pre>