summaryrefslogtreecommitdiff
path: root/examples/algorithms
diff options
context:
space:
mode:
authorMatthias Bruhns <45033731+mbruhns@users.noreply.github.com>2020-08-16 02:32:27 +0200
committerGitHub <noreply@github.com>2020-08-15 20:32:27 -0400
commit9813a65391d5852188321a73bb09f2e38aa1cfd3 (patch)
tree1f7db6b2234d81be584c1aff9d14bb2529eee3f6 /examples/algorithms
parent8968a4290521f440c311d021d1b7fd1a6571eac9 (diff)
downloadnetworkx-9813a65391d5852188321a73bb09f2e38aa1cfd3.tar.gz
junction_tree for #1012 (#4004)
* Initial commit. * Fixed PEP 8 issues. * Fixed more PEP 8 issues. * Added type to sepset-nodes. * Moved file to networkx/algorithms/tree, changed name of module to avoid namespace collision, added entry to doc system, changed deepcopy to list, removed check for None, shifted to G.is_directed(), added example. * Removed example code. * Removed unused import statement. * Moved notes section. * Fixed PEP 8 issues and removed old file. * Fixed PEP 8 issues. * Formatting with Black, added docstring to example and removed license information. * Added name to name of contributors. * Ran black i.e., black networkx/algorithms/tree/tests/test_junction_tree_algorithm.py * Updated explanation of junction trees, removed 'Graph' from unsupported classes. * DOC: tweaks to docstring. * Changed naming and updated docstring/code according to suggestions. * Removed old files. * Updated doc and init. * Minor tweaks in docs and import structure * Improve example Co-authored-by: Jarrod Millman <jarrod.millman@gmail.com> Co-authored-by: Ross Barnowski <rossbar@berkeley.edu> Co-authored-by: Dan Schult <dschult@colgate.edu>
Diffstat (limited to 'examples/algorithms')
-rw-r--r--examples/algorithms/plot_decomposition.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/examples/algorithms/plot_decomposition.py b/examples/algorithms/plot_decomposition.py
new file mode 100644
index 00000000..7b83a718
--- /dev/null
+++ b/examples/algorithms/plot_decomposition.py
@@ -0,0 +1,40 @@
+"""
+=============
+Decomposition
+=============
+
+Example of creating a junction tree from a directed graph.
+"""
+
+import networkx as nx
+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()