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.
I know that the number of all possible graphs is given by
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
This gives us the series A003024
But I'd like to find the algorithm to be able to loop over all these possible graphs and not just count them.
I know there is some code available for undirected graphs, but I couldn't get them to work for me.
I'm open to any form of representation of the graph, be it a library, a custom function or a list of lists.
Example- when you have two labels, there are 3 possible graphs:
[[A:{}], [B:{}]] # A B no connection P(A,B) = P(A)P(B)
[[A:{B}], [B:{}]] # A -> B P(A,B) = P(A)P(B|A)
[[A:{}], [B:{A}]] # A <- B P(A,B) = P(B)P(A|B)