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

.. only:: html

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

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

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

.. _sphx_glr_tutorials_generate_dag.py:


.. _tutorials-dag:

======================
Directed Acyclic Graph
======================

This example demonstrates how to create a random directed acyclic graph (DAG), which is useful in a number of contexts including for Git commit history.

.. GENERATED FROM PYTHON SOURCE LINES 11-17

.. code-block:: Python


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









.. GENERATED FROM PYTHON SOURCE LINES 18-19

First, we set a random seed for reproducibility.

.. GENERATED FROM PYTHON SOURCE LINES 19-21

.. code-block:: Python

    random.seed(0)








.. GENERATED FROM PYTHON SOURCE LINES 22-23

First, we generate a random undirected graph with a fixed number of edges, without loops.

.. GENERATED FROM PYTHON SOURCE LINES 23-25

.. code-block:: Python

    g = ig.Graph.Erdos_Renyi(n=15, m=30, directed=False, loops=False)








.. GENERATED FROM PYTHON SOURCE LINES 26-27

Then we convert it to a DAG *in place*. This method samples DAGs with a given number of edges and vertices uniformly.

.. GENERATED FROM PYTHON SOURCE LINES 27-29

.. code-block:: Python

    g.to_directed(mode="acyclic")








.. GENERATED FROM PYTHON SOURCE LINES 30-31

We can print out a summary of the DAG.

.. GENERATED FROM PYTHON SOURCE LINES 31-34

.. code-block:: Python

    ig.summary(g)






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

 .. code-block:: none

    IGRAPH D--- 15 30 -- 




.. GENERATED FROM PYTHON SOURCE LINES 35-36

Finally, we can plot the graph using the Sugiyama layout from :meth:`igraph.Graph.layout_sugiyama`:

.. GENERATED FROM PYTHON SOURCE LINES 36-47

.. code-block:: Python

    fig, ax = plt.subplots()
    ig.plot(
        g,
        target=ax,
        layout="sugiyama",
        vertex_size=15,
        vertex_color="grey",
        edge_color="#222",
        edge_width=1,
    )
    plt.show()



.. image-sg:: /tutorials/images/sphx_glr_generate_dag_001.png
   :alt: generate dag
   :srcset: /tutorials/images/sphx_glr_generate_dag_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_tutorials_generate_dag.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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