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

def single_source_shortest_path_length ( G , source , cutoff = None ) : if ( source not in G ) : raise nx . NodeNotFound ( 'Source {} is not in G' . format ( source ) ) seen = { } level = 0 nextlevel = { source : 1 } while nextlevel : thislevel = nextlevel nextlevel = { } for v in thislevel : if ( v not in seen ) : seen [ v ] = level nextlevel . update ( G [ v ] ) ( yield ( v , level ) ) if ( ( cutoff is not None ) and ( cutoff <= level ) ) : break level = ( level + 1 ) del seen
return the shortest path length from source to all reachable nodes .