networkx.algorithms.weakly_connected_component_subgraphs¶
-
networkx.algorithms.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 : generator
A generator of graphs, one for each weakly connected component of G.
Raises: NetworkXNotImplemented:
If G is undirected.
See also
weakly_connected_components,strongly_connected_component_subgraphs,connected_component_subgraphsNotes
For directed graphs only. Graph, node, and edge attributes are copied to the subgraphs by default.
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)