Warning

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

networkx.algorithms.components.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

Raises:

NetworkXNotImplemented: – If G is undirected.

Examples

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

>>> G = nx.path_graph(4, create_using=nx.DiGraph())
>>> nx.add_path(G, [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)

Notes

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