
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorials/connected_components.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_tutorials_connected_components.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_tutorials_connected_components.py:


.. _tutorials-connected-components:

=====================
Connected Components
=====================

This example demonstrates how to visualise the connected components in a graph using :meth:`igraph.GraphBase.connected_components`.

.. GENERATED FROM PYTHON SOURCE LINES 10-14

.. code-block:: default

    import igraph as ig
    import matplotlib.pyplot as plt
    import random








.. GENERATED FROM PYTHON SOURCE LINES 15-18

First, we generate a randomized geometric graph with random vertex sizes. The
seed is set to the example is reproducible in our manual: you don't really
need it to understand the concepts.

.. GENERATED FROM PYTHON SOURCE LINES 18-21

.. code-block:: default

    random.seed(0)
    g = ig.Graph.GRG(50, 0.15)








.. GENERATED FROM PYTHON SOURCE LINES 22-24

Now we can cluster the graph into weakly connected components, i.e. subgraphs
that have no edges connecting them to one another:

.. GENERATED FROM PYTHON SOURCE LINES 24-26

.. code-block:: default

    components = g.connected_components(mode='weak')








.. GENERATED FROM PYTHON SOURCE LINES 27-28

Finally, we can visualize the distinct connected components of the graph:

.. GENERATED FROM PYTHON SOURCE LINES 28-39

.. code-block:: default

    fig, ax = plt.subplots()
    ig.plot(
        components,
        target=ax,
        palette=ig.RainbowPalette(),
        vertex_size=7,
        vertex_color=list(map(int, ig.rescale(components.membership, (0, 200), clamp=True))),
        edge_width=0.7
    )
    plt.show()




.. image-sg:: /tutorials/images/sphx_glr_connected_components_001.png
   :alt: connected components
   :srcset: /tutorials/images/sphx_glr_connected_components_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 40-45

.. note::

    We use the integers from 0 to 200 instead of 0 to 255 in our vertex
    colors, since 255 in the :class:`igraph.drawing.colors.RainbowPalette`
    corresponds to looping back to red. This gives us nicely distinct hues.


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.746 seconds)


.. _sphx_glr_download_tutorials_connected_components.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: connected_components.py <connected_components.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: connected_components.ipynb <connected_components.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
