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

.. only:: html

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

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

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

.. _sphx_glr_tutorials_erdos_renyi.py:


.. _tutorials-random:

=================
Erdős-Rényi Graph
=================

This example demonstrates how to generate `Erdős–Rényi graphs <https://en.wikipedia.org/wiki/Erd%C5%91s%E2%80%93R%C3%A9nyi_model>`_ using :meth:`igraph.GraphBase.Erdos_Renyi`. There are two variants of graphs:

- ``Erdos_Renyi(n, p)`` will generate a graph from the so-called :math:`G(n,p)` model where each edge between any two pair of nodes has an independent probability ``p`` of existing.
- ``Erdos_Renyi(n, m)`` will pick a graph uniformly at random out of all graphs with ``n`` nodes and ``m`` edges. This is referred to as the :math:`G(n,m)` model.

We generate two graphs of each, so we can confirm that our graph generator is truly random.

.. GENERATED FROM PYTHON SOURCE LINES 15-19

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 20-21

First, we set a random seed for reproducibility

.. GENERATED FROM PYTHON SOURCE LINES 21-23

.. code-block:: Python

    random.seed(0)








.. GENERATED FROM PYTHON SOURCE LINES 24-25

Then, we generate two :math:`G(n,p)` Erdős–Rényi graphs with identical parameters:

.. GENERATED FROM PYTHON SOURCE LINES 25-28

.. code-block:: Python

    g1 = ig.Graph.Erdos_Renyi(n=15, p=0.2, directed=False, loops=False)
    g2 = ig.Graph.Erdos_Renyi(n=15, p=0.2, directed=False, loops=False)








.. GENERATED FROM PYTHON SOURCE LINES 29-31

For comparison, we also generate two :math:`G(n,m)` Erdős–Rényi graphs with a fixed number
of edges:

.. GENERATED FROM PYTHON SOURCE LINES 31-34

.. code-block:: Python

    g3 = ig.Graph.Erdos_Renyi(n=20, m=35, directed=False, loops=False)
    g4 = ig.Graph.Erdos_Renyi(n=20, m=35, directed=False, loops=False)








.. GENERATED FROM PYTHON SOURCE LINES 35-36

We can print out summaries of each graph to verify their randomness

.. GENERATED FROM PYTHON SOURCE LINES 36-46

.. code-block:: Python

    ig.summary(g1)
    ig.summary(g2)
    ig.summary(g3)
    ig.summary(g4)

    # IGRAPH U--- 15 18 --
    # IGRAPH U--- 15 21 --
    # IGRAPH U--- 20 35 --
    # IGRAPH U--- 20 35 --





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

 .. code-block:: none

    IGRAPH U--- 15 23 -- 
    IGRAPH U--- 15 28 -- 
    IGRAPH U--- 20 35 -- 
    IGRAPH U--- 20 35 -- 




.. GENERATED FROM PYTHON SOURCE LINES 47-49

Finally, we can plot the graphs to illustrate their structures and
differences:

.. GENERATED FROM PYTHON SOURCE LINES 49-81

.. code-block:: Python

    fig, axs = plt.subplots(2, 2)
    # Probability
    ig.plot(
        g1,
        target=axs[0, 0],
        layout="circle",
        vertex_color="lightblue"
    )
    ig.plot(
        g2,
        target=axs[0, 1],
        layout="circle",
        vertex_color="lightblue"
    )
    axs[0, 0].set_ylabel('Probability')
    # N edges
    ig.plot(
        g3,
        target=axs[1, 0],
        layout="circle",
        vertex_color="lightblue",
        vertex_size=15
    )
    ig.plot(
        g4,
        target=axs[1, 1],
        layout="circle",
        vertex_color="lightblue",
        vertex_size=15
    )
    axs[1, 0].set_ylabel('N. edges')
    plt.show()



.. image-sg:: /tutorials/images/sphx_glr_erdos_renyi_001.png
   :alt: erdos renyi
   :srcset: /tutorials/images/sphx_glr_erdos_renyi_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_tutorials_erdos_renyi.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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