WEIGHTED SHORTEST PATHS
- weights in the dataset are the importance of the relationship,
- while shortest paths assume this is a cost
- so, use 1/weights as the weights of the shortest path computations weights = np.array(g_GC_u.es["weight"])
we need the number of hops on the shortest (weighted) path so, we need to:
- compute the shortest weighted paths (method get_shortest_paths)
- extract the # of hops
- get_shortest_paths() can be used on a SINGLE source vertices at a time
- so, we need to cycle over all elements of src, and compute the number of hops towards all target nodes (function n_hops)
def n_hops(s, t, w):
sps = g_GC_u.get_shortest_paths(s, t, output = 'epath', weights = w)
return [len(sps[i]) for i in range(0,len(sps))]
n_hops_w = []
for s in src:
n_hops_w += n_hops(s, trg, weights)
How can I apply this algorithm using the igraph library if my graph is weighted with negative arcs. For src I take 200 random nodes