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

.. only:: html

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

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

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

.. _sphx_glr_tutorials_maxflow.py:


.. _tutorials-maxflow:

============
Maximum Flow
============

This example shows how to construct a max flow on a directed graph with edge capacities using :meth:`igraph.Graph.maxflow`.

.. 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, we generate a graph and assign a "capacity" to each edge:

.. GENERATED FROM PYTHON SOURCE LINES 16-23

.. code-block:: Python

    g = ig.Graph(
        6,
        [(3, 2), (3, 4), (2, 1), (4,1), (4, 5), (1, 0), (5, 0)],
        directed=True
    )
    g.es["capacity"] = [7, 8, 1, 2, 3, 4, 5]








.. GENERATED FROM PYTHON SOURCE LINES 24-25

To find the max flow, we can simply run:

.. GENERATED FROM PYTHON SOURCE LINES 25-34

.. code-block:: Python

    flow = g.maxflow(3, 0, capacity=g.es["capacity"])

    print("Max flow:", flow.value)
    print("Edge assignments:", flow.flow)

    # Output:
    # Max flow: 6.0
    # Edge assignments [1.0, 5.0, 1.0, 2.0, 3.0, 3.0, 3.0]





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

 .. code-block:: none

    Max flow: 6.0
    Edge assignments: [1.0, 5.0, 1.0, 2.0, 3.0, 3.0, 3.0]




.. GENERATED FROM PYTHON SOURCE LINES 35-36

Finally, we can plot the directed graph to look at the situation:

.. GENERATED FROM PYTHON SOURCE LINES 36-45

.. code-block:: Python

    fig, ax = plt.subplots()
    ig.plot(
        g,
        target=ax,
        layout="circle",
        vertex_label=range(g.vcount()),
        vertex_color="lightblue"
    )
    plt.show()



.. image-sg:: /tutorials/images/sphx_glr_maxflow_001.png
   :alt: maxflow
   :srcset: /tutorials/images/sphx_glr_maxflow_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_tutorials_maxflow.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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