Return 使用 Networkx 的网络中未连接的节点岛

Return unconnected islands of nodes in a network with Networkx

我正在使用 NetworkX 分析传输连接的网络 G。可视化后,我看到有一些节点“孤岛”与网络没有任何连接。这些岛屿大多由 2 到 5 个节点组成。由于网络非常大,我正在寻找 returns 每个岛屿的命令,最好是在指示节点名称的数据结构中。 isolates(G) 命令仅 returns 个零度节点,但我对这些岛屿感兴趣。有相关命令吗?

查看 connected_components 函数

# Create three separate graphs and then compose them together.
import networkx as nx
G = nx.complete_graph(8)
G2 = nx.complete_graph(range(13, 15))
G3 = nx.complete_graph(range(16, 19))

G = nx.compose_all([G, G2, G3])
nx.draw(G)

使用connected_components():

list(nx.connected_components(G))
[{0, 1, 2, 3, 4, 5, 6, 7}, {13, 14}, {16, 17, 18}]
threshold = 6
[c for c in nx.connected_components(G) if len(c) < threshold]
[{13, 14}, {16, 17, 18}]

您也可以查看connected_components_subgraph