Ejemplo n.º 1
0
def test_junction_tree_directed_unconnected_edges():
    B = nx.DiGraph()
    B.add_edges_from([("A", "B"), ("C", "D"), ("E", "F")])
    G = junction_tree(B)

    J = nx.Graph()
    J.add_nodes_from([("A", "B"), ("C", "D"), ("E", "F")])

    assert nx.is_isomorphic(G, J)
Ejemplo n.º 2
0
def test_junction_tree_directed_cascade():
    B = nx.DiGraph()
    B.add_edges_from([("A", "B"), ("B", "C"), ("C", "D")])
    G = junction_tree(B)

    J = nx.Graph()
    J.add_edges_from([
        (("A", "B"), ("B", )),
        (("B", ), ("B", "C")),
        (("B", "C"), ("C", )),
        (("C", ), ("C", "D")),
    ])
    assert nx.is_isomorphic(G, J)
Ejemplo n.º 3
0
from networkx.algorithms import moral
from networkx.algorithms.tree.decomposition import junction_tree
from networkx.drawing.nx_agraph import graphviz_layout as layout
import matplotlib.pyplot as plt

B = nx.DiGraph()
B.add_nodes_from(["A", "B", "C", "D", "E", "F"])
B.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("B", "F"), ("C", "E"),
                  ("E", "F")])

options = {"with_labels": True, "node_color": "white", "edgecolors": "blue"}

bayes_pos = layout(B, prog="neato")
ax1 = plt.subplot(1, 3, 1)
plt.title("Bayesian Network")
nx.draw_networkx(B, pos=bayes_pos, **options)

mg = moral.moral_graph(B)
plt.subplot(1, 3, 2, sharex=ax1, sharey=ax1)
plt.title("Moralized Graph")
nx.draw_networkx(mg, pos=bayes_pos, **options)

jt = junction_tree(B)
plt.subplot(1, 3, 3)
plt.title("Junction Tree")
nsize = [2000 * len(n) for n in list(jt.nodes())]
nx.draw_networkx(jt, pos=layout(jt, prog="neato"), node_size=nsize, **options)

plt.tight_layout()
plt.show()