.. 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 :ref:`Go to the end ` 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-14 .. code-block:: Python import igraph as ig import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 15-18 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 18-23 .. code-block:: Python 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 24-25 We can easily check that the graph is indeed bipartite: .. GENERATED FROM PYTHON SOURCE LINES 25-27 .. code-block:: Python assert g.is_bipartite() .. GENERATED FROM PYTHON SOURCE LINES 28-29 Now can can compute the maximum bipartite matching: .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python matching = g.maximum_bipartite_matching() .. GENERATED FROM PYTHON SOURCE LINES 32-33 It's easy to print matching pairs of vertices .. GENERATED FROM PYTHON SOURCE LINES 33-41 .. code-block:: Python 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 .. 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 42-44 Finally, we can plot the bipartite graph, highlighting the edges connecting maximal matches by a red color: .. GENERATED FROM PYTHON SOURCE LINES 44-55 .. code-block:: Python 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 .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.079 seconds) .. _sphx_glr_download_tutorials_bipartite_matching.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: bipartite_matching.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: bipartite_matching.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: bipartite_matching.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_