def johnson(G, weight='weight'):
 if (not nx.is_weighted(G, weight=weight)):
  raise nx.NetworkXError('Graph is not weighted.')
 dist = {v: 0 for v in G}
 pred = {v: [None] for v in G}
 weight = _weight_function(G, weight)
 dist_bellman = _bellman_ford(G, list(G), weight, pred=pred, dist=dist)
 scale = (lambda u, v: (dist_bellman[u] - dist_bellman[v]))
 new_weight = (lambda u, v, d: (weight(u, v, d) + scale(u, v)))
 def dist_path(v):
  paths = {v: [v]}
  _dijkstra(G, v, new_weight, paths=paths)
  return paths
 return {v: dist_path(v) for v in G}

def johnson ( G , weight = 'weight' ) : if ( not nx . is_weighted ( G , weight = weight ) ) : raise nx . NetworkXError ( 'Graph is not weighted.' ) dist = { v : 0 for v in G } pred = { v : [ None ] for v in G } weight = _weight_function ( G , weight ) dist_bellman = _bellman_ford ( G , list ( G ) , weight , pred = pred , dist = dist ) scale = ( lambda u , v : ( dist_bellman [ u ] - dist_bellman [ v ] ) ) new_weight = ( lambda u , v , d : ( weight ( u , v , d ) + scale ( u , v ) ) ) def dist_path ( v ) : paths = { v : [ v ] } _dijkstra ( G , v , new_weight , paths = paths ) return paths return { v : dist_path ( v ) for v in G }
uses johnsons algorithm to compute shortest paths .