如何从igraph正确使用short_paths_dijkstra函数?

时间:2019-06-13 10:07:41

标签: python igraph adjacency-matrix

这是我尝试过的

def weighted_path(g, u, v):
   x= g.shortest_paths_dijkstra(source=u, target=v, weights=True)
   eff=1/x
   return eff

如何正确使用它?我不知道如何正确使用igraph,也找不到真正的文档。

1 个答案:

答案 0 :(得分:1)

假设您想要所有节点的节点效率,则可以执行以下操作:

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]