Note

This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.

rustworkx.number_connected_components#

number_connected_components(graph, /)#

Find the number of connected components in an undirected graph

A connected component is a subset of the graph where there is a path between any two vertices in that subset, and which is connected to no additional vertices in the graph.

>>> G = rx.PyGraph()
>>> G.extend_from_edge_list([(0, 1), (1, 2), (3, 4)])
>>> rx.number_connected_components(G)
2

To get these components, see [connected_components].

If rx.number_connected_components(G) == 1, then rx.is_connected(G) is True.

For directed graphs, see [number_weakly_connected_components].

Parameters:

graph (PyGraph) – The undirected graph to find the number of connected components in

Returns:

The number of connected components in the graph

Return type:

int