Search code examples
pythonnetworkxtweepy

Error for max(nx.connected_component_subgraphs(),) no attribute 'connected_component_subgraphs'


I am trying to run this code:

graph = nx.Graph()

largest_subgraph = max(nx.connected_component_subgraphs(graph), key=len)

But I am getting this error message:

AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs'

How do I get around that?


Solution

  • connected_component_subgraphs() has been removed from version 2.4.

    Instead, use this:

    graph = nx.Graph()
    
    connected_component_subgraphs = (graph.subgraph(c) for c in nx.connected_components(graph))
    
    largest_subgraph = max(connected_component_subgraphs, key=len)
    

    according to this answer.