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.connected_components#

connected_components(graph, /)#

Find the connected components in an undirected graph

A connected component is a subset of an undirected 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.connected_components(G)
[{0, 1, 2}, {3, 4}]

To get just the number of these components, see [number_connected_components].

For directed graphs, see [weakly_connected_components] and [strongly_connected_components].

Parameters:

graph (PyGraph) – An undirected graph to find the connected components in

Returns:

A list of sets of node indices where each set is a connected component of the graph

Return type:

list[set[int]]