You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:Python NetworkX是否具备类似R语言igraph的closeness centrality及出入度中心性计算能力

关于NetworkX与igraph的Closeness Centrality对比及有向图计算方法

嘿,别担心,NetworkX其实完全支持计算有向图的in-closeness和out-closeness centrality,和R的igraph包功能是对齐的!

核心功能说明

NetworkX的closeness_centrality函数针对有向图场景,提供了明确的方向控制参数,完美覆盖你需要的两种中心性计算:

  • 计算out-closeness centrality:指定distance='out',基于节点的出边路径统计可达性
  • 计算in-closeness centrality:指定distance='in',基于节点的入边路径统计可达性

示例代码实现

import networkx as nx

# 构建一个示例有向图
G = nx.DiGraph()
G.add_edges_from([(1,2), (2,3), (3,1), (2,4), (4,3)])

# 计算out-closeness中心性
out_closeness = nx.closeness_centrality(G, distance='out')
print("Out-Closeness Centrality:", out_closeness)

# 计算in-closeness中心性
in_closeness = nx.closeness_centrality(G, distance='in')
print("In-Closeness Centrality:", in_closeness)

和igraph的功能对齐点

  • R的igraph包中,通过mode参数控制方向:mode="out"对应out-closeness,mode="in"对应in-closeness
  • NetworkX的distance参数和igraph的mode参数逻辑完全一致,都是用来限定有向图中的路径遍历方向

小提示

  • 该功能需要NetworkX 2.6及以上版本支持,如果你的版本较旧,建议先升级到最新稳定版
  • 对于孤立节点(无入边/出边),两种中心性的计算结果都会是0,这和igraph的处理逻辑保持一致

内容的提问来源于stack exchange,提问作者qfd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:26:17