Skip to content

Commit d118991

Browse files
committed
add method number_of_biconnected_components
1 parent f4adc25 commit d118991

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/sage/graphs/connectivity.pyx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Here is what the module can do:
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.
2525
:meth:`biconnected_components_subgraphs` | Return a list of biconnected components as graph objects.
26+
:meth:`number_of_biconnected_components` | Return the number of biconnected components.
2627
:meth:`is_cut_edge` | Check whether the input edge is a cut-edge or a bridge.
2728
:meth:`is_edge_cut` | Check whether the input edges form an edge cut.
2829
:meth:`is_cut_vertex` | Check whether the input vertex is a cut-vertex.
@@ -818,6 +819,70 @@ def biconnected_components_subgraphs(G):
818819
return [G.subgraph(c) for c in blocks_and_cut_vertices(G)[0]]
819820

820821

822+
def number_of_biconnected_components(G):
823+
r"""
824+
Return the number of biconnected components.
825+
826+
A biconnected component is a maximal subgraph on two or more vertices that
827+
is biconnected, i.e., removing any vertex does not disconnect it.
828+
829+
.. SEEALSO::
830+
831+
- :meth:`~sage.graphs.generic_graph.GenericGraph.blocks_and_cut_vertices`
832+
- :meth:`~Graph.is_biconnected`
833+
834+
EXAMPLES:
835+
836+
The disjoint union of cycles has as many biconnected components as the
837+
number of cycles::
838+
839+
sage: G = graphs.CycleGraph(5)
840+
sage: G.number_of_biconnected_components()
841+
1
842+
sage: (3 * G).number_of_biconnected_components()
843+
3
844+
845+
A block graph is a connected graph in which every biconnected component
846+
(block) is a clique. Hence its number of biconnected components is its
847+
number of blocks::
848+
849+
sage: number_of_blocks = randint(4, 10)
850+
sage: G = graphs.RandomBlockGraph(number_of_blocks, 5)
851+
sage: G.number_of_biconnected_components() == number_of_blocks
852+
True
853+
854+
By definition, an edge is a biconnected component. Hence, the number of
855+
biconnected components of a tree is its number of edges::
856+
857+
sage: T = graphs.RandomTree(randint(0, 10))
858+
sage: T.number_of_biconnected_components() == T.size()
859+
True
860+
861+
An isolated vertex is a block but not a biconnected component::
862+
863+
sage: G = Graph(3)
864+
sage: len(G.blocks_and_cut_vertices()[0])
865+
3
866+
sage: G.number_of_biconnected_components()
867+
0
868+
869+
TESTS:
870+
871+
An error is raised if the input is not a Sage graph::
872+
873+
sage: from sage.graphs.connectivity import number_of_biconnected_components
874+
sage: number_of_biconnected_components('I am not a graph')
875+
Traceback (most recent call last):
876+
...
877+
TypeError: the input must be a Sage graph
878+
"""
879+
from sage.graphs.generic_graph import GenericGraph
880+
if not isinstance(G, GenericGraph):
881+
raise TypeError("the input must be a Sage graph")
882+
883+
return len([c for c in G.blocks_and_cut_vertices()[0] if len(c) > 1])
884+
885+
821886
def is_edge_cut(G, edges):
822887
"""
823888
Check whether ``edges`` form an edge cut.

src/sage/graphs/generic_graph.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
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.
247247
:meth:`~GenericGraph.biconnected_components_subgraphs` | Return a list of biconnected components as graph objects.
248+
:meth:`~GenericGraph.number_of_biconnected_components` | Return the number of biconnected components.
248249
:meth:`~GenericGraph.is_cut_edge` | Check whether the input edge is a cut-edge or a bridge.
249250
:meth:`~GenericGraph.is_edge_cut` | Check whether the input edges form an edge cut.
250251
:meth:`~GenericGraph.is_cut_vertex` | Check whether the input vertex is a cut-vertex.
@@ -26005,6 +26006,7 @@ def is_self_complementary(self):
2600526006
is_cut_vertex,
2600626007
is_edge_cut,
2600726008
is_vertex_cut,
26009+
number_of_biconnected_components,
2600826010
vertex_connectivity,
2600926011
)
2601026012
from sage.graphs.distances_all_pairs import distances_distribution, szeged_index

0 commit comments

Comments
 (0)