.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/graph_theory/spectral/plot_01_barbell_bipartition.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_graph_theory_spectral_plot_01_barbell_bipartition.py: Fiedler's spectral bipartition of a "barbell" graph ========================================================= Two dense clusters joined by a single bridge edge -- a case where the Fiedler vector's sign cleanly recovers the two clusters, and the small algebraic connectivity reflects how easily the graph is disconnected (by removing just the bridge). .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: Python from mathematicskit.graph_theory import Graph, spectral_analysis from mathematicskit.graph_theory.utils.generators import complete_graph from mathematicskit.graph_theory.visualizers.plots import plot_spectral_bipartition .. GENERATED FROM PYTHON SOURCE LINES 17-19 Build the barbell graph: two K5 cliques joined by one edge ------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-30 .. code-block:: Python cluster_size = 5 g = Graph(2 * cluster_size) left = complete_graph(cluster_size) right = complete_graph(cluster_size) for u, v, w in left.edges(): g.add_edge(u, v, w) for u, v, w in right.edges(): g.add_edge(u + cluster_size, v + cluster_size, w) g.add_edge(cluster_size - 1, cluster_size) # the single bridge .. GENERATED FROM PYTHON SOURCE LINES 31-33 Spectral analysis ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: Python result = spectral_analysis(g) print(f"algebraic connectivity: {result.algebraic_connectivity:.4f}") print(f"spectral bipartition: {result.bipartition.astype(int)}") plot_spectral_bipartition(g, result) .. image-sg:: /api/gallery/graph_theory/spectral/images/sphx_glr_plot_01_barbell_bipartition_001.png :alt: Spectral bipartition (algebraic connectivity = 0.298) :srcset: /api/gallery/graph_theory/spectral/images/sphx_glr_plot_01_barbell_bipartition_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none algebraic connectivity: 0.2984 spectral bipartition: [1 1 1 1 1 0 0 0 0 0] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.017 seconds) .. _sphx_glr_download_api_gallery_graph_theory_spectral_plot_01_barbell_bipartition.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_barbell_bipartition.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_barbell_bipartition.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_barbell_bipartition.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_