
\c           @   s   d  Z  d Z d d d d d d g Z d d	 l Z d	 d
  Z d	 d  Z d   Z d   Z	 d	 d  Z
 d	 d  Z d	 d	 d	 d  Z d	 S(   s1   
Shortest path algorithms for unweighted graphs.
s   Aric Hagberg (hagberg@lanl.gov)t   bidirectional_shortest_patht   single_source_shortest_patht"   single_source_shortest_path_lengtht   all_pairs_shortest_patht   all_pairs_shortest_path_lengtht   predecessoriNc         C   s   i  } d } i d | 6} xw | r | } i  } x8 | D]0 } | | k r5 | | | <| j  |  |  q5 q5 W| d k	 r | | k r Pn  | d } q W| S(   s  Compute the shortest path lengths from source to all reachable nodes.

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

    source : node
       Starting node for path

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.

    Returns
    -------
    lengths : dictionary
        Dictionary of shortest path lengths keyed by target.

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> length=nx.single_source_shortest_path_length(G,0)
    >>> length[4]
    4
    >>> print(length)
    {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}

    See Also
    --------
    shortest_path_length
    i    i   N(   t   updatet   None(   t   Gt   sourcet   cutofft   seent   levelt	   nextlevelt	   thislevelt   v(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyR      s    	
 c         C   s4   i  } x' |  D] } t  |  | d | | | <q W| S(   sy   Compute the shortest path lengths between all nodes in G.

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

    cutoff : integer, optional
        depth to stop the search. Only paths of length <= cutoff are returned.

    Returns
    -------
    lengths : dictionary
        Dictionary of shortest path lengths keyed by source and target.

    Notes
    -----
    The dictionary returned only has keys for reachable node pairs.

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> length=nx.all_pairs_shortest_path_length(G)
    >>> print(length[1][4])
    3
    >>> length[1]
    {0: 1, 1: 0, 2: 1, 3: 2, 4: 3}

    R
   (   R   (   R   R
   t   pathst   n(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyR   E   s    c         C   s   t  |  | |  } | \ } } } g  } x' | d k	 rP | j |  | | } q* W| | d } x* | d k	 r | j d |  | | } qb W| S(   s'  Return a list of nodes in a shortest path between source and target.

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

    source : node label
       starting node for path

    target : node label
       ending node for path

    Returns
    -------
    path: list
       List of nodes in a path from source to target.

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

    See Also
    --------
    shortest_path

    Notes
    -----
    This algorithm is used by shortest_path(G,source,target).
    i    N(   t   _bidirectional_pred_succR   t   appendt   insert(   R   R	   t   targett   resultst   predt   succt   wt   path(    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyR    j   s     c         C   s  | | k r' i d | 6i d | 6| f S|  j   rH |  j } |  j } n |  j } |  j } i d | 6} i d | 6} | g } | g } x| r| rt |  t |  k r#| }	 g  } x |	 D]\ }
 xS | |
  D]E } | | k r | j |  |
 | | <n  | | k r | | | f Sq Wq Wq | }	 g  } xd |	 D]\ }
 xS | |
  D]E } | | k ru|
 | | <| j |  n  | | k rI| | | f SqIWq6Wq Wt j d | | f   d S(   s   Bidirectional shortest path helper.

       Returns (pred,succ,w) where
       pred is a dictionary of predecessors from w to the source, and
       succ is a dictionary of successors from w to the target.
    s   No path between %s and %s.N(	   R   t   is_directedt   predecessors_itert   successors_itert   neighbors_itert   lenR   t   nxt   NetworkXNoPath(   R   R	   R   t   Gpredt   GsuccR   R   t   forward_fringet   reverse_fringet
   this_levelR   R   (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyR      s@    					 
 c   	      C   s   d } i d | 6} i | g | 6} | d k r3 | Sx | r | } i  } xQ | D]I } x@ |  | D]4 } | | k r` | | | g | | <d | | <q` q` WqO W| d } | d k	 r6 | | k r6 Pq6 q6 W| S(   sc  Compute shortest path between source
    and all other nodes reachable from source.

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

    source : node label
       Starting node for path

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.

    Returns
    -------
    lengths : dictionary
        Dictionary, keyed by target, of shortest paths.

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> path=nx.single_source_shortest_path(G,0)
    >>> path[4]
    [0, 1, 2, 3, 4]

    Notes
    -----
    The shortest path is not necessarily unique. So there can be multiple
    paths between the source and each target node, all of which have the
    same 'shortest' length. For each target node, this function returns
    only one of those paths.

    See Also
    --------
    shortest_path
    i    i   N(   R   (	   R   R	   R
   R   R   R   R   R   R   (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyR      s"    %	
 c         C   s4   i  } x' |  D] } t  |  | d | | | <q W| S(   s	   Compute shortest paths between all nodes.

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

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.

    Returns
    -------
    lengths : dictionary
        Dictionary, keyed by source and target, of shortest paths.

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> path=nx.all_pairs_shortest_path(G)
    >>> print(path[0][4])
    [0, 1, 2, 3, 4]

    See Also
    --------
    floyd_warshall()

    R
   (   R   (   R   R
   R   R   (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyR     s    c         C   sL  d } | g } i | | 6} i g  | 6} x | r | d } | }	 g  } xz |	 D]r }
 xi |  |
 D]] } | | k r |
 g | | <| | | <| j  |  q` | | | k r` | | j  |
  q` q` WqO W| r, | | k r, Pq, q, W| d k	 r4| r| | k rg  d f S| | | | f S| | k r)g  S| | Sn | rD| | f S| Sd S(   s   Returns dictionary of predecessors for the path from source to all nodes in G.


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

    source : node label
       Starting node for path

    target : node label, optional
       Ending node for path. If provided only predecessors between
       source and target are returned

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.


    Returns
    -------
    pred : dictionary
        Dictionary, keyed by node, of predecessors in the shortest path.

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> print(G.nodes())
    [0, 1, 2, 3]
    >>> nx.predecessor(G,0)
    {0: [], 1: [0], 2: [1], 3: [2]}

    i    i   iN(   R   R   (   R   R	   R   R
   t   return_seenR   R   R   R   R   R   R   (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyR   '  s:    !		

 
 
(   t   __doc__t
   __author__t   __all__t   networkxR    R   R   R   R    R   R   R   R   (    (    (    se   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/unweighted.pyt   <module>   s   	.%	1	27#