networkx.connected_component_subgraphs¶
-
networkx.connected_component_subgraphs(G, copy=True)[source]¶ Generate connected components as subgraphs.
Parameters: G : NetworkX graph
An undirected graph.
copy: bool (default=True)
If True make a copy of the graph attributes
Returns: comp : generator
A generator of graphs, one for each connected component of G.
Raises: NetworkXNotImplemented:
If G is undirected.
See also
connected_components,strongly_connected_component_subgraphs,weakly_connected_component_subgraphsNotes
For undirected graphs only. Graph, node, and edge attributes are copied to the subgraphs by default.
Examples
>>> G = nx.path_graph(4) >>> G.add_edge(5,6) >>> graphs = list(nx.connected_component_subgraphs(G))
If you only want the largest connected component, it’s more efficient to use max instead of sort:
>>> Gc = max(nx.connected_component_subgraphs(G), key=len)