gpt4 book ai didi

python - 如何从 igraph 正确使用 Short_paths_dijkstra 函数?

转载 作者:行者123 更新时间:2023-12-01 07:43:20 26 4
gpt4 key购买 nike

这是我尝试过的

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]

关于python - 如何从 igraph 正确使用 Short_paths_dijkstra 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com