Skip to content

Commit 7d5b227

Browse files
committed
add method biconnected_components
1 parent d118991 commit 7d5b227

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/sage/graphs/connectivity.pyx

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Here is what the module can do:
2222
:meth:`connected_components_sizes` | Return the sizes of the connected components as a list.
2323
:meth:`blocks_and_cut_vertices` | Return the blocks and cut vertices of the graph.
2424
:meth:`blocks_and_cuts_tree` | Return the blocks-and-cuts tree of the graph.
25+
:meth:`biconnected_components` | Return the list of biconnected components.
2526
:meth:`biconnected_components_subgraphs` | Return a list of biconnected components as graph objects.
2627
:meth:`number_of_biconnected_components` | Return the number of biconnected components.
2728
:meth:`is_cut_edge` | Check whether the input edge is a cut-edge or a bridge.
@@ -779,12 +780,49 @@ def blocks_and_cuts_tree(G):
779780
return g
780781

781782

783+
def biconnected_components(G):
784+
r"""
785+
Return the list of biconnected components.
786+
787+
A biconnected component is a maximal subgraph on two or more vertices that
788+
is biconnected, i.e., removing any vertex does not disconnect it.
789+
790+
INPUT:
791+
792+
- ``G`` -- the input graph
793+
794+
EXAMPLES::
795+
796+
sage: from sage.graphs.connectivity import biconnected_components
797+
sage: G = Graph({0: [1, 2], 1: [0, 2], 2: [0, 1, 3], 3: [2]})
798+
sage: sorted(len(b) for b in biconnected_components(G))
799+
[2, 3]
800+
sage: sorted(len(b) for b in biconnected_components(2 * G))
801+
[2, 2, 3, 3]
802+
803+
TESTS:
804+
805+
If ``G`` is not a Sage graph, an error is raised::
806+
807+
sage: from sage.graphs.connectivity import biconnected_components
808+
sage: biconnected_components('I am not a graph')
809+
Traceback (most recent call last):
810+
...
811+
TypeError: the input must be a Sage graph
812+
"""
813+
from sage.graphs.generic_graph import GenericGraph
814+
if not isinstance(G, GenericGraph):
815+
raise TypeError("the input must be a Sage graph")
816+
817+
return [b for b in blocks_and_cut_vertices(G)[0] if len(b) > 1]
818+
819+
782820
def biconnected_components_subgraphs(G):
783821
r"""
784822
Return a list of biconnected components as graph objects.
785823
786-
A biconnected component is a maximal subgraph that is biconnected, i.e.,
787-
removing any vertex does not disconnect it.
824+
A biconnected component is a maximal subgraph on two or more vertices that
825+
is biconnected, i.e., removing any vertex does not disconnect it.
788826
789827
INPUT:
790828
@@ -816,7 +854,7 @@ def biconnected_components_subgraphs(G):
816854
if not isinstance(G, GenericGraph):
817855
raise TypeError("the input must be a Sage graph")
818856

819-
return [G.subgraph(c) for c in blocks_and_cut_vertices(G)[0]]
857+
return [G.subgraph(c) for c in G.biconnected_components()]
820858

821859

822860
def number_of_biconnected_components(G):
@@ -880,7 +918,7 @@ def number_of_biconnected_components(G):
880918
if not isinstance(G, GenericGraph):
881919
raise TypeError("the input must be a Sage graph")
882920

883-
return len([c for c in G.blocks_and_cut_vertices()[0] if len(c) > 1])
921+
return len(G.biconnected_components())
884922

885923

886924
def is_edge_cut(G, edges):

src/sage/graphs/generic_graph.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
:meth:`~GenericGraph.connected_components_sizes` | Return the sizes of the connected components as a list.
245245
:meth:`~GenericGraph.blocks_and_cut_vertices` | Compute the blocks and cut vertices of the graph.
246246
:meth:`~GenericGraph.blocks_and_cuts_tree` | Compute the blocks-and-cuts tree of the graph.
247+
:meth:`~GenericGraph.biconnected_components` | Return the list of biconnected components.
247248
:meth:`~GenericGraph.biconnected_components_subgraphs` | Return a list of biconnected components as graph objects.
248249
:meth:`~GenericGraph.number_of_biconnected_components` | Return the number of biconnected components.
249250
:meth:`~GenericGraph.is_cut_edge` | Check whether the input edge is a cut-edge or a bridge.
@@ -25992,6 +25993,7 @@ def is_self_complementary(self):
2599225993
from sage.graphs.base.static_dense_graph import connected_subgraph_iterator
2599325994
from sage.graphs.base.static_sparse_graph import spectral_radius
2599425995
from sage.graphs.connectivity import (
25996+
biconnected_components,
2599525997
biconnected_components_subgraphs,
2599625998
blocks_and_cut_vertices,
2599725999
blocks_and_cuts_tree,

0 commit comments

Comments
 (0)