ó
|£*^c           @   sò   d  d l  m Z m Z d  d l m Z d  d l Z d  d l m Z d j	 d d d d	 g ƒ Z
 d
 d g Z d d „ Z d d „ Z d d „ Z e d ƒ d d „ ƒ Z d e f d „  ƒ  YZ d d d d „ Z d d d „ Z d d d d „ Z d S(   iÿÿÿÿ(   t   heappusht   heappop(   t   countN(   t   not_implemented_fors   
s+   SÃ©rgio Nery SimÃµes <sergionery@gmail.com>s%   Aric Hagberg <aric.hagberg@gmail.com>s   Andrey Paramonovs$   Jordi Torrents <jordi.t21@gmail.com>t   all_simple_pathst   shortest_simple_pathsc         C   sŸ   | |  k r" t  j d | ƒ ‚ n  | |  k rD t  j d | ƒ ‚ n  | d k rc t |  ƒ d } n  |  j ƒ  r… t |  | | d | ƒSt |  | | d | ƒSd S(   s·  Generate all simple paths in the graph G from source to target.

    A simple path is a path with no repeated nodes.

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

    source : node
       Starting node for path

    target : node
       Ending node for path

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

    Returns
    -------
    path_generator: generator
       A generator that produces lists of simple paths.  If there are no paths
       between the source and target within the given cutoff the generator
       produces no output.

    Examples
    --------
    >>> G = nx.complete_graph(4)
    >>> for path in nx.all_simple_paths(G, source=0, target=3):
    ...     print(path)
    ...
    [0, 1, 2, 3]
    [0, 1, 3]
    [0, 2, 1, 3]
    [0, 2, 3]
    [0, 3]
    >>> paths = nx.all_simple_paths(G, source=0, target=3, cutoff=2)
    >>> print(list(paths))
    [[0, 1, 3], [0, 2, 3], [0, 3]]

    Notes
    -----
    This algorithm uses a modified depth-first search to generate the
    paths [1]_.  A single path can be found in `O(V+E)` time but the
    number of simple paths in a graph can be very large, e.g. `O(n!)` in
    the complete graph of order n.

    References
    ----------
    .. [1] R. Sedgewick, "Algorithms in C, Part 5: Graph Algorithms",
       Addison Wesley Professional, 3rd ed., 2001.

    See Also
    --------
    all_shortest_paths, shortest_path
    s   source node %s not in graphs   target node %s not in graphi   t   cutoffN(   t   nxt   NetworkXErrort   Nonet   lent   is_multigrapht   _all_simple_paths_multigrapht   _all_simple_paths_graph(   t   Gt   sourcet   targetR   (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR      s    8c         c   s  | d k  r d  S| g } t  |  | ƒ g } xä | r| d } t | d  ƒ } | d  k rq | j ƒ  | j ƒ  q/ t | ƒ | k  rÔ | | k rž | | g Vq| | k r| j | ƒ | j t  |  | ƒ ƒ qq/ | | k sì | | k rû | | g Vn  | j ƒ  | j ƒ  q/ Wd  S(   Ni   iÿÿÿÿ(   t   itert   nextR	   t   popR
   t   append(   R   R   R   R   t   visitedt   stackt   childrent   child(    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR   [   s(    		


c   
      c   sA  | d k  r d  S| g } d „  |  j  | ƒ Dƒ g } x| r<| d } t | d  ƒ } | d  k rz | j ƒ  | j ƒ  q8 t | ƒ | k  ræ | | k r§ | | g Vq9| | k r9| j | ƒ | j d „  |  j  | ƒ Dƒ ƒ q9q8 | g t | ƒ j | ƒ } x  t | ƒ D] }	 | | g VqW| j ƒ  | j ƒ  q8 Wd  S(   Ni   c         s   s   |  ] \ } } | Vq d  S(   N(    (   t   .0t   ut   v(    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pys	   <genexpr>w   s    iÿÿÿÿc         s   s   |  ] \ } } | Vq d  S(   N(    (   R   R   R   (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pys	   <genexpr>ƒ   s    (	   t   edgesR   R	   R   R
   R   t   listR   t   range(
   R   R   R   R   R   R   R   R   R   t   i(    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR   s   s*    		

&
t
   multigraphc         #   s  | ˆ  k r" t  j d | ƒ ‚ n  | ˆ  k rD t  j d | ƒ ‚ n  ˆ d	 k r_ t } t } n ‡  ‡ f d †  } t } t ƒ  } t ƒ  } d	 } xvt r| sÏ | ˆ  | | d ˆ ƒ\ }	 }
 | j	 |	 |
 ƒ nt
 ƒ  } t
 ƒ  } xò t d t | ƒ ƒ D]Û } | |  } | | ƒ } x@ | D]8 }
 |
 |  | k r| j |
 | d |
 | f ƒ qqW| j | d ƒ yT | ˆ  | d | d | d | d ˆ ƒ\ }	 } | d  | }
 | j	 | |	 |
 ƒ Wq÷ t  j k
 rÑq÷ Xq÷ W| r| j ƒ  }
 |
 V| j |
 ƒ |
 } q’ Pq’ Wd	 S(
   sº  Generate all simple paths in the graph G from source to target,
       starting from shortest ones.

    A simple path is a path with no repeated nodes.

    If a weighted shortest path search is to be used, no negative weights
    are allawed.

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

    source : node
       Starting node for path

    target : node
       Ending node for path

    weight : string
        Name of the edge attribute to be used as a weight. If None all
        edges are considered to have unit weight. Default value None.

    Returns
    -------
    path_generator: generator
       A generator that produces lists of simple paths, in order from
       shortest to longest.

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

    NetworkXError
       If source or target nodes are not in the input graph.

    NetworkXNotImplemented
       If the input graph is a Multi[Di]Graph.

    Examples
    --------

    >>> G = nx.cycle_graph(7)
    >>> paths = list(nx.shortest_simple_paths(G, 0, 3))
    >>> print(paths)
    [[0, 1, 2, 3], [0, 6, 5, 4, 3]]

    You can use this function to efficiently compute the k shortest/best
    paths between two nodes.

    >>> from itertools import islice
    >>> def k_shortest_paths(G, source, target, k, weight=None):
    ...     return list(islice(nx.shortest_simple_paths(G, source, target, weight=weight), k))
    >>> for path in k_shortest_paths(G, 0, 3, 2):
    ...     print(path)
    [0, 1, 2, 3]
    [0, 6, 5, 4, 3]

    Notes
    -----
    This procedure is based on algorithm by Jin Y. Yen [1]_.  Finding
    the first K paths requires O(KN^3) operations.

    See Also
    --------
    all_shortest_paths
    shortest_path
    all_simple_paths

    References
    ----------
    .. [1] Jin Y. Yen, "Finding the K Shortest Loopless Paths in a
       Network", Management Science, Vol. 17, No. 11, Theory Series
       (Jul., 1971), pp. 712-716.

    s   source node %s not in graphs   target node %s not in graphc            s*   t  ‡  ‡ f d †  t |  |  d ƒ Dƒ ƒ S(   Nc         3   s*   |  ]  \ } } ˆ  j  | | ˆ Vq d  S(   N(   t   edge(   R   R   R   (   R   t   weight(    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pys	   <genexpr>å   s    i   (   t   sumt   zip(   t   path(   R   R"   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   length_funcä   s    R"   i   iÿÿÿÿt   ignore_nodest   ignore_edgesN(   R   R   R	   R
   t   _bidirectional_shortest_patht   _bidirectional_dijkstraR   t
   PathBuffert   Truet   pusht   setR   t   addt   NetworkXNoPathR   R   (   R   R   R   R"   R&   t   shortest_path_funct   listAt   listBt	   prev_patht   lengthR%   R'   R(   R   t   roott   root_lengtht   spur(    (   R   R"   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR   Œ   sP    N						
&	R+   c           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s(   t  ƒ  |  _ t ƒ  |  _ t ƒ  |  _ d  S(   N(   R.   t   pathsR   t   sortedpathsR   t   counter(   t   self(    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   __init__  s    c         C   s   t  |  j ƒ S(   N(   R
   R:   (   R<   (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   __len__  s    c         C   sT   t  | ƒ } | |  j k rP t |  j | t |  j ƒ | f ƒ |  j j | ƒ n  d  S(   N(   t   tupleR9   R    R:   R   R;   R/   (   R<   t   costR%   t   hashable_path(    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR-     s    "c         C   s8   t  |  j ƒ \ } } } t | ƒ } |  j j | ƒ | S(   N(   R   R:   R?   R9   t   remove(   R<   R@   t   numR%   RA   (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR     s    (   t   __name__t
   __module__R=   R>   R-   R   (    (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR+     s   			c         C   s¢   t  |  | | | | ƒ } | \ } } }	 g  }
 x' |	 d k	 rV |
 j |	 ƒ | |	 }	 q0 W| |
 d }	 x* |	 d k	 r‘ |
 j d |	 ƒ | |	 }	 qh Wt |
 ƒ |
 f S(   s®  Return the shortest path between source and target ignoring
       nodes and edges in the containers ignore_nodes and ignore_edges.

    This is a custom modification of the standard bidirectional shortest 
    path implementation at networkx.algorithms.unweighted

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

    source : node
       starting node for path

    target : node
       ending node for path

    ignore_nodes : container of nodes
       nodes to ignore, optional

    ignore_edges : container of edges
       edges to ignore, optional

    weight : None
       This function accepts a weight argument for convinience of 
       shortest_simple_paths function. It will be ignored.

    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

    i    N(   t   _bidirectional_pred_succR	   R   t   insertR
   (   R   R   R   R'   R(   R"   t   resultst   predt   succt   wR%   (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR)   #  s    .c            s\  | | k r' i d | 6i d | 6| f S|  j ƒ  rH |  j } |  j } n |  j } |  j } ˆ rŠ ‡ f d †  } | | ƒ } | | ƒ } n  ˆ  rÿ |  j ƒ  rÕ ‡  f d †  } ‡  f d †  }	 | | ƒ } |	 | ƒ } qÿ ‡  f d †  } | | ƒ } | | ƒ } n  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 rx|
 | | f SqxWqeWq.| } g  } xd | D]\ } xS | | ƒ D]E } | | k r| | | <| j | ƒ n  | |
 k rî|
 | | f SqîWqÛ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.
    c            s   ‡ ‡  f d †  } | S(   Nc         3   s/   x( ˆ |  ƒ D] } | ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R'   t
   nodes_iter(    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   iteratex  s    (    (   RL   RM   (   R'   (   RL   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   filter_iterw  s    c            s   ‡ ‡  f d †  } | S(   Nc         3   s5   x. ˆ |  ƒ D]  } | |  f ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R(   t	   pred_iter(    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRM   …  s    (    (   RO   RM   (   R(   (   RO   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   filter_pred_iter„  s    c            s   ‡ ‡  f d †  } | S(   Nc         3   s5   x. ˆ |  ƒ D]  } |  | f ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R(   t	   succ_iter(    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRM   Œ  s    (    (   RQ   RM   (   R(   (   RQ   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   filter_succ_iter‹  s    c            s   ‡ ‡  f d †  } | S(   Nc         3   sG   x@ ˆ |  ƒ D]2 } |  | f ˆ  k r | |  f ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R(   RL   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRM   —  s    (    (   RL   RM   (   R(   (   RL   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRN   –  s    s   No path between %s and %s.N(	   R	   t   is_directedt   predecessors_itert   successors_itert   neighbors_iterR
   R   R   R0   (   R   R   R   R'   R(   t   Gpredt   GsuccRN   RP   RR   RI   RJ   t   forward_fringet   reverse_fringet
   this_levelR   RK   (    (   R(   R'   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRF   c  sZ    					
R"   c            sv  | | k r d | g f S|  j  ƒ  r: |  j } |  j } n |  j } |  j } ˆ r| ‡ f d †  } | | ƒ } | | ƒ } n  ˆ  rñ |  j  ƒ  rÇ ‡  f d †  }	 ‡  f d †  }
 |	 | ƒ } |
 | ƒ } qñ ‡  f d †  } | | ƒ } | | ƒ } n  t } 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 ƒ | | g } g  } d } x¦| d rX| d rXd | } | | | ƒ \ } } } | | | k r 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 rR| | | | k  rQt d	 ƒ ‚ qQq=| | | k sv| | | | k  r=| | | | <| | | | t | ƒ | f ƒ | | | | g | | | <| | d k rQ| | d k rQ| d | | d | } | g  k s| | k rN| } | d | } | j ƒ  | d | | d } qNqQq=q=Wq³Wt j d
 | | f ƒ ‚ d S(   sŸ  Dijkstra's algorithm for shortest paths using bidirectional search.

    This function returns the shortest path between source and target
    ignoring nodes and edges in the containers ignore_nodes and 
    ignore_edges.

    This is a custom modification of the standard Dijkstra bidirectional
    shortest path implementation at networkx.algorithms.weighted

    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

    ignore_nodes : container of nodes
       nodes to ignore, optional

    ignore_edges : container of edges
       edges to ignore, optional

    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.

    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    c            s   ‡ ‡  f d †  } | S(   Nc         3   s/   x( ˆ |  ƒ D] } | ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R'   RL   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRM     s    (    (   RL   RM   (   R'   (   RL   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRN     s    c            s   ‡ ‡  f d †  } | S(   Nc         3   s5   x. ˆ |  ƒ D]  } | |  f ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R(   RO   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRM   #  s    (    (   RO   RM   (   R(   (   RO   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRP   "  s    c            s   ‡ ‡  f d †  } | S(   Nc         3   s5   x. ˆ |  ƒ D]  } |  | f ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R(   RQ   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRM   *  s    (    (   RQ   RM   (   R(   (   RQ   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRR   )  s    c            s   ‡ ‡  f d †  } | S(   Nc         3   sG   x@ ˆ |  ƒ D]2 } |  | f ˆ  k r | |  f ˆ  k r | Vq q Wd  S(   N(    (   R   RK   (   R(   RL   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRM   5  s    (    (   RL   RM   (   R(   (   RL   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyRN   4  s    i   c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   t   get(   R   t   kt   dd(   R"   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pys	   <genexpr>e  s   c         3   s'   |  ] \ } } | j  ˆ  d  ƒ Vq d S(   i   N(   R\   (   R   R]   R^   (   R"   (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pys	   <genexpr>l  s   s,   Contradictory paths found: negative weights?s   No path between %s and %s.N(   RS   RT   RU   RV   R    R   R   R   R   t   mint   itemsR\   t
   ValueErrort   reverseR   R0   (   R   R   R   R"   R'   R(   RW   RX   RN   RP   RR   R-   R   t   distsR9   t   fringet   seent   ct   neighst	   finalpatht   dirt   distt   _R   t	   finaldistRK   t	   minweightt   vwLengtht	   totaldistt   revpath(    (   R(   R'   R"   st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyR*   Ä  sˆ    D			 	  

$  
'(   t   heapqR    R   t	   itertoolsR   t   networkxR   t   networkx.utilsR   t   joint
   __author__t   __all__R	   R   R   R   R   t   objectR+   R)   RF   R*   (    (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/simple_paths.pyt   <module>   s*   		D	=a