连通分量#

connected_components(G)[源]#

生成连通分量。

参数:
GNetworkX 图

一个无向图

返回:
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)]
----

其他后端实现了此函数

cugraph : GPU加速后端。