Thanks to @CorleyBrigman. The solution is to create a new Graph W
from G
, replacing the colours of G
with weights you desire. Then you can efficiently use the nx.shortest_path
and nx.shortest_path_length
with their typically fast runtimes.
In [23]:
def update_weights(G, weights_dict):
W = nx.DiGraph()
for m in G.nodes():
for n in G[m].iterkeys():
relation = G[m][n]['rel']
weight = weights_dict[relation]
W.add_edge(m, n, rel=weights_dict[relation])
return W
In [41]:
weights_dict_test = {'notational_hiearchy':50,
'see_reference':0.6,
'class_elsewhere':1}
In [42]:
W = update_weights(G, weights_dict_test)
In [43]:
print len(W)
print len(G)
43241
43241
In [45]:
nx.shortest_path_length(W, '394.1', '341.33',weight='rel')
Out[45]:
52.2