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

.. code-block:: Python

    import igraph as ig
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 14-16

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

.. GENERATED FROM PYTHON SOURCE LINES 16-20

.. 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 21-23

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 23-25

.. code-block:: Python

    bridges = g.bridges()








.. GENERATED FROM PYTHON SOURCE LINES 26-27

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

.. GENERATED FROM PYTHON SOURCE LINES 27-32

.. 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 33-34

Finally, we plot the graph using that emphasis:

.. GENERATED FROM PYTHON SOURCE LINES 34-44

.. 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 45-48

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 50-51

As before, we begin by constructing the graph:

.. GENERATED FROM PYTHON SOURCE LINES 51-55

.. 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 56-58

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 58-66

.. 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 67-68

Finally, we can plot the graph:

.. GENERATED FROM PYTHON SOURCE LINES 68-81

.. 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.450 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>`


.. only:: html

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

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