【问题标题】:Return unconnected islands of nodes in a network with Networkx使用 Networkx 返回网络中未连接的节点岛
【发布时间】:2021-11-18 01:16:33
【问题描述】:

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

【问题讨论】:

  • 快速提问 - 图表是定向的吗?这有点影响答案。

标签: python networkx isolation


【解决方案1】:

查看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

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多