erdogant / bnlearn

Python library for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.
https://erdogant.github.io/bnlearn
Other
465 stars 46 forks source link

Make it possible to get nodes list in the topological sort order #26

Closed DMozhevitin closed 3 years ago

DMozhevitin commented 3 years ago

This feature might be useful in the forward sampling of Bayesian networks when we require all parents of the node to be already processed.

erdogant commented 3 years ago

Great suggestion. Im going to look into this.

erdogant commented 3 years ago

A first implementation of topological sorting is now available!

pip install -U bnlearn

import bnlearn as bn

# Set Edges in graph
edges = [('1', '2'),
     ('1', '3'),
     ('2', '4'),
     ('2', '3'),
     ('3', '4'),
     ('3', '5'),
     ]

# Make the actual Bayesian DAG
DAG = bn.make_DAG(edges, verbose=0)

# Plot
bn.plot(DAG)

# Topological ordering of the entire graph
bn.topological_sort(DAG)

#>['1', '2', '3', '4', '5']

# Topological ordering using starting point in graph
bn.topological_sort(DAG, '2')

#>['2', '3', '5', '4']