python - 如何从 igraph 正确使用 short_paths_dijkstra 函数?
问题描述
这是我尝试过的
def weighted_path(g, u, v):
x= g.shortest_paths_dijkstra(source=u, target=v, weights=True)
eff=1/x
return eff
我该如何正确使用它?我不知道如何正确使用 igraph,也找不到文档。
解决方案
假设您想要所有节点的节点效率,那么您可以这样做:
import numpy as np
from igraph import *
np.seterr(divide='ignore')
# Example using a random graph with 20 nodes
g = Graph.Erdos_Renyi(20,0.5)
# Assign weights on the edges. Here 1s everywhere
g.es["weight"] = np.ones(g.ecount())
def nodal_eff(g):
weights = g.es["weight"][:]
sp = (1.0 / np.array(g.shortest_paths_dijkstra(weights=weights)))
np.fill_diagonal(sp,0)
N=sp.shape[0]
ne= (1.0/(N-1)) * np.apply_along_axis(sum,0,sp)
return ne
eff = nodal_eff(g)
print(eff)
#[0.68421053 0.81578947 0.73684211 0.76315789 0.76315789 0.71052632
# 0.81578947 0.81578947 0.81578947 0.73684211 0.71052632 0.68421053
# 0.71052632 0.81578947 0.84210526 0.76315789 0.68421053 0.68421053
# 0.78947368 0.76315789]
推荐阅读
- spring-boot - curl post请求不适用于选项--http2,但是当我使用--http2-prior-knowledge时它可以正常工作
- regex - URL 在 IIS 8.5 中重写相同的主机不同的端口
- react-native - 每次渲染相同的dataRow太多
- java - 如果我在 onBindViewHolder 中迭代 JsonArray 而不是 Arraylist,性能会有什么不同吗?
- android - Play 商店不允许安装我的应用的两种风格
- xslt-1.0 - 如何为每组节点累积满足特定条件的值使用 XSLT-1.0
- python - 通过 python 中的 xlwings 调整 Excel 中的数字格式
- c++ - 创建 QWebSocket 对象时,它会显示 QSslSocket 警告消息
- c# - 如何打印或显示两个字符串列表的所有组合
- mongodb - 为什么我的 mongo 数据库被攻击者删除了?