微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

网络中有限分量的平均度

如何解决网络中有限分量的平均度

我有一个包含巨大组件和一些有限组件的网络。我需要计算有限分量(没有巨分量)的平均度数。 为此,我尝试从所有组件的列表中删除巨型组件并创建一个控制组件的子图

  components = nx.connected_components(G) #list of all the components
  GC = max(components,key=len )  #giant component
  finite_cluster= components.remove(max(components,key=len )) #remove the GC from the components
  subgraph_finite_cluster= G.subgraph(finite_cluster)

但是我收到子图的错误

我也尝试删除子图的一部分

  components = nx.connected_components(G) #list of all the components
  GC = max(components,key=len )) #remove the GC from the components

在这种情况下,错误

'generator' object has no attribute 'remove' 

那么如何将有限分量与巨型分量分开?

解决方法

您的基本问题是 nx.connected_components 返回组件的生成器不是组件列表。这是 range 的近亲。例如,range(10) 不返回整数 0-9 的列表;它返回一个迭代器,它将在十次连续调用时返回整数 0-9。

您的修复应该很简单:将生成器输出收集到一个列表中

components = list(nx.connected_components(G))

由于您忽略了发布 minimal,reproducible example (MRE),我无法为您进行测试。

,

按长度对组件进行排序并取不包括 GCC 的切片:

finite_cluster = sorted(nx.connected_components(G),key=len)[:-1]

注意:当然,如果您只有一个 GCC,这会起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。