max(nx.connected_component_subgraphs(),) 没有属性 'connected_component_subgraphs' 的错误

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

我正在尝试 运行 此代码:

graph = nx.Graph()

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

但我收到此错误消息:

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

我该如何解决这个问题?

connected_component_subgraphs() 已从 2.4 版中删除。

改为使用:

graph = nx.Graph()

connected_component_subgraphs = (graph.subgraph(c) for c in nx.connected_components(graph))

largest_subgraph = max(connected_component_subgraphs, key=len)

根据.