
\c           @   s   d  Z  d d l Z d j d d g  Z d d d d	 d
 g Z d   Z d d d d  Z d d d d  Z	 d d  Z
 d d  Z d S(   s  
Compute the shortest paths and path lengths between nodes in the graph.

These algorithms work with undirected and directed graphs.

For directed graphs the paths can be computed in the reverse
order by first flipping the edge orientation using R=G.reverse(copy=False).

iNs   
s%   Aric Hagberg <aric.hagberg@gmail.com>s+   Sérgio Nery Simões <sergionery@gmail.com>t   shortest_patht   all_shortest_pathst   shortest_path_lengtht   average_shortest_path_lengtht   has_pathc         C   s5   y t  j |  | |  } Wn t  j k
 r0 t SXt S(   s   Return True if G has a path from source to target, False otherwise.

    Parameters
    ----------
    G : NetworkX graph

    source : node
       Starting node for path

    target : node
       Ending node for path
    (   t   nxR    t   NetworkXNoPatht   Falset   True(   t   Gt   sourcet   targett   sp(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/generic.pyR      s
    c         C   s   | d k r` | d k rN | d k r6 t j |   } q] t j |  d | } q t j d   n | d k r | d k r t j |  |  } q t j |  | d | } n< | d k r t j |  | |  } n t j |  | | |  } | S(   s  Compute shortest paths in the graph.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Starting node for path.
       If not specified compute shortest paths for all connected node pairs.

    target : node, optional
       Ending node for path.
       If not specified compute shortest paths for every node reachable
       from the source.

    weight : None or string, optional (default = None)
       If None, every edge has weight/distance/cost 1.
       If a string, use this edge attribute as the edge weight.
       Any edge attribute not present defaults to 1.

    Returns
    -------
    path: list or dictionary
        If the source and target are both specified return a single list
        of nodes in a shortest path.
        If only the source is specified return a dictionary keyed by
        targets with a list of nodes in a shortest path.
        If neither the source or target is specified return a dictionary
        of dictionaries with path[source][target]=[list of nodes in path].

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> print(nx.shortest_path(G,source=0,target=4))
    [0, 1, 2, 3, 4]
    >>> p=nx.shortest_path(G,source=0) # target not specified
    >>> p[4]
    [0, 1, 2, 3, 4]
    >>> p=nx.shortest_path(G) # source,target not specified
    >>> p[0][4]
    [0, 1, 2, 3, 4]

    Notes
    -----
    There may be more than one shortest path between a source and target.
    This returns only one of them.

    For digraphs this returns a shortest directed path.
    To find paths in the reverse direction first use G.reverse(copy=False)
    to flip the edge orientation.

    See Also
    --------
    all_pairs_shortest_path()
    all_pairs_dijkstra_path()
    single_source_shortest_path()
    single_source_dijkstra_path()
    t   weights%   Target given but no source specified.N(	   t   NoneR   t   all_pairs_shortest_patht   all_pairs_dijkstra_patht   NetworkXErrort   single_source_shortest_patht   single_source_dijkstra_patht   bidirectional_shortest_patht   dijkstra_path(   R	   R
   R   R   t   paths(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/generic.pyR    +   s    ;c         C   s   | d k r` | d k rN | d k r6 t j |   } q] t j |  d | } q t j d   n | d k r | d k r t j |  |  } q t j |  | d | } nL | d k r t j |  | |  } t |  d } n t j	 |  | | |  } | S(   s  Compute shortest path lengths in the graph.

    This function can compute the single source shortest path
    lengths by specifying only the source or all pairs shortest
    path lengths by specifying neither the source or target.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Starting node for path.
       If not specified compute shortest path lengths for all
       connected node pairs.

    target : node, optional
       Ending node for path.
       If not specified compute shortest path lengths for every
       node reachable from the source.

    weight : None or string, optional (default = None)
       If None, every edge has weight/distance/cost 1.
       If a string, use this edge attribute as the edge weight.
       Any edge attribute not present defaults to 1.

    Returns
    -------
    length : number, or container of numbers
        If the source and target are both specified return a
        single number for the shortest path.
        If only the source is specified return a dictionary keyed by
        targets with a the shortest path as keys.
        If neither the source or target is specified return a dictionary
        of dictionaries with length[source][target]=value.

    Raises
    ------
    NetworkXNoPath
        If no path exists between source and target.

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> print(nx.shortest_path_length(G,source=0,target=4))
    4
    >>> p=nx.shortest_path_length(G,source=0) # target not specified
    >>> p[4]
    4
    >>> p=nx.shortest_path_length(G) # source,target not specified
    >>> p[0][4]
    4

    Notes
    -----
    For digraphs this returns the shortest directed path.
    To find path lengths in the reverse direction use G.reverse(copy=False)
    first to flip the edge orientation.

    See Also
    --------
    all_pairs_shortest_path_length()
    all_pairs_dijkstra_path_length()
    single_source_shortest_path_length()
    single_source_dijkstra_path_length()

    R   s%   Target given but no source specified.i   N(
   R   R   t   all_pairs_shortest_path_lengtht   all_pairs_dijkstra_path_lengthR   t"   single_source_shortest_path_lengtht"   single_source_dijkstra_path_lengthR   t   lent   dijkstra_path_length(   R	   R
   R   R   R   t   p(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/generic.pyR      s    Cc         C   s   |  j    r0 t j |   sQ t j d   qQ n! t j |   sQ t j d   n  d } | d k r xx |  D]. } t j |  |  } | t | j    7} qj Wn? x< |  D]4 } t j	 |  | d | } | t | j    7} q Wt
 |   } | | | d S(   s  Return the average shortest path length.

    The average shortest path length is

    .. math::

       a =\sum_{s,t \in V} \frac{d(s, t)}{n(n-1)}

    where `V` is the set of nodes in `G`,
    `d(s, t)` is the shortest path from `s` to `t`,
    and `n` is the number of nodes in `G`.

    Parameters
    ----------
    G : NetworkX graph

    weight : None or string, optional (default = None)
       If None, every edge has weight/distance/cost 1.
       If a string, use this edge attribute as the edge weight.
       Any edge attribute not present defaults to 1.

    Raises
    ------
    NetworkXError:
       if the graph is not connected.

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> print(nx.average_shortest_path_length(G))
    2.0

    For disconnected graphs you can compute the average shortest path
    length for each component:
    >>> G=nx.Graph([(1,2),(3,4)])
    >>> for g in nx.connected_component_subgraphs(G):
    ...     print(nx.average_shortest_path_length(g))
    1.0
    1.0

    s   Graph is not connected.g        R   i   N(   t   is_directedR   t   is_weakly_connectedR   t   is_connectedR   R   t   sumt   valuesR   R   (   R	   R   t   avgt   nodet   path_lengtht   n(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/generic.pyR      s    *c         c   s^  | d k	 r- t j |  | d | \ } } n t j |  |  } | | k rZ t j    n  | d g g } d } x | d k rY| | \ } }	 | | k r g  t | | d   D] \ }
 } |
 ^ q Vn  t | |  |	 k r4| d 7} | t |  k r| j | | |	 d g  qV| | |	 d g | | <qr | | d d c d 7<| d 8} qr Wd S(   s  Compute all shortest paths in the graph.

    Parameters
    ----------
    G : NetworkX graph

    source : node
       Starting node for path.

    target : node
       Ending node for path.

    weight : None or string, optional (default = None)
       If None, every edge has weight/distance/cost 1.
       If a string, use this edge attribute as the edge weight.
       Any edge attribute not present defaults to 1.

    Returns
    -------
    paths: generator of lists
        A generator of all paths between source and target.

    Examples
    --------
    >>> G=nx.Graph()
    >>> G.add_path([0,1,2])
    >>> G.add_path([0,10,2])
    >>> print([p for p in nx.all_shortest_paths(G,source=0,target=2)])
    [[0, 1, 2], [0, 10, 2]]

    Notes
    -----
    There may be many shortest paths between the source and target.

    See Also
    --------
    shortest_path()
    single_source_shortest_path()
    all_pairs_shortest_path()
    R   i    i   N(   R   R   t!   dijkstra_predecessor_and_distancet   predecessorR   t   reversedR   t   append(   R	   R
   R   R   t   predt   distt   stackt   topR$   t   iR   R&   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/generic.pyR     s$    )!/
(   t   __doc__t   networkxR   t   joint
   __author__t   __all__R   R   R    R   R   R   (    (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/generic.pyt   <module>
   s   			T[=