
.. 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

        :ref:`Go to the end <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-15

.. code-block:: Python


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








.. GENERATED FROM PYTHON SOURCE LINES 16-19

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 19-22

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 23-25

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 25-27

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 28-29

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

.. GENERATED FROM PYTHON SOURCE LINES 29-40

.. code-block:: Python

    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 41-46

.. 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.634 seconds)


.. _sphx_glr_download_tutorials_connected_components.py:

.. only:: html

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

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

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

    .. 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-zip

      :download:`Download zipped: connected_components.zip <connected_components.zip>`


.. only:: html

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

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