Networkx 中节点的组件数

Number of components of a node in Networkx

如何计算网络中给定节点周围的连通分量数?

示例:假设A连接到B。A也连接到C和D,并且C和D也相互连接。在这种情况下,节点 A 有 2 个连通分量,包括 [B] 和 [C,D]。

我相信我现在明白你的意图了。

让我们以帕吉特的佛罗伦萨家庭为榜样。

这里,Medici节点的邻居中,只有Ridolfi和Tornabuoni互为邻居。

# Setup
import networkx as nx
G = nx.florentine_families_graph()  # Or whatever else your graph is.

# Computation
node_id = 'Medici'  # Your nodes are probably identified by a number.
ego = nx.ego_graph(G, n=node_id, center=False)
components = nx.connected_components(ego)
for c in components:
    print(c)
# {'Acciaiuoli'}
# {'Ridolfi', 'Tornabuoni'}
# {'Albizzi'}
# {'Salviati'}
# {'Barbadori'}

自我图是节点 n 的所有直接邻居。 center=False 从该图中排除了 n。从那里,我们找到了组件。