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

.. only:: html

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

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

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

.. _sphx_glr_tutorials_complement.py:


.. _tutorials-complement:

================
Complement
================

This example shows how to generate the `complement graph <https://en.wikipedia.org/wiki/Complement_graph>`_ of a graph (sometimes known as the anti-graph) using :meth:`igraph.GraphBase.complementer`.

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

First, we generate a random graph

.. GENERATED FROM PYTHON SOURCE LINES 17-20

.. code-block:: Python

    random.seed(0)
    g1 = ig.Graph.Erdos_Renyi(n=10, p=0.5)








.. GENERATED FROM PYTHON SOURCE LINES 21-25

.. note::
    We set the random seed to ensure the graph comes out exactly the same
    each time in the gallery. You don't need to do that if you're exploring
    really random graphs ;-)

.. GENERATED FROM PYTHON SOURCE LINES 27-28

Then we generate the complement graph:

.. GENERATED FROM PYTHON SOURCE LINES 28-30

.. code-block:: Python

    g2 = g1.complementer(loops=False)








.. GENERATED FROM PYTHON SOURCE LINES 31-35

The union graph of the two is of course the full graph, i.e. a graph with
edges connecting all vertices to all other vertices. Because we decided to
ignore loops (aka self-edges) in the complementer, the full graph does not
include loops either.

.. GENERATED FROM PYTHON SOURCE LINES 35-37

.. code-block:: Python

    g_full = g1 | g2








.. GENERATED FROM PYTHON SOURCE LINES 38-40

In case there was any doubt, the complement of the full graph is an
empty graph, with the same vertices but no edges:

.. GENERATED FROM PYTHON SOURCE LINES 40-42

.. code-block:: Python

    g_empty = g_full.complementer(loops=False)








.. GENERATED FROM PYTHON SOURCE LINES 43-46

To demonstrate these concepts more clearly, here's a layout of each of the
four graphs we discussed (input, complement, union/full, complement of
union/empty):

.. GENERATED FROM PYTHON SOURCE LINES 46-77

.. code-block:: Python

    fig, axs = plt.subplots(2, 2)
    ig.plot(
        g1,
        target=axs[0, 0],
        layout="circle",
        vertex_color="black",
    )
    axs[0, 0].set_title("Original graph")
    ig.plot(
        g2,
        target=axs[0, 1],
        layout="circle",
        vertex_color="black",
    )
    axs[0, 1].set_title("Complement graph")

    ig.plot(
        g_full,
        target=axs[1, 0],
        layout="circle",
        vertex_color="black",
    )
    axs[1, 0].set_title("Union graph")
    ig.plot(
        g_empty,
        target=axs[1, 1],
        layout="circle",
        vertex_color="black",
    )
    axs[1, 1].set_title("Complement of union graph")
    plt.show()



.. image-sg:: /tutorials/images/sphx_glr_complement_001.png
   :alt: Original graph, Complement graph, Union graph, Complement of union graph
   :srcset: /tutorials/images/sphx_glr_complement_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_tutorials_complement.py:

.. only:: html

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

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

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


.. only:: html

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

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