Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the giant component of a NetworkX graph?

I don't know if NetworkX recently tweaked one of the methods to be a generator instead of returning a list, but I'm looking for a good (rather, better) way to get the GC of a graph.

I have a working, but really inefficient-looking, snippet down:

# G = nx.Graph()
giant = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0]

Is there a cleaner way?

like image 966
Nick T Avatar asked Sep 29 '14 17:09

Nick T


People also ask

What is a giant component in a network?

A giant component is a connected component of a network that contains a significant proportion of the entire nodes in the network. Typically as the network expands the giant component will continue to have a significant fraction of the nodes.


2 Answers

In networkx 2.4, nx.connected_component_subgraphs() is deprecated, so the following should work:

Gcc = sorted(nx.connected_components(G), key=len, reverse=True)
G0 = G.subgraph(Gcc[0])

G0 is the giant component.

like image 121
Rakesh Chintha Avatar answered Oct 19 '22 03:10

Rakesh Chintha


In networkx 1.9, connected_components_subgraphs returns an iterator (instead of a sorted list). The values yielded by the iterator are not in sorted order. So to find the largest, use max:

giant = max(nx.connected_component_subgraphs(G), key=len)

Sorting is O(n log n). Taking the max is O(n).

like image 29
unutbu Avatar answered Oct 19 '22 03:10

unutbu