I have a network that has a bunch of connected components, and I would like to extract the first 100 connected components into a new graph. I know how to extract individual connected components and union the individual connected components together into a new graph.
However, I know there must be a better way then extract all 100 connected components and union-ing all these together.
# create empty graph
G = nx.Graph()
# add edges to graph representing disconnected components
G.add_edges_from([('a','b'),('b','c'),('a','c'),('d','e'),('d','f'),('d','g'),('h','j'),('j','k'),('h','k'),('m','n'),('n','o'),('o','p'),('p','m'),('n','p')])
# extract individual components (I would like a more efficient way to do this for the first 100 components)
G0 = nx.connected_component_subgraph(G)[0]
G1 = nx.connected_component_subgraph(G)[1]
G2 = nx.connected_component_subgraph(G)[2]
...
(with the actual network, I would like to extract the first 100 connected components and this is inefficient)
# union connected components together into a new graph
G_union = nx.union([G0,G1,G2,...])