如何获得NetworkX图的巨型组件? [英] How do I get the giant component of a NetworkX graph?

查看:82
本文介绍了如何获得NetworkX图的巨型组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道NetworkX最近是否将其中一种方法调整为生成器而不是返回列表,但我正在寻找一种更好(更好)的方法来获取图形的GC.

I don't know if NetworkX recently tweaked one of the methods to be a generator instead of returning a list, but I'm looking for a good (rather, better) way to get the GC of a graph.

我有一段工作正常但看上去效率低下的摘要:

I have a working, but really inefficient-looking, snippet down:

# G = nx.Graph()
giant = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0]

有没有更清洁的方法?

推荐答案

在networkx 1.9中,connected_components_subgraphs返回一个迭代器(而不是排序列表).迭代器产生的值是未按排序顺序.因此,要找到最大的对象,请使用max:

In networkx 1.9, connected_components_subgraphs returns an iterator (instead of a sorted list). The values yielded by the iterator are not in sorted order. So to find the largest, use max:

giant = max(nx.connected_component_subgraphs(G), key=len)

排序为O(n log n).取最大值为O(n).

Sorting is O(n log n). Taking the max is O(n).

这篇关于如何获得NetworkX图的巨型组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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