【问题标题】:Calculate the diameter and radius of a network which has multiple connected components计算具有多个连通分量的网络的直径和半径
【发布时间】:2022-01-15 20:20:22
【问题描述】:

我的网络有几个连接的组件。 我需要为它们中的每一个计算直径和半径。

我试图循环遍历它们中的每一个,但是出了点问题(错误:AttributeError: 'set' object has no attribute 'order')

N_comp=[len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)] # this should return the maximum number of connected components in the network
for i in range(N_comp):

# Calculate average diameter and average path length of connected components
print('Average diameter:{}'.format(nx.diameter(nx.connected_components(G), key=len))))
print('average path length: {}'.format(nx.average_shortest_path_length(nx.connected_components(G), key=len)))

【问题讨论】:

    标签: python networkx


    【解决方案1】:

    documentationconnected_components 返回“节点集的生成器,一个用于 G 的每个组件。”

    但这些不是子图。您需要在每组节点上获取G 的诱导子图。

    来自文档:“创建每个组件的诱导子图使用:S = [G.subgraph(c).copy() for c in nx.connected_components(G)]

    获得S 后,您可以对S 的元素进行所需的计算(这类似于我认为您试图在列表N_comp 中创建的内容)

    在您的代码中还有其他问题。在我看来,您已经在 N_comp (这是一组节点的列表)上设置了一个 for 循环。但是你从不使用你循环的东西,我没有看到任何尝试计算单个组件的直径然后平均它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多