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

.. only:: html

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

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

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

.. _sphx_glr_tutorials_bridges.py:


.. _tutorials-bridges:

========
Bridges
========

This example shows how to compute and visualize the `bridges <https://en.wikipedia.org/wiki/Bridge_(graph_theory)>`_ in a graph using :meth:`igraph.GraphBase.bridges`. For an example on articulation points instead, see :ref:`tutorials-articulation-points`.

.. 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-17

Let's start with a simple example. We begin by constructing a graph that
includes a few bridges:

.. GENERATED FROM PYTHON SOURCE LINES 17-21

.. code-block:: Python

    g = ig.Graph(14, [(0, 1), (1, 2), (2, 3), (0, 3), (0, 2), (1, 3), (3, 4),
            (4, 5), (5, 6), (6, 4), (6, 7), (7, 8), (7, 9), (9, 10), (10 ,11),
            (11 ,7), (7, 10), (8, 9), (8, 10), (5, 12), (12, 13)])








.. GENERATED FROM PYTHON SOURCE LINES 22-24

Then we can use a function to actually find the bridges, i.e. the edges that
connect different parts of the graph:

.. GENERATED FROM PYTHON SOURCE LINES 24-26

.. code-block:: Python

    bridges = g.bridges()








.. GENERATED FROM PYTHON SOURCE LINES 27-28

We set a separate color for those edges, to emphasize then in a plot:

.. GENERATED FROM PYTHON SOURCE LINES 28-33

.. code-block:: Python

    g.es["color"] = "gray"
    g.es[bridges]["color"] = "red"
    g.es["width"] = 0.8
    g.es[bridges]["width"] = 1.2








.. GENERATED FROM PYTHON SOURCE LINES 34-35

Finally, we plot the graph using that emphasis:

.. GENERATED FROM PYTHON SOURCE LINES 35-45

.. code-block:: Python

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




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





.. GENERATED FROM PYTHON SOURCE LINES 46-49

Advanced: Cutting Effect
--------------------------
Bridges are edges that when removed, will separate the graph into more components then they started with. To emphasise the removal of edges from the graph, we can add small "x" effect to each of the bridges by using edge labels.

.. GENERATED FROM PYTHON SOURCE LINES 51-52

As before, we begin by constructing the graph:

.. GENERATED FROM PYTHON SOURCE LINES 52-56

.. code-block:: Python

    g = ig.Graph(14, [(0, 1), (1, 2), (2, 3), (0, 3), (0, 2), (1, 3), (3, 4),
            (4, 5), (5, 6), (6, 4), (6, 7), (7, 8), (7, 9), (9, 10), (10 ,11),
            (11 ,7), (7, 10), (8, 9), (8, 10), (5, 12), (12, 13)])








.. GENERATED FROM PYTHON SOURCE LINES 57-59

We then find and set the color for the bridges, but this time we also set a
label for those edges:

.. GENERATED FROM PYTHON SOURCE LINES 59-67

.. code-block:: Python

    bridges = g.bridges()
    g.es["color"] = "gray"
    g.es[bridges]["color"] = "red"
    g.es["width"] = 0.8
    g.es[bridges]["width"] = 1.2
    g.es["label"] = ""
    g.es[bridges]["label"] = "x"








.. GENERATED FROM PYTHON SOURCE LINES 68-69

Finally, we can plot the graph:

.. GENERATED FROM PYTHON SOURCE LINES 69-82

.. code-block:: Python

    fig, ax = plt.subplots()
    ig.plot(
        g,
        target=ax,
        vertex_size=30,
        vertex_color="lightblue",
        vertex_label=range(g.vcount()),
        edge_background="#FFF0",  # transparent background color
        edge_align_label=True,  # make sure labels are aligned with the edge
        edge_label=g.es["label"],
        edge_label_color="red",
    )
    plt.show()



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






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

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


.. _sphx_glr_download_tutorials_bridges.py:

.. only:: html

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

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: bridges.zip <bridges.zip>`


.. only:: html

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

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