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

.. code-block:: Python

    import igraph as ig
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 15-16

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

.. GENERATED FROM PYTHON SOURCE LINES 16-18

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 19-20

Computing cliques can be done as follows:

.. GENERATED FROM PYTHON SOURCE LINES 20-22

.. code-block:: Python

    cliques = g.cliques(4, 4)








.. GENERATED FROM PYTHON SOURCE LINES 23-25

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

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

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 44-68

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


.. only:: html

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

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