如何计算网络中给定节点周围连接组件的数量?
示例:假设 A 连接到 B。A 还连接到 C 和 D,并且 C 和 D 也相互连接。在这种情况下,节点 A 有 2 个连通分量,包括 [B] 和 [C,D]。
最佳答案
我相信我现在明白你的意图了。
让我们以帕 git 的佛罗伦萨家庭为模型。
这里,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'}
self 图是节点n
的所有直接邻居。 center=False
从该图表中排除 n
。从那里,我们找到组件。
关于python - Networkx 中节点的组件数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43549232/