Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

# connected_components¶

connected_components(G)[source]

Generate connected components.

Parameters: G (NetworkX graph) – An undirected graph comp – A generator of sets of nodes, one for each component of G. generator of sets

Examples

Generate a sorted list of connected components, largest first.

>>> G = nx.path_graph(4)
>>> [len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)]
[4, 3]


If you only want the largest connected component, it’s more efficient to use max instead of sort.

>>> largest_cc = max(nx.connected_components(G), key=len)


strongly_connected_components()