如何正确使用 igraph 中的 short_paths_dijkstra 函数?

How do I use the short_paths_dijkstra function properly from igraph?

这是我试过的

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]