#NEWLINE# def shortest_path_length( G , source = None , target = None , weight = None ) : #NEWLINE# #INDENT# 'Compute shortest path lengths in the graph.\n\n    Parameters\n    ----------\n    G : NetworkX graph\n\n    source : node, optional\n        Starting node for path.\n        If not specified, compute shortest path lengths using all nodes as\n        source nodes.\n\n    target : node, optional\n        Ending node for path.\n        If not specified, compute shortest path lengths using all nodes as\n        target nodes.\n\n    weight : None or string, optional (default = None)\n        If None, every edge has weight/distance/cost 1.\n        If a string, use this edge attribute as the edge weight.\n        Any edge attribute not present defaults to 1.\n\n    Returns\n    -------\n    length: int or iterator\n        If the source and target are both specified, return the length of\n        the shortest path from the source to the target.\n\n        If only the source is specified, return a tuple\n        (target, shortest path length) iterator, where shortest path lengths\n        are the lengths of the shortest path from the source to one of the\n        targets.\n\n        If only the target is specified, return a tuple\n        (source, shortest path length) iterator, where shortest path lengths\n        are the lengths of the shortest path from one of the sources\n        to the target.\n\n        If neither the source nor target are specified, return a\n        (source, dictionary) iterator with dictionary keyed by target and\n        shortest path length as the key value.\n\n    Raises\n    ------\n    NetworkXNoPath\n        If no path exists between source and target.\n\n    Examples\n    --------\n    >>> G = nx.path_graph(5)\n    >>> nx.shortest_path_length(G, source=0, target=4)\n    4\n    >>> p = nx.shortest_path_length(G, source=0) # target not specified\n    >>> dict(p)[4]\n    4\n    >>> p = nx.shortest_path_length(G, target=4) # source not specified\n    >>> dict(p)[0]\n    4\n    >>> p = nx.shortest_path_length(G) # source,target not specified\n    >>> dict(p)[0][4]\n    4\n\n    Notes\n    -----\n    The length of the path is always 1 less than the number of nodes involved\n    in the path since the length measures the number of edges followed.\n\n    For digraphs this returns the shortest directed path length. To find path\n    lengths in the reverse direction use G.reverse(copy=False) first to flip\n    the edge orientation.\n\n    See Also\n    --------\n    all_pairs_shortest_path_length()\n    all_pairs_dijkstra_path_length()\n    single_source_shortest_path_length()\n    single_source_dijkstra_path_length()\n\n    ' #NEWLINE# if ( source is None ) : #NEWLINE# #INDENT# if ( target is None ) : #NEWLINE# #INDENT# if ( weight is None ) : #NEWLINE# #INDENT# paths = nx . all_pairs_shortest_path_length ( G ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# paths = nx . all_pairs_dijkstra_path_length ( G , weight = weight ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# with nx . utils . reversed ( G ) : #NEWLINE# #INDENT# if ( weight is None ) : #NEWLINE# #INDENT# path_length = nx . single_source_shortest_path_length #NEWLINE# paths = list ( path_length ( G , target ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# path_length = nx . single_source_dijkstra_path_length #NEWLINE# paths = path_length ( G , target , weight = weight ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( source not in G ) : #NEWLINE# #INDENT# raise nx . NodeNotFound ( 'Source {} not in G' . format ( source ) ) #NEWLINE# #UNINDENT# if ( target is None ) : #NEWLINE# #INDENT# if ( weight is None ) : #NEWLINE# #INDENT# paths = nx . single_source_shortest_path_length ( G , source ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# paths = nx . single_source_dijkstra_path_length ( p , source , weight = weight ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( weight is None ) : #NEWLINE# #INDENT# p = nx . bidirectional_shortest_path ( G , source , target ) #NEWLINE# paths = ( len ( p ) - 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# paths = nx . dijkstra_path_length ( G , source , target , weight ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return paths