1
votes

Sorry if this is rough: it's my first post to Stackoverflow! I am sorry in advance for not posting code, but nothing I am doing is complex (and maybe that's the problem), so describing should work. I also apologize if I am bad at describing issues because I am new to Python; I am not really sure how to recreate an example without data already :(

When using NetworkX, I frequently run large, undirected graphs (let's call them G) with thousands of nodes after importing the data in from pandas. The VAST majority of nodes only have one or two edges, which are just noise to me. It's clusters with lots of nodes that interest me, and that's actually the minority.

So I will then run the nx.connected_components command to make a long list of all the subgraph sets contained with G, review the top results, and print the individual subgraphs that interest me one at a time.

As such, when I get my generator list/dictionary of all of the connected component subgraphs (which is typically very long), I will also generally just look at the first 50-100 results. Because these tend to have what I am looking for.

I tried nx.connected_component_subgraphs, but, there are so many I don't need that way that it's almost as bad as just visualizing the whole network at once.

So in short: how can I take the generator/list of sets that nx.connected_components gives me--which I then shorten to the top 50--and make that into a new graph?

I tried converting the output of nx.component_components to a list, but it is all sets.

No error messages.

1
Welcome to StackOverflow. I think @Andreas comment is not quite warranted but I do think that a minimal working example (MWE) with some fake data would help here. Demonstrating the problem is just one reason for an MWE. IMO more importantly, it shows your own assumptions regarding the problem and it forces you to specify the shape of a solution. For example, in your case, the title says that you are struggling with a drawing problem, whereas the text suggests (to me) that you would simply like to fuse the components into one network. Few people will attempt to solve an ill specified problem. - Paul Brodersen
Thank you both for the input; it's embarrassing to admit but I had trouble making my own MWE, and I am aware of everything you stated @Andreas. I got the solution In needed thanks to the pro below, and now I also learned how to make my own examples moving forward. Sorry for posting when I am so new, but, I had tried so many things on my own that I just didn't know where to begin here and did my best to describe the problem. - thegreatjon

1 Answers

1
votes

One approach could be something like the following:

First find all components but the N largest ones

small_components = sorted(nx.connected_components(G), key=len)[:-N]

Then, remove from G all vertices belonging to one of these components:

G.remove_nodes_from(itertools.chain.from_iterable(small_components))

Here's an example where we keep only the two largest components of a given graph:

In [31]: G = nx.Graph()
In [32]: G.add_edges_from([(1, 2), (2, 3), (3, 4), (5, 6), (7, 8), (8, 9)])
In [33]: small_components = sorted(nx.connected_components(G), key=len)[:-2]
In [34]: small_components
Out[34]: [{5, 6}]
In [35]: G.remove_nodes_from(itertools.chain.from_iterable(small_components))
In [36]: G.nodes()
Out[36]: NodeView((1, 2, 3, 4, 7, 8, 9))