.. 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 ` 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-15 .. code-block:: Python import igraph as ig import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 16-17 First, we generate a graph and assign a "capacity" to each edge: .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. 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 21-22 To find the max flow, we can simply run: .. GENERATED FROM PYTHON SOURCE LINES 22-31 .. 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 32-33 Finally, we can plot the directed graph to look at the situation: .. GENERATED FROM PYTHON SOURCE LINES 33-42 .. 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.061 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 ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: maxflow.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: maxflow.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_