Generate weakly connected components as subgraphs.
- G (NetworkX graph) – A directed graph.
- copy (bool (default=True)) – If True make a copy of the graph attributes
comp – A generator of graphs, one for each weakly connected component of G.
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)
For directed graphs only. Graph, node, and edge attributes are copied to the subgraphs by default.