ó
|£*^c           @   sp  d  Z  d j d d d g ƒ Z d d d d d	 d
 d d d d d d d g Z d d l m Z d d l m Z m Z d d l	 m
 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 d d „ Z d d d „ Z d d d  „ Z d d d! „ Z d d" „ Z d# „  Z 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_cyclet   goldberg_radzikt   johnsoniÿÿÿÿ(   t   deque(   t   heappusht   heappop(   t   countN(   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(    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/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   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   V   s    *c         C   s%   t  |  | d | 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()

    t   cutoffR   (   R   (   R   R   R   R   R   R   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   ˆ   s    'c            sC   |  j  ƒ  r ‡  f d †  } n ‡  f d †  } t |  | | d | ƒ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()

    c            s    t  ‡  f d †  | j ƒ  Dƒ ƒ S(   Nc         3   s!   |  ] } | j  ˆ  d  ƒ Vq d S(   i   N(   t   get(   t   .0t   eattr(   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>à   s    (   t   mint   values(   t   ut   vt   data(   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyt   <lambda>ß   s   c            s   | j  ˆ  d ƒ S(   Ni   (   R   (   R!   R"   R#   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR$   â   s    R   (   t   is_multigrapht	   _dijkstra(   R   R   R   R   t
   get_weight(    (   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   ´   s    *c      
      s†   | | k r' i d | 6i | g | 6f S|  j  ƒ  rE ‡  f d †  } n ‡  f d †  } i | g | 6} t |  | | d | d | d | ƒS(   ss  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            s    t  ‡  f d †  | j ƒ  Dƒ ƒ S(   Nc         3   s!   |  ] } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>"  s    (   R   R    (   R!   R"   R#   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR$   !  s   c            s   | j  ˆ  d ƒ S(   Ni   (   R   (   R!   R"   R#   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR$   $  s    t   pathsR   R   (   R%   R&   (   R   R   R   R   R   R'   R(   (    (   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   ç   s    6c         C   sO  |  j  ƒ  r |  j n |  j } t } t }	 i  }
 i d | 6} t ƒ  } g  } | | d t | ƒ | f ƒ x´| r|	 | ƒ \ } } } | |
 k r˜ qk n  | |
 | <| | k r² Pn  xf| | j ƒ  D]T\ } } | | | | ƒ } | d k ró qÃ n  |
 | | | | | ƒ } | d k	 r.| | k r.qÃ q.n  | |
 k r_| |
 | k  rt	 d d ƒ ‚ qqÃ | | k s{| | | k  rä| | | <| | | t | ƒ | f ƒ | d k	 rÅ| | | g | | <n  | d k	 r| g | | <qqÃ | | | k rÃ | d k	 r| | j
 | ƒ qqÃ qÃ Wqk W| d k	 r5|
 | f S| d k	 rK| |
 f S|
 S(   s  Implementation of Dijkstra's algorithm

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

    source : node label
       Starting node for path

    get_weight: function
        Function for getting edge weight

    pred: list, optional(default=None)
        List of predecessors of a node

    paths: dict, optional (default=None)
        Path from the source to a target node.

    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.

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

    distance : dictionary
       Dictionary of shortest lengths keyed by target.
    i    s   Contradictory paths found:s   negative weights?N(   t   is_directedt   succt   adjR   R   R   t   nextt   itemst   Nonet
   ValueErrort   append(   R   R   R'   t   predR(   R   R   t   G_succt   pusht   popt   distt   seent   ct   fringet   dt   _R"   R!   t   et   costt   vu_dist(    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR&   +  sT    (		
	


c            sV   |  j  ƒ  r ‡  f d †  } n ‡  f d †  } i g  | 6} t |  | | d | d | ƒ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.
    c            s    t  ‡  f d †  | j ƒ  Dƒ ƒ S(   Nc         3   s!   |  ] } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>£  s    (   R   R    (   R!   R"   R#   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR$   ¢  s   c            s   | j  ˆ  d ƒ S(   Ni   (   R   (   R!   R"   R#   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR$   ¥  s    R1   R   (   R%   R&   (   R   R   R   R   R'   R1   (    (   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   ‚  s
    c            s#   t  ‰ ‡  ‡ ‡ ‡ f d †  ˆ  Dƒ 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.
    c      	      s.   i  |  ]$ } ˆ ˆ  | d  ˆ d ˆ ƒ| “ q S(   R   R   (    (   R   t   n(   R   R   R   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys
   <dictcomp>Ï  s   	 (   R   (   R   R   R   (    (   R   R   R   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   «  s    "c            s#   t  ‰ ‡  ‡ ‡ ‡ f d †  ˆ  Dƒ 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()

    c      	      s.   i  |  ]$ } ˆ ˆ  | d  ˆ d ˆ ƒ| “ q S(   R   R   (    (   R   R>   (   R   R   R   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys
   <dictcomp>÷  s   	 (   R   (   R   R   R   (    (   R   R   R   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   Ò  s    #c         C   s¾   | |  k r t  d | ƒ ‚ n  xM |  j d t ƒ D]9 \ } } } | j | d ƒ d k  r2 t j d ƒ ‚ q2 q2 Wi d | 6} i d | 6} t |  ƒ d k r¥ | | f St |  | | | g | ƒ 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)
    >>> sorted(pred.items())
    [(0, None), (1, 0), (2, 1), (3, 2), (4, 3)]
    >>> sorted(dist.items())
    [(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 graphR#   i   i    s   Negative cost cycle detected.N(	   R   t   selfloop_edgest   TrueR   R   t   NetworkXUnboundedR.   t   lent   _bellman_ford_relaxation(   R   R   R   R!   R"   t   attrR5   R1   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR	   ú  s    >"
c            s•  |  j  ƒ  r ‡  f d †  } n ‡  f d †  } |  j ƒ  rB |  j n |  j } t d ƒ } t |  ƒ } i  }	 t | ƒ }
 t | ƒ } x|
 rŠ|
 j ƒ  } | j	 | ƒ | | | k r„ | | } xÇ | | j
 ƒ  D]² \ } } | | | ƒ } | | j | | ƒ k  rÎ | | k ri|
 j | ƒ | j | ƒ |	 j | d ƒ d } | | k r\t j d ƒ ‚ n  | |	 | <n  | | | <| | | <qÎ qÎ Wq„ q„ W| | f S(   s(  Relaxation loop for Bellmanâ€“Ford algorithm

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

    pred: dict
        Keyed by node to predecessor in the path

    dist: dict
        Keyed by node to the distance from the source

    source: list
        List of source nodes

    weight: string
       Edge data key corresponding to the edge weight

    Returns
    -------
    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
    c            s    t  ‡  f d †  |  j ƒ  Dƒ ƒ S(   Nc         3   s!   |  ] } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>j  s    (   R   R    (   t	   edge_dict(   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR'   i  s    c            s   |  j  ˆ  d ƒ S(   Ni   (   R   (   RE   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR'   l  s    t   infi    i   s   Negative cost cycle detected.(   R%   R)   R*   R+   t   floatRB   R   t   sett   popleftt   removeR-   R   R0   t   addR   RA   (   R   R1   R5   R   R   R'   R2   RF   R>   R   t   qt   in_qR!   t   dist_uR"   R;   t   dist_vt   count_v(    (   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyRC   H  s8     	

c   
         s°  | |  k r t  d | ƒ ‚ n  xM |  j d t ƒ D]9 \ } } } | j ˆ d ƒ d k  r2 t j d ƒ ‚ q2 q2 Wt |  ƒ d k r™ i d | 6i d | 6f S|  j ƒ  r· ‡ f d †  ‰ n ‡ f d †  ‰ |  j	 ƒ  rÞ |  j
 ‰  n	 |  j ‰  t d ƒ ‰ t ‡ f d	 †  |  Dƒ ƒ ‰ d ˆ | <i d | 6‰ ‡  ‡ ‡ ‡ f d
 †  } ‡  ‡ ‡ ‡ f d †  } t | g ƒ } x" | r‰| | ƒ }	 | |	 ƒ } qhWt ‡ f d †  ˆ 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.goldberg_radzik(G, 0)
    >>> sorted(pred.items())
    [(0, None), (1, 0), (2, 1), (3, 2), (4, 3)]
    >>> sorted(dist.items())
    [(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.goldberg_radzik, 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 graphR#   i   i    s   Negative cost cycle detected.c            s    t  ‡  f d †  |  j ƒ  Dƒ ƒ S(   Nc         3   s!   |  ] } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   RD   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>×  s    (   R   R    (   RE   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR'   Ö  s    c            s   |  j  ˆ  d ƒ S(   Ni   (   R   (   RE   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR'   Ù  s    RF   c         3   s   |  ] } | ˆ  f Vq d  S(   N(    (   R   R!   (   RF   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>â  s    c            sÿ  g  } i  } xâ|  D]Ú} | | k r+ q n  ˆ | ‰  t  ‡ ‡  ‡ f d †  ˆ | j ƒ  Dƒ ƒ rg q n  | t ˆ | j ƒ  ƒ f g } t | g ƒ } d | | <xK| rì| d \ } } y t | ƒ \ } } Wn8 t k
 r| j | ƒ | j ƒ  | j | ƒ q¢ n Xˆ | ˆ | ƒ }	 ˆ | }
 |	 |
 k r¢ |	 |
 k  } |	 ˆ | <| ˆ | <| | k rª| | t	 | ƒ | | <| j | t ˆ | j ƒ  ƒ f ƒ | j
 | ƒ qé| | k ré| | t	 | ƒ | | k rét j d ƒ ‚ qéq¢ q¢ Wq W| j ƒ  | S(   se   Topologically sort nodes relabeled in the previous round and detect
        negative cycles.
        c         3   s/   |  ]% \ } } ˆ ˆ | ƒ ˆ  | k Vq d  S(   N(    (   R   R"   R;   (   R9   t   d_uR'   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>ú  s    i    iÿÿÿÿs   Negative cost cycle detected.(   t   allR-   t   iterRH   R,   t   StopIterationR0   R4   RJ   t   intRK   R   RA   t   reverse(   t	   relabeledt   to_scant	   neg_countR!   t   stackt   in_stackt   itR"   R;   t   tt   d_vt   is_neg(   R2   R9   R'   R1   (   RQ   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyt	   topo_sortæ  sH    
,
	



#
c            s‘   t  ƒ  } x� |  D]y } ˆ | } xf ˆ  | j ƒ  D]T \ } } ˆ | ƒ } | | ˆ | k  r1 | | ˆ | <| ˆ | <| j | ƒ q1 q1 Wq W| S(   s,   Relax out-edges of relabeled nodes.
        (   RH   R-   RK   (   RX   RW   R!   RQ   R"   R;   t   w_e(   R2   R9   R'   R1   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyt   relax   s    	

c         3   s   |  ] } | ˆ  | f Vq d  S(   N(    (   R   R!   (   R9   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>8  s    N(   R   R?   R@   R   R   RA   RB   R.   R%   R)   R*   R+   RG   t   dictRH   (
   R   R   R   R!   R"   RD   R`   Rb   RW   RX   (    (   R2   R9   R'   RF   R1   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   �  s2    >"	
:	c         C   st   t  ƒ  } |  j g  |  D] } | | f ^ q ƒ z0 y t |  | | ƒ Wn t j k
 r] t SXWd |  j | ƒ Xt 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.
    N(   R   t   add_edges_fromR	   R   RA   R@   t   remove_nodet   False(   R   R   t   newnodeR>   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR
   <  s    #	& 	c            sÅ  | | k r d | g f St  } t } i  i  g } i | g | 6i | g | 6g } g  g  g } i d | 6i d | 6g }	 t ƒ  }
 | | d d t |
 ƒ | f ƒ | | d d t |
 ƒ | f ƒ |  j ƒ  rá |  j |  j g } n |  j |  j g } g  } d } x¦| d r§| d r§d | } | | | ƒ \ } } } | | | k rOqn  | | | | <| | d | k r{| | 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  r t d ƒ ‚ q qŒ| |	 | k sÅ| |	 | | k  rŒ| |	 | | <| | | | t |
 ƒ | f ƒ | | | | g | | | <| |	 d k r | |	 d k r |	 d | |	 d | } | g  k sb| | k r�| } | d | } | j ƒ  | d | | d } q�q qŒqŒ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   t   kt   dd(   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>Õ  s   c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   Rh   Ri   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>Ü  s   s,   Contradictory paths found: negative weights?s   No path between %s and %s.N(   R   R   R   R,   R)   t   successors_itert   predecessors_itert   neighbors_iterR%   R   R-   R   R/   RV   R   R   (   R   R   R   R   R3   R4   t   distsR(   R8   R6   R7   t   neighst	   finalpatht   dirR5   R:   R"   t	   finaldistt   wt	   minweightt   vwLengtht	   totaldistt   revpath(    (   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   k  sh    ? 	  

$  
'c            s¹   t  j ˆ  d ˆ ƒs' t  j d ƒ ‚ n  d „  ˆ  Dƒ } d „  ˆ  Dƒ } t ˆ  | | ˆ  j ƒ  ˆ ƒ d ‰ ˆ  j ƒ  rŠ ‡ ‡ f d †  ‰ n ‡ ‡ f d †  ‰ ‡  ‡ f d †  ˆ  Dƒ } | S(	   si  Compute shortest paths between all nodes in a weighted graph using
    Johnson's algorithm.

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

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

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

    Raises
    ------
    NetworkXError
       If given graph is not weighted.

    Examples
    --------
    >>> import networkx as nx
    >>> graph = nx.DiGraph()
    >>> graph.add_weighted_edges_from([('0', '3', 3), ('0', '1', -5),
    ... ('0', '2', 2), ('1', '2', 4), ('2', '3', 1)])
    >>> paths = nx.johnson(graph, weight='weight')
    >>> paths['0']['2']
    ['0', '1', '2']

    Notes
    -----
    Johnson's algorithm is suitable even for graphs with negative weights. It
    works by using the Bellmanâ€“Ford algorithm to compute a transformation of
    the input graph that removes all negative weights, allowing Dijkstra's
    algorithm to be used on the transformed graph.

    It may be faster than Floyd - Warshall algorithm in sparse graphs.
    Algorithm complexity: O(V^2 * logV + V * E)

    See Also
    --------
    floyd_warshall_predecessor_and_distance
    floyd_warshall_numpy
    all_pairs_shortest_path
    all_pairs_shortest_path_length
    all_pairs_dijkstra_path
    bellman_ford
    R   s   Graph is not weighted.c         S   s   i  |  ] } d  | “ q S(   i    (    (   R   R"   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys
   <dictcomp>,  s   	 c         S   s   i  |  ] } d  | “ q S(   N(   R.   (   R   R"   (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys
   <dictcomp>-  s   	 i   c            s0   t  ‡ f d †  | j ƒ  Dƒ ƒ ˆ  |  ˆ  | S(   Nc         3   s!   |  ] } | j  ˆ  d  ƒ Vq d S(   i   N(   R   (   R   R   (   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys	   <genexpr>5  s    (   R   R    (   R!   R"   R#   (   t   dist_bellmanR   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR$   4  s    c            s    | j  ˆ d ƒ ˆ  |  ˆ  | S(   Ni   (   R   (   R!   R"   R#   (   Rw   R   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR$   8  s    c      
      s9   i  |  ]/ } t  ˆ  | ˆ d  i | g | 6ƒd | “ q S(   R(   i   (   R&   (   R   R"   (   R   R'   (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pys
   <dictcomp>;  s   	 (   R   t   is_weightedt   NetworkXErrorRC   t   nodesR%   (   R   R   R5   R1   t	   all_pairs(    (   R   Rw   R'   R   s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyR   ÷  s    2(   t   __doc__t   joint
   __author__t   __all__t   collectionsR   t   heapqR   R   t	   itertoolsR   t   networkxR   t   networkx.utilsR   R    R   R.   R   R   R   R&   R   R   R   R	   RC   R   R
   R   R   (    (    (    s   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/shortest_paths/weighted.pyt   <module>   sL   		22,2D	V)'(N	E¯/Œ