Warning

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

weakly_connected_component_subgraphs

weakly_connected_component_subgraphs(G, copy=True)[source]

Generate weakly connected components as subgraphs.

Parameters:
  • G (NetworkX graph) – A directed graph.
  • copy (bool (default=True)) – If True make a copy of the graph attributes
Returns:

comp – A generator of graphs, one for each weakly connected component of G.

Return type:

generator

Examples

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

>>> G = nx.path_graph(4, create_using=nx.DiGraph())
>>> G.add_path([10, 11, 12])
>>> [len(c) for c in sorted(nx.weakly_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.weakly_connected_component_subgraphs(G), key=len)

See also

strongly_connected_components(), connected_components()

Notes

For directed graphs only. Graph, node, and edge attributes are copied to the subgraphs by default.