def single_source_shortest_path(G, source, cutoff=None):
 if (source not in G):
  raise nx.NodeNotFound('Source {} not in G'.format(source))
 level = 0
 nextlevel = {source: 1}
 paths = {source: [source]}
 if (cutoff == 0):
  return paths
 while nextlevel:
  thislevel = nextlevel
  nextlevel = {}
  for v in thislevel:
   for w in G[v]:
    if (w not in paths):
     paths[w] = (paths[v] + [w])
     nextlevel[w] = 1
  level = (level + 1)
  if ((cutoff is not None) and (cutoff <= level)):
   break
 return paths

def single_source_shortest_path ( G , source , cutoff = None ) : if ( source not in G ) : raise nx . NodeNotFound ( 'Source {} not in G' . format ( source ) ) level = 0 nextlevel = { source : 1 } paths = { source : [ source ] } if ( cutoff == 0 ) : return paths while nextlevel : thislevel = nextlevel nextlevel = { } for v in thislevel : for w in G [ v ] : if ( w not in paths ) : paths [ w ] = ( paths [ v ] + [ w ] ) nextlevel [ w ] = 1 level = ( level + 1 ) if ( ( cutoff is not None ) and ( cutoff <= level ) ) : break return paths
compute shortest path between source and all other nodes reachable from source .