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

.. only:: html

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

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

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

.. _sphx_glr_tutorials_visualize_cliques.py:


.. _tutorials-cliques:

============
Cliques
============

This example shows how to compute and visualize cliques of a graph using :meth:`igraph.GraphBase.cliques`.

.. GENERATED FROM PYTHON SOURCE LINES 11-15

.. code-block:: Python


    import igraph as ig
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 16-17

First, let's create a graph, for instance the famous karate club graph:

.. GENERATED FROM PYTHON SOURCE LINES 17-19

.. code-block:: Python

    g = ig.Graph.Famous("Zachary")








.. GENERATED FROM PYTHON SOURCE LINES 20-21

Computing cliques can be done as follows:

.. GENERATED FROM PYTHON SOURCE LINES 21-23

.. code-block:: Python

    cliques = g.cliques(4, 4)








.. GENERATED FROM PYTHON SOURCE LINES 24-26

We can plot the result of the computation. To make things a little more
interesting, we plot each clique highlighted in a separate axes:

.. GENERATED FROM PYTHON SOURCE LINES 26-41

.. code-block:: Python

    fig, axs = plt.subplots(3, 4)
    axs = axs.ravel()
    for clique, ax in zip(cliques, axs):
        ig.plot(
            ig.VertexCover(g, [clique]),
            mark_groups=True,
            palette=ig.RainbowPalette(),
            vertex_size=5,
            edge_width=0.5,
            target=ax,
        )
    plt.axis("off")
    plt.show()





.. image-sg:: /tutorials/images/sphx_glr_visualize_cliques_001.png
   :alt: visualize cliques
   :srcset: /tutorials/images/sphx_glr_visualize_cliques_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 42-46

Advanced: improving plotting style
----------------------------------
If you want a little more style, you can color the vertices/edges within each
clique to make them stand out:

.. GENERATED FROM PYTHON SOURCE LINES 46-70

.. code-block:: Python

    fig, axs = plt.subplots(3, 4)
    axs = axs.ravel()
    for clique, ax in zip(cliques, axs):
        # Color vertices yellow/red based on whether they are in this clique
        g.vs["color"] = "yellow"
        g.vs[clique]["color"] = "red"

        # Color edges black/red based on whether they are in this clique
        clique_edges = g.es.select(_within=clique)
        g.es["color"] = "black"
        clique_edges["color"] = "red"
        # also increase thickness of clique edges
        g.es["width"] = 0.3
        clique_edges["width"] = 1

        ig.plot(
            ig.VertexCover(g, [clique]),
            mark_groups=True,
            palette=ig.RainbowPalette(),
            vertex_size=5,
            target=ax,
        )
    plt.axis("off")
    plt.show()



.. image-sg:: /tutorials/images/sphx_glr_visualize_cliques_002.png
   :alt: visualize cliques
   :srcset: /tutorials/images/sphx_glr_visualize_cliques_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_tutorials_visualize_cliques.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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