.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/09_problems/plot_32_acyclicity.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_09_problems_plot_32_acyclicity.py: Acyclic hypergraphs and join trees ================================== A hypergraph is :math:`\alpha`-acyclic if the GYO reduction (removing ear vertices and contained hyperedges) empties it. Acyclic hypergraphs have join trees: the hyperedges can be arranged in a tree in which the hyperedges that contain a vertex form a subtree. Join trees make joins of relational tables and inference on graphical models efficient; the 3-cycle of pairs is the smallest non-acyclic example. .. GENERATED FROM PYTHON SOURCE LINES 14-31 .. code-block:: Python import matplotlib.pyplot as plt import networkx as nx import hatvis as hv from hatvis import problems # [snippet:gyo] schema = hv.Hypergraph({"Orders": ["order", "customer", "date"], "Lines": ["order", "item", "qty"], "Items": ["item", "supplier", "price"], "Customers": ["customer", "city"]}) cycle = hv.Hypergraph({"R": ["a", "b"], "S": ["b", "c"], "T": ["a", "c"]}) for H in (schema, cycle): ok, steps = problems.gyo_reduction(H) print(f"alpha-acyclic: {ok}; first steps: {steps[:4]}") tree = problems.join_tree(schema) # [/snippet:gyo] print("join tree edges:", sorted(tuple(sorted(e)) for e in tree.edges)) .. rst-class:: sphx-glr-script-out .. code-block:: none alpha-acyclic: True; first steps: [('vertex', 'date'), ('vertex', 'qty'), ('vertex', 'supplier'), ('vertex', 'price')] alpha-acyclic: False; first steps: [] join tree edges: [('Customers', 'Orders'), ('Items', 'Lines'), ('Lines', 'Orders')] .. GENERATED FROM PYTHON SOURCE LINES 32-44 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(11, 4.5)) hv.plotting.draw_hypergraph(schema, layout="force", ax=axes[0], title="database schema as a hypergraph", random_state=2) pos = nx.spring_layout(tree, seed=0) nx.draw_networkx(tree, pos, ax=axes[1], node_color=hv.plotting.BRAND, font_color="white", node_size=1400, font_size=8, edge_color=hv.plotting.GRAY) nx.draw_networkx_edge_labels(tree, pos, ax=axes[1], font_size=7, edge_labels={(a, b): ",".join(sorted(set(schema.members(a)) & set(schema.members(b)))) for a, b in tree.edges}) axes[1].set_title("join tree (shared attributes on the links)") axes[1].axis("off") plt.tight_layout() .. image-sg:: /auto_examples/09_problems/images/sphx_glr_plot_32_acyclicity_001.png :alt: database schema as a hypergraph, join tree (shared attributes on the links) :srcset: /auto_examples/09_problems/images/sphx_glr_plot_32_acyclicity_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.228 seconds) .. _sphx_glr_download_auto_examples_09_problems_plot_32_acyclicity.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/hatvis/main?urlpath=lab/tree/notebooks/auto_examples/09_problems/plot_32_acyclicity.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_32_acyclicity.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_32_acyclicity.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_32_acyclicity.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_