.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_graphs/plot_01_proximity_graphs.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_01_graphs_plot_01_proximity_graphs.py: Proximity graphs and their nesting ================================== A graph turns a cloud of samples into a structure: who is whose neighbor. The classical proximity graphs differ in how strict "neighbor" is: * the nearest neighbor graph (NNG) links each sample to its nearest neighbor; * the minimal spanning tree (MST) connects all samples with minimal total length; * the relative neighborhood graph (RNG) links two samples when no third sample is closer to both of them; * the Gabriel graph (GG) links two samples when the ball spanned by them as a diameter is empty; * the Delaunay triangulation (DT) is the dual of the Voronoi diagram. Each graph contains the previous one, NNG ⊆ MST ⊆ RNG ⊆ GG ⊆ DT, so the chain moves from "only the most obvious relations" to "every geometrically possible relation". .. GENERATED FROM PYTHON SOURCE LINES 20-46 .. image-sg:: /auto_examples/01_graphs/images/sphx_glr_plot_01_proximity_graphs_001.png :alt: nearest neighbor graph (69 edges), minimal spanning tree (99 edges), relative neighborhood graph (111 edges), Gabriel graph (168 edges), Delaunay triangulation (286 edges), symmetric 5-NN graph (313 edges) :srcset: /auto_examples/01_graphs/images/sphx_glr_plot_01_proximity_graphs_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {'edges': {'NNG': 69, 'MST': 99, 'RNG': 111, 'GG': 168, 'DT': 286}, 'nested': {'NNG<=MST': True, 'MST<=RNG': True, 'RNG<=GG': True, 'GG<=DT': True}} | .. code-block:: Python import matplotlib.pyplot as plt from graphclust import graphs from graphclust.datasets import make_variety from graphclust.plotting import plot_graph # [snippet:graphs] X, y = make_variety() G = { "nearest neighbor graph": graphs.nearest_neighbor_graph(X), "minimal spanning tree": graphs.minimum_spanning_tree(X), "relative neighborhood graph": graphs.relative_neighborhood_graph(X), "Gabriel graph": graphs.gabriel_graph(X), "Delaunay triangulation": graphs.delaunay_graph(X), "symmetric 5-NN graph": graphs.knn_graph(X, k=5), } print(graphs.neighborhood_chain(X)) # [/snippet:graphs] fig, axes = plt.subplots(2, 3, figsize=(12, 7.6)) for ax, (name, A) in zip(axes.flat, G.items()): plot_graph(X, A, ax=ax, node_size=10, title=f"{name} ({A.nnz // 2} edges)") ax.set_xticks([]) ax.set_yticks([]) plt.tight_layout() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.112 seconds) .. _sphx_glr_download_auto_examples_01_graphs_plot_01_proximity_graphs.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/graphclust/main?urlpath=lab/tree/notebooks/auto_examples/01_graphs/plot_01_proximity_graphs.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_proximity_graphs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_proximity_graphs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_proximity_graphs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_