networkx.strongly_connected_component_subgraphs

networkx.strongly_connected_component_subgraphs(G, copy=True)[source]

Generate strongly connected components as subgraphs.

Parameters:

G : NetworkX Graph

A directed graph.

copy : boolean, optional

if copy is True, Graph, node, and edge attributes are copied to the subgraphs.

Returns:

comp : generator of graphs

A generator of graphs, one for each strongly connected component of G.

Raises:

NetworkXNotImplemented:

If G is undirected.

Examples

Generate a sorted list of strongly connected components, largest first.

>>> G = nx.cycle_graph(4, create_using=nx.DiGraph())
>>> nx.add_cycle(G, [10, 11, 12])
>>> [len(Gc) for Gc in sorted(nx.strongly_connected_component_subgraphs(G),
...                         key=len, reverse=True)]
[4, 3]

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

>>> Gc = max(nx.strongly_connected_component_subgraphs(G), key=len)