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

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_tutorials_simplify.py>`
        to download the full example code.

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

.. _sphx_glr_tutorials_simplify.py:


========
Simplify
========

This example shows how to remove self loops and multiple edges using :meth:`igraph.GraphBase.simplify`.

.. GENERATED FROM PYTHON SOURCE LINES 8-11

.. code-block:: Python

    import igraph as ig
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 12-13

We start with a graph that includes loops and multiedges:

.. GENERATED FROM PYTHON SOURCE LINES 13-29

.. code-block:: Python

    g1 = ig.Graph([
        (0, 1),
        (1, 2),
        (2, 3),
        (3, 4),
        (4, 0),
        (0, 0),
        (1, 4),
        (1, 4),
        (0, 2),
        (2, 4),
        (2, 4),
        (2, 4),
        (3, 3)],
    )








.. GENERATED FROM PYTHON SOURCE LINES 30-34

To simplify the graph, we must remember that the function operates in place,
i.e. directly changes the graph that it is run on. So we need to first make a
copy of our graph, and then simplify that copy to keep the original graph
untouched:

.. GENERATED FROM PYTHON SOURCE LINES 34-37

.. code-block:: Python

    g2 = g1.copy()
    g2.simplify()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <igraph.Graph object at 0x70ef6c968850>



.. GENERATED FROM PYTHON SOURCE LINES 38-40

We can then proceed to plot both graphs to see the difference. First, let's
choose a consistent visual style:

.. GENERATED FROM PYTHON SOURCE LINES 40-46

.. code-block:: Python

    visual_style = {
        "vertex_color": "lightblue",
        "vertex_size": 20,
        "vertex_label": [0, 1, 2, 3, 4],
    }








.. GENERATED FROM PYTHON SOURCE LINES 47-49

And finally, let's plot them in twin axes, with rectangular frames around
each plot:

.. GENERATED FROM PYTHON SOURCE LINES 49-72

.. code-block:: Python

    fig, axs = plt.subplots(1, 2, sharex=True, sharey=True)
    ig.plot(
        g1,
        layout="circle",
        target=axs[0],
        **visual_style,
    )
    ig.plot(
        g2,
        layout="circle",
        target=axs[1],
        **visual_style,
    )
    axs[0].set_title('Multigraph...')
    axs[1].set_title('...simplified')
    # Draw rectangles around axes
    axs[0].add_patch(plt.Rectangle(
        (0, 0), 1, 1, fc='none', ec='k', lw=4, transform=axs[0].transAxes,
        ))
    axs[1].add_patch(plt.Rectangle(
        (0, 0), 1, 1, fc='none', ec='k', lw=4, transform=axs[1].transAxes,
        ))
    plt.show()



.. image-sg:: /tutorials/images/sphx_glr_simplify_001.png
   :alt: Multigraph..., ...simplified
   :srcset: /tutorials/images/sphx_glr_simplify_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_tutorials_simplify.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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