Randy Olson - 2 months ago 14

Python Question

I'm using Python's NetworkX package to calculate a bunch of network statistics for networks of varying size. I'm sweeping an independent parameter that systematically prunes edges, so sometimes a small network will become disconnected from the main network. Is there an easy way to detect and remove those smaller disconnected networks in NetworkX?

Answer

Sorin is correct. The function is called `connected_component_subgraphs`

in NetworkX.

Here's some code that finds the largest network in a NetworkX graph:

```
cur_graph = # whatever graph you're working with
if not nx.is_connected(cur_graph):
# get a list of unconnected networks
sub_graphs = nx.connected_component_subgraphs(cur_graph)
main_graph = sub_graphs[0]
# find the largest network in that list
for sg in sub_graphs:
if len(sg.nodes()) > len(main_graph.nodes()):
main_graph = sg
cur_graph = main_graph
```