NetworkX最大的组件不再起作用? [英] NetworkX largest component no longer working?

查看:243
本文介绍了NetworkX最大的组件不再起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据networkx文档,connected_component_subgraphs(G)返回所有组件的排序列表.因此,第一个组件应该是最大的组件.

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.

但是,当我尝试使用文档页面上的示例代码获取图G的最大成分时

However, when I try to get the largest component of a graph G using the example code on the documentation page

G=nx.path_graph(4)
G.add_edge(5,6)
H=nx.connected_component_subgraphs(G)[0]

我知道

TypeError: 'generator' object has no attribute '__getitem__'

它以前可以在我的其他计算机上使用较早版本的networkx(我认为是1.7,不是100%确定)

It used to work on my other computer with earlier versions of networkx (1.7 I think, not 100% sure)

现在,我使用的是另一台计算机,其中包含python 2.7.7和networkx 1.9.是版本问题吗?

Now I am using a different computer with python 2.7.7 and networkx 1.9. Is it a version problem?

我自己编写了一个带有几行代码的小函数,以查找最大的组件,只是想知道为什么会出现此错误.

I have written a small function with a couple lines myself to find the largest component, just wondering why this error came out.

顺便说一句,我可以通过将生成器对象转换为列表来获取组件.

BTW, I can get the components by converting the generator object to a list.

components = [comp for comp in nx.connected_components(G)]

但是该列表未按文档中所述的组件大小排序.

But the list is not sorted by component size as stated in the documentation.

示例:

G = nx.Graph()
G.add_edges_from([(1,2),(1,3),(4,5)])
G.add_nodes_from(range(6,20))
components = [comp for comp in nx.connected_components(G)]
component_size = [len(comp) for comp in components]
print G.number_of_nodes(), G.number_of_edges(), component_size

G = nx.Graph()
G.add_edges_from([(1000,2000),(1000,3000),(4000,5000)])
G.add_nodes_from(range(6,20))
components = [comp for comp in nx.connected_components(G)]
component_size = [len(comp) for comp in components]
print G.number_of_nodes(), G.number_of_edges(), component_size

输出:

19 3 [3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
19 3 [2, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

看起来像当节点名称为大数字并且有一堆单个节点时,返回的子图未正确排序

looks like when the node names are large numbers and when there are a bunch of single nodes, the returned subgraphs are not sorted properly

推荐答案

networkx-1.9文档在此处

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]]

>>> sorted(nx.connected_component_subgraphs(G), key = len, reverse=True)

这篇关于NetworkX最大的组件不再起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆