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

后端 未结 2 908
面向向阳花
面向向阳花 2020-12-09 19:27

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 o

相关标签:
2条回答
  • 2020-12-09 20:11

    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).

    0 讨论(0)
  • 2020-12-09 20:13

    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.

    0 讨论(0)
提交回复
热议问题