ó
ú£Õ\c           @   s
  d  Z  d j d d d g ƒ Z d d d d d	 d
 d d d d d g Z d d l Z d d l Z d d l m Z d d „ Z	 d d „ Z
 d d d „ Z d d d „ Z d d d d „ Z d d d „ Z d d d „ Z d d d „ Z d d „ Z d d „ Z d d „ Z d S(   s.   
Shortest path algorithms for weighed graphs.
s   
s   Aric Hagberg <hagberg@lanl.gov>s'   LoÃ¯c SÃ©guin-C. <loicseguin@gmail.com>s    Dan Schult <dschult@colgate.edu>t   dijkstra_patht   dijkstra_path_lengtht   bidirectional_dijkstrat   single_source_dijkstrat   single_source_dijkstra_patht"   single_source_dijkstra_path_lengtht   all_pairs_dijkstra_patht   all_pairs_dijkstra_path_lengtht!   dijkstra_predecessor_and_distancet   bellman_fordt   negative_edge_cycleiÿÿÿÿN(   t   generate_unique_nodet   weightc         C   s^   t  |  | d | d | ƒ\ } } y | | SWn* t k
 rY t j d | | f ƒ ‚ n Xd S(   s  Returns the shortest path from source to target in a weighted graph G.

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

    source : node
       Starting node

    target : node
       Ending node

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

    Returns
    -------
    path : list
       List of nodes in a shortest path.

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

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

    Notes
    ------
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    See Also
    --------
    bidirectional_dijkstra()
    t   targetR   s   node %s not reachable from %sN(   R   t   KeyErrort   nxt   NetworkXNoPath(   t   Gt   sourceR   R   t   lengtht   path(    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR       s    )c         C   sR   t  |  | d | ƒ} y | | SWn* t k
 rM t j d | | f ƒ ‚ n Xd S(   s.  Returns the shortest path length from source to target
    in a weighted graph.

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

    source : node label
       starting node for path

    target : node label
       ending node for path

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

    Returns
    -------
    length : number
        Shortest path length.

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

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> print(nx.dijkstra_path_length(G,0,4))
    4

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    See Also
    --------
    bidirectional_dijkstra()
    R   s   node %s not reachable from %sN(   R   R   R   R   (   R   R   R   R   R   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   O   s
    *c         C   s   t  |  | d | ƒ\ } } | S(   s\  Compute shortest path between source and all other reachable
    nodes for a weighted graph.

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

    source : node
       Starting node for path.

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

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

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

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

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    See Also
    --------
    single_source_dijkstra()

    R   (   R   (   R   R   t   cutoffR   R   R   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   €   s    'c            s»  i  } i d | 6} g  } t  j | d | f ƒ x…| r¶t  j | ƒ \ } } | | k r_ q2 n  | | | <|  j ƒ  rÛ g  }	 xs |  | j ƒ  D]H \ }
 } t ‡  f d †  | j ƒ  Dƒ ƒ } |	 j |
 i | ˆ  6f ƒ qŒ Wn t |  | j ƒ  ƒ }	 x¿ |	 D]· \ }
 } | | | j ˆ  d ƒ } | d k	 r?| | k r?qø q?n  |
 | k rp| | |
 k  r¯t
 d d ƒ ‚ q¯qø |
 | k sŒ| | |
 k  rø | | |
 <t  j | | |
 f ƒ qø qø Wq2 W| S(   sŸ  Compute the shortest path length between source and all other
    reachable nodes for a weighted graph.

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

    source : node label
       Starting node for path

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight.

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

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

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

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    See Also
    --------
    single_source_dijkstra()

    i    c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   t   get(   t   .0t   kt   dd(   R   (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>ã   s   i   s   Contradictory paths found:s   negative weights?N(   t   heapqt   heappusht   heappopt   is_multigrapht   itemst   mint   appendt   iterR   t   Nonet
   ValueError(   R   R   R   R   t   distt   seent   fringet   dt   vt   edatat   wt   keydatat	   minweightt   edgedatat   vw_dist(    (   R   sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   «   s:    *	
!	
!c            s  | | k r' i d | 6i | g | 6f Si  } i | g | 6} i d | 6} g  } t  j | d | f ƒ xª| rt  j | ƒ \ }	 }
 |
 | k r– qi n  |	 | |
 <|
 | k r° Pn  |  j ƒ  r"g  } xs |  |
 j ƒ  D]H \ } } t ‡  f d †  | j ƒ  Dƒ ƒ } | j | i | ˆ  6f ƒ qÓ Wn t |  |
 j ƒ  ƒ } xÔ | D]Ì \ } } | |
 | j ˆ  d ƒ } | d k	 r†| | k r†q?q†n  | | k r·| | | k  rt
 d d ƒ ‚ qq?| | k sÓ| | | k  r?| | | <t  j | | | f ƒ | |
 | g | | <q?q?Wqi W| | f S(   sw  Compute shortest paths and lengths in a weighted graph G.

    Uses Dijkstra's algorithm for shortest paths.

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

    source : node label
       Starting node for path

    target : node label, optional
       Ending node for path

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

    Returns
    -------
    distance,path : dictionaries
       Returns a tuple of two dictionaries keyed by node.
       The first dictionary stores distance from the source.
       The second stores the path from the source to that node.


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

    Notes
    ---------
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    Based on the Python cookbook recipe (119466) at
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/119466

    This algorithm is not guaranteed to work if edge weights
    are negative or are floating point numbers
    (overflows and roundoff errors can cause problems).

    See Also
    --------
    single_source_dijkstra_path()
    single_source_dijkstra_path_length()
    i    c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   R   (   R   (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>A  s   i   s   Contradictory paths found:s   negative weights?N(   R   R   R   R   R   R   R    R!   R   R"   R#   (   R   R   R   R   R   R$   t   pathsR%   R&   R'   R(   R)   R*   R+   R,   R-   R.   (    (   R   sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   ø   sF    6	
!	
 c            s  t  j } t  j } i  } i g  | 6} i d | 6} g  }	 | |	 d | f ƒ x°|	 rý| |	 ƒ \ }
 } | | k rx qN n  |
 | | <|  j ƒ  rô g  } xs |  | j ƒ  D]H \ } } t ‡  f d †  | j ƒ  Dƒ ƒ } | j | i | ˆ  6f ƒ q¥ Wn t |  | j ƒ  ƒ } xí | D]å \ } } | | | j ˆ  d ƒ } | d k	 rX| | k rXqqXn  | | k r‰| | | k  röt
 d d ƒ ‚ qöq| | k s¥| | | k  rÒ| | | <| |	 | | f ƒ | g | | <q| | | k r| | j | ƒ qqWqN W| | f S(   s]  Compute shortest path length and predecessors on shortest paths
    in weighted graphs.

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

    source : node label
       Starting node for path

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

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

    Returns
    -------
    pred,distance : dictionaries
       Returns two dictionaries representing a list of predecessors
       of a node and the distance to each node.

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    The list of predecessors contains more than one element only when
    there are more than one shortest paths to the key node.
    i    c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   R   (   R   (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>„  s   i   s   Contradictory paths found:s   negative weights?N(   R   R   R   R   R   R   R    R!   R   R"   R#   (   R   R   R   R   t   pusht   popR$   t   predR%   R&   R'   R(   R)   R*   R+   R,   R-   R.   (    (   R   sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   W  sF    			 
!	
c         C   s:   i  } x- |  D]% } t  |  | d | d | ƒ| | <q W| S(   se   Compute shortest path lengths between all nodes in a weighted graph.

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

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

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

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

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

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    The dictionary returned only has keys for reachable node pairs.
    R   R   (   R   (   R   R   R   R/   t   n(    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   ›  s
    "c         C   s:   i  } x- |  D]% } t  |  | d | d | ƒ| | <q W| S(   s   Compute shortest paths between all nodes in a weighted graph.

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

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

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

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

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

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

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

    R   R   (   R   (   R   R   R   R/   R3   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   Ã  s
    #c            sU  | |  k r t  d | ƒ ‚ n  t |  ƒ } i d | 6} i d | 6} | d k r[ | | f S|  j ƒ  ry ‡  f d †  } n ‡  f d †  } xÀ t | ƒ D]£ } t } xŠ t | j ƒ  ƒ D]v \ }	 }
 xg |  |	 j ƒ  D]U \ } } |
 | | ƒ } | | k s	| | | k rÑ | | | <|	 | | <t } qÑ qÑ Wq´ W| r• Pq• q• Wt	 j
 d ƒ ‚ | | f S(   s}  Compute shortest path lengths and predecessors on shortest paths
    in weighted graphs.

    The algorithm has a running time of O(mn) where n is the number of
    nodes and m is the number of edges.  It is slower than Dijkstra but
    can handle negative edge weights.

    Parameters
    ----------
    G : NetworkX graph
       The algorithm works for all types of graphs, including directed
       graphs and multigraphs.

    source: node label
       Starting node for path

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

    Returns
    -------
    pred, dist : dictionaries
       Returns two dictionaries keyed by node to predecessor in the
       path and to the distance from the source respectively.

    Raises
    ------
    NetworkXUnbounded
       If the (di)graph contains a negative cost (di)cycle, the
       algorithm raises an exception to indicate the presence of the
       negative cost (di)cycle.  Note: any negative weight edge in an
       undirected graph is a negative cost cycle.

    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.path_graph(5, create_using = nx.DiGraph())
    >>> pred, dist = nx.bellman_ford(G, 0)
    >>> pred
    {0: None, 1: 0, 2: 1, 3: 2, 4: 3}
    >>> dist
    {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}

    >>> from nose.tools import assert_raises
    >>> G = nx.cycle_graph(5, create_using = nx.DiGraph())
    >>> G[1][2]['weight'] = -7
    >>> assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, 0)

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    The dictionaries returned only have keys for nodes reachable from
    the source.

    In the case where the (di)graph is not connected, if a component
    not containing the source contains a negative cost (di)cycle, it
    will not be detected.

    s!   Node %s is not found in the graphi    i   c            s/   t  g  |  j ƒ  D] } | j ˆ  d ƒ ^ q ƒ S(   Ni   (   R   t   valuesR   (   t	   edge_dictt   eattr(   R   (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyt
   get_weight5  s    c            s   |  j  ˆ  d ƒ S(   Ni   (   R   (   R5   (   R   (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR7   8  s    s   Negative cost cycle detected.N(   R   t   lenR"   R   t   ranget   Truet   listR   t   FalseR   t   NetworkXUnbounded(   R   R   R   t
   numb_nodesR$   R2   R7   t   it
   no_changest   ut   dist_uR(   t   edictt   dist_v(    (   R   sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR	   ì  s.    >


c         C   sy   t  ƒ  } |  j g  |  D] } | | f ^ q ƒ y t |  | | ƒ Wn" t j k
 rg |  j | ƒ t SX|  j | ƒ t S(   s®  Return True if there exists a negative edge cycle anywhere in G.

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

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

    Returns
    -------
    negative_cycle : bool
        True if a negative edge cycle exists, otherwise False.

    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.cycle_graph(5, create_using = nx.DiGraph())
    >>> print(nx.negative_edge_cycle(G))
    False
    >>> G[1][2]['weight'] = -7
    >>> print(nx.negative_edge_cycle(G))
    True

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    This algorithm uses bellman_ford() but finds negative cycles
    on any component by first adding a new node connected to
    every node, and starting bellman_ford on that node.  It then
    removes that extra node.
    (   R   t   add_edges_fromR	   R   R=   t   remove_nodeR:   R<   (   R   R   t   newnodeR3   (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR
   K  s    #	&c            sž  | | k r d | g f Si  i  g } i | g | 6i | g | 6g } g  g  g } i d | 6i d | 6g } t  j | d d | f ƒ t  j | d d | f ƒ |  j ƒ  rÀ |  j |  j g } n |  j |  j g } g  }	 d }
 x | d r€| d r€d |
 }
 t  j | |
 ƒ \ } } | | |
 k r.qá n  | | |
 | <| | d |
 k rZ| |	 f Sx | |
 | ƒ D]} |
 d k rå|  j ƒ  r¶t ‡  f d †  |  | | j	 ƒ  Dƒ ƒ } n |  | | j
 ˆ  d ƒ } | |
 | | } ne |  j ƒ  rt ‡  f d †  |  | | j	 ƒ  Dƒ ƒ } n |  | | j
 ˆ  d ƒ } | |
 | | } | | |
 k r€| | |
 | k  ryt d ƒ ‚ qyqk| | |
 k s¤| | |
 | k  rk| | |
 | <t  j | |
 | | f ƒ | |
 | | g | |
 | <| | d k ry| | d k ry| d | | d | } |	 g  k s;| | k rv| } | d | } | j ƒ  | d | | d }	 qvqyqkqkWqá Wt j d | | f ƒ ‚ d S(   s™  Dijkstra's algorithm for shortest paths using bidirectional search.

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

    source : node
       Starting node.

    target : node
       Ending node.

    weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight

    Returns
    -------
    length : number
        Shortest path length.

    Returns a tuple of two dictionaries keyed by node.
    The first dictionary stores distance from the source.
    The second stores the path from the source to that node.

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

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

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    In practice  bidirectional Dijkstra is much more than twice as fast as
    ordinary Dijkstra.

    Ordinary Dijkstra expands nodes in a sphere-like manner from the
    source. The radius of this sphere will eventually be the length
    of the shortest path. Bidirectional Dijkstra will expand nodes
    from both the source and the target, making two spheres of half
    this radius. Volume of the first sphere is pi*r*r while the
    others are 2*pi*r/2*r/2, making up half the volume.

    This algorithm is not guaranteed to work if edge weights
    are negative or are floating point numbers
    (overflows and roundoff errors can cause problems).

    See Also
    --------
    shortest_path
    shortest_path_length
    i    i   c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   R   (   R   (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>Þ  s   c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   R   (   R   (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>å  s   s,   Contradictory paths found: negative weights?s   No path between %s and %s.N(   R   R   t   is_directedt   successors_itert   predecessors_itert   neighbors_iterR   R   R   R   R   R#   t   reverseR   R   (   R   R   R   R   t   distsR/   R&   R%   t   neighst	   finalpatht   dirR$   R(   t	   finaldistR*   R,   t   vwLengtht	   totaldistt   revpath(    (   R   sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyR   z  s`    ?  

$ 
'(   t   __doc__t   joint
   __author__t   __all__R   t   networkxR   t   networkx.utilsR   R    R   R"   R   R   R   R   R   R   R	   R
   R   (    (    (    sc   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/shortest_paths/weighted.pyt   <module>   s8   	11+L_D()_/