def bellman_ford_predecessor_and_distance(G, source, target=None, cutoff=None, weight='weight'):
 if (source not in G):
  raise nx.NodeNotFound(('Node %s is not found in the graph' % source))
 weight = _weight_function(G, weight)
 if any(((weight(u, v, d) < 0) for (u, v, d) in G.selfloop_edges(data=True))):
  raise nx.NetworkXUnbounded('Negative cost cycle detected.')
 dist = {source: 0}
 pred = {source: [None]}
 if (len(G) == 1):
  return (pred, dist)
 weight = _weight_function(G, weight)
 return (pred, _bellman_ford(G, [source], weight, pred=pred, dist=dist, cutoff=cutoff, target=target))

def bellman_ford_predecessor_and_distance ( G , source , target = None , cutoff = None , weight = 'weight' ) : if ( source not in G ) : raise nx . NodeNotFound ( ( 'Node %s is not found in the graph' % source ) ) weight = _weight_function ( G , weight ) if any ( ( ( weight ( u , v , d ) < 0 ) for ( u , v , d ) in G . selfloop_edges ( data = True ) ) ) : raise nx . NetworkXUnbounded ( 'Negative cost cycle detected.' ) dist = { source : 0 } pred = { source : [ None ] } if ( len ( G ) == 1 ) : return ( pred , dist ) weight = _weight_function ( G , weight ) return ( pred , _bellman_ford ( G , [ source ] , weight , pred = pred , dist = dist , cutoff = cutoff , target = target ) )
compute shortest path lengths and predecessors on shortest paths in weighted graphs .