number_connected_components#

number_connected_components(G)[source]#

Returns the number of connected components.

Parameters:
GNetworkX graph

An undirected graph.

Returns:
ninteger

Number of connected components

Raises:
NetworkXNotImplemented

If G is directed.

Notes

For undirected graphs only.

Examples

>>> G = nx.Graph([(0, 1), (1, 2), (5, 6), (3, 4)])
>>> nx.number_connected_components(G)
3
----

Additional backends implement this function

cugraph : GPU-accelerated backend.