NetworkX largest component no longer working?

前端 未结 2 351
Happy的楠姐
Happy的楠姐 2021-01-04 06:14

according to networkx documentation, connected_component_subgraphs(G) returns a sorted list of all components. Thus the very first one should be the largest component.

相关标签:
2条回答
  • 2021-01-04 06:22

    The networkx-1.9 documentation is here http://networkx.github.io/documentation/networkx-1.9/reference/generated/networkx.algorithms.components.connected.connected_components.html#networkx.algorithms.components.connected.connected_components

    The interface was changed to return a generator (as you figured out). The example in the documentation shows how to do what you ask.

    Generate a sorted list of connected components, largest first.

    >> G = nx.path_graph(4)
    >>> G.add_path([10, 11, 12])
    >>> sorted(nx.connected_components(G), key = len, reverse=True)
    [[0, 1, 2, 3], [10, 11, 12]]
    

    or

    >>> sorted(nx.connected_component_subgraphs(G), key = len, reverse=True)
    
    0 讨论(0)
  • 2021-01-04 06:25

    As for version 2.4: nx.connected_component_subgraphs(G) is removed.

    Instead to get the same result use:

    connected_subgraphs = [G.subgraph(cc) for cc in nx.connected_components(G)]
    

    And to get the giant component:

    Gcc = max(nx.connected_components(G), key=len)
    giantC = G.subgraph(Gcc)
    
    0 讨论(0)
提交回复
热议问题