connected_components#

connected_components(G)[源代码]#

生成连接的组件。

参数
G网络X图表

无向图

返回
comp集合的生成元

一个节点集的生成器,每个节点集对应于G的每个分量。

加薪
NetworkXNotImplemented

如果G被指示。

笔记

仅适用于无向图。

实例

生成已连接组件的排序列表,最大的一个。

>>> G = nx.path_graph(4)
>>> nx.add_path(G, [10, 11, 12])
>>> [len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)]
[4, 3]

如果只需要最大的连接组件,那么使用max而不是sort会更有效。

>>> largest_cc = max(nx.connected_components(G), key=len)

要创建每个组件的诱导子图,请使用:

>>> S = [G.subgraph(c).copy() for c in nx.connected_components(G)]