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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_tutorials_bipartite_matching.py>`
        to download the full example code

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

.. _sphx_glr_tutorials_bipartite_matching.py:


.. _tutorials-bipartite-matching:

==========================
Maximum Bipartite Matching
==========================

This example demonstrates an efficient way to find and visualise a maximum biparite matching using :meth:`igraph.Graph.maximum_bipartite_matching`.

.. GENERATED FROM PYTHON SOURCE LINES 10-13

.. code-block:: default

    import igraph as ig
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 14-17

First, we construct a bipartite graph, assigning:
 - nodes 0-4 to one side
 - nodes 5-8 to the other side

.. GENERATED FROM PYTHON SOURCE LINES 17-22

.. code-block:: default

    g = ig.Graph.Bipartite(
        [0, 0, 0, 0, 0, 1, 1, 1, 1],
        [(0, 5), (1, 6), (1, 7), (2, 5), (2, 8), (3, 6), (4, 5), (4, 6)]
    )








.. GENERATED FROM PYTHON SOURCE LINES 23-24

We can easily check that the graph is indeed bipartite:

.. GENERATED FROM PYTHON SOURCE LINES 24-26

.. code-block:: default

    assert g.is_bipartite()








.. GENERATED FROM PYTHON SOURCE LINES 27-28

Now can can compute the maximum bipartite matching:

.. GENERATED FROM PYTHON SOURCE LINES 28-30

.. code-block:: default

    matching = g.maximum_bipartite_matching()








.. GENERATED FROM PYTHON SOURCE LINES 31-32

It's easy to print matching pairs of vertices

.. GENERATED FROM PYTHON SOURCE LINES 32-40

.. code-block:: default

    matching_size = 0
    print("Matching is:")
    for i in range(5):
        print(f"{i} - {matching.match_of(i)}")
        if matching.is_matched(i):
            matching_size += 1
    print("Size of maximum matching is:", matching_size)





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

 Out:

 .. code-block:: none

    Matching is:
    0 - 5
    1 - 7
    2 - 8
    3 - 6
    4 - None
    Size of maximum matching is: 4




.. GENERATED FROM PYTHON SOURCE LINES 41-43

Finally, we can plot the bipartite graph, highlighting the edges connecting
maximal matches by a red color:

.. GENERATED FROM PYTHON SOURCE LINES 43-54

.. code-block:: default

    fig, ax = plt.subplots(figsize=(7, 3))
    ig.plot(
        g,
        target=ax,
        layout=g.layout_bipartite(),
        vertex_size=30,
        vertex_label=range(g.vcount()),
        vertex_color="lightblue",
        edge_width=[3 if e.target == matching.match_of(e.source) else 1.0 for e in g.es],
        edge_color=["red" if e.target == matching.match_of(e.source) else "black" for e in g.es]
    )



.. image-sg:: /tutorials/images/sphx_glr_bipartite_matching_001.png
   :alt: bipartite matching
   :srcset: /tutorials/images/sphx_glr_bipartite_matching_001.png
   :class: sphx-glr-single-img


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

 Out:

 .. code-block:: none


    <igraph.drawing.matplotlib.graph.GraphArtist object at 0x7f6080db2390>




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

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


.. _sphx_glr_download_tutorials_bipartite_matching.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

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