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

.. only:: html

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

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

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

.. _sphx_glr_tutorials_minimum_spanning_trees.py:


.. _tutorials-minimum-spanning-trees:

======================
Minimum Spanning Trees
======================

This example shows how to generate a `minimum spanning tree <https://en.wikipedia.org/wiki/Minimum_spanning_tree>`_ from an input graph using :meth:`igraph.Graph.spanning_tree`. If you only need a regular spanning tree, check out :ref:`tutorials-spanning-trees`.

.. GENERATED FROM PYTHON SOURCE LINES 11-15

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 16-18

We start by generating a grid graph with random integer weights between 1 and
20:

.. GENERATED FROM PYTHON SOURCE LINES 18-22

.. code-block:: Python

    random.seed(0)
    g = ig.Graph.Lattice([5, 5], circular=False)
    g.es["weight"] = [random.randint(1, 20) for _ in g.es]








.. GENERATED FROM PYTHON SOURCE LINES 23-26

We can then compute a minimum spanning tree using
:meth:`igraph.Graph.spanning_tree`, making sure to pass in the randomly
generated weights.

.. GENERATED FROM PYTHON SOURCE LINES 26-28

.. code-block:: Python

    mst_edges = g.spanning_tree(weights=g.es["weight"], return_tree=False)








.. GENERATED FROM PYTHON SOURCE LINES 29-30

We can print out the minimum edge weight sum

.. GENERATED FROM PYTHON SOURCE LINES 30-34

.. code-block:: Python

    print("Minimum edge weight sum:", sum(g.es[mst_edges]["weight"]))

    # Minimum edge weight sum: 136





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

 .. code-block:: none

    Minimum edge weight sum: 201




.. GENERATED FROM PYTHON SOURCE LINES 35-37

Finally, we can plot the graph, highlighting the edges that are part of the
minimum spanning tree.

.. GENERATED FROM PYTHON SOURCE LINES 37-53

.. code-block:: Python

    g.es["color"] = "lightgray"
    g.es[mst_edges]["color"] = "midnightblue"
    g.es["width"] = 1.0
    g.es[mst_edges]["width"] = 3.0

    fig, ax = plt.subplots()
    ig.plot(
        g,
        target=ax,
        layout="grid",
        vertex_color="lightblue",
        edge_width=g.es["width"],
        edge_label=g.es["weight"],
        edge_background="white",
    )
    plt.show()



.. image-sg:: /tutorials/images/sphx_glr_minimum_spanning_trees_001.png
   :alt: minimum spanning trees
   :srcset: /tutorials/images/sphx_glr_minimum_spanning_trees_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_tutorials_minimum_spanning_trees.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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