ó
 ,µ[c           @   s¡   d  Z  d Z d d l m Z d d l Z d d d d d	 d
 g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d „  Z d „  Z d „  Z d S(   s<   
Utility classes and functions for network flow algorithms.
s&   ysitu <ysitu@users.noreply.github.com>iÿÿÿÿ(   t   dequeNt   CurrentEdget   Levelt   GlobalRelabelThresholdt   build_residual_networkt   detect_unboundednesst   build_flow_dictc           B   s8   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z RS(	   s’   Mechanism for iterating over out-edges incident to a node in a circular
    manner. StopIteration exception is raised when wraparound occurs.
    t   _edgest   _itt   _currc         C   s#   | |  _  |  j  r |  j ƒ  n  d  S(   N(   R   t   _rewind(   t   selft   edges(    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyt   __init__   s    		c         C   s   |  j  S(   N(   R	   (   R   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyt   get   s    c         C   s;   y t  |  j ƒ |  _ Wn t k
 r6 |  j ƒ  ‚  n Xd  S(   N(   t   nextR   R	   t   StopIterationR
   (   R   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyt   move_to_next    s
    
c         C   s.   t  |  j j ƒ  ƒ |  _ t |  j ƒ |  _ d  S(   N(   t   iterR   t   itemsR   R   R	   (   R   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR
   '   s    (   R   R   R	   (   t   __name__t
   __module__t   __doc__t	   __slots__R   R   R   R
   (    (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR      s   			c           B   s   e  Z d  Z d Z d „  Z RS(   s*   Active and inactive nodes in a level.
    t   activet   inactivec         C   s   t  ƒ  |  _ t  ƒ  |  _ d  S(   N(   t   setR   R   (   R   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR   1   s    (   R   R   (   R   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR   ,   s   c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sV   Measurement of work before the global relabeling heuristic should be
    applied.
    c         C   s0   | r | | | n	 t  d ƒ |  _ d |  _ d  S(   Nt   infi    (   t   floatt
   _thresholdt   _work(   R   t   nt   mt   freq(    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR   ;   s    #c         C   s   |  j  | 7_  d  S(   N(   R   (   R   t   work(    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyt   add_work?   s    c         C   s   |  j  |  j k S(   N(   R   R   (   R   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyt
   is_reachedB   s    c         C   s   d |  _  d  S(   Ni    (   R   (   R   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyt
   clear_workE   s    (   R   R   R   R   R#   R$   R%   (    (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR   6   s
   			c            sÐ  |  j  ƒ  r t j d ƒ ‚ n  t j ƒ  } | j |  ƒ t d ƒ ‰ g  |  j d t ƒ D]B \ } } } | | k rV | j ˆ  ˆ ƒ d k rV | | | f ^ qV } d t	 ‡  ‡ f d †  | Dƒ ƒ pÄ d ‰ |  j
 ƒ  r^xé | D]} \ } } } t | j ˆ  ˆ ƒ ˆ ƒ } | j | | ƒ sE| j | | d | ƒ| j | | d d ƒqÚ | | | | d <qÚ Wna x^ | D]V \ } } } t | j ˆ  ˆ ƒ ˆ ƒ } | j | | d | ƒ| j | | d | ƒqeWˆ | j d <| S(	   sù  Build a residual network and initialize a zero flow.

    The residual network :samp:`R` from an input graph :samp:`G` has the
    same nodes as :samp:`G`. :samp:`R` is a DiGraph that contains a pair
    of edges :samp:`(u, v)` and :samp:`(v, u)` iff :samp:`(u, v)` is not a
    self-loop, and at least one of :samp:`(u, v)` and :samp:`(v, u)` exists
    in :samp:`G`.

    For each edge :samp:`(u, v)` in :samp:`R`, :samp:`R[u][v]['capacity']`
    is equal to the capacity of :samp:`(u, v)` in :samp:`G` if it exists
    in :samp:`G` or zero otherwise. If the capacity is infinite,
    :samp:`R[u][v]['capacity']` will have a high arbitrary finite value
    that does not affect the solution of the problem. This value is stored in
    :samp:`R.graph['inf']`. For each edge :samp:`(u, v)` in :samp:`R`,
    :samp:`R[u][v]['flow']` represents the flow function of :samp:`(u, v)` and
    satisfies :samp:`R[u][v]['flow'] == -R[v][u]['flow']`.

    The flow value, defined as the total flow into :samp:`t`, the sink, is
    stored in :samp:`R.graph['flow_value']`. If :samp:`cutoff` is not
    specified, reachability to :samp:`t` using only edges :samp:`(u, v)` such
    that :samp:`R[u][v]['flow'] < R[u][v]['capacity']` induces a minimum
    :samp:`s`-:samp:`t` cut.

    s0   MultiGraph and MultiDiGraph not supported (yet).R   t   datai    i   c         3   s>   |  ]4 \ } } } ˆ  | k r | ˆ  ˆ k r | ˆ  Vq d  S(   N(    (   t   .0t   ut   vt   attr(   t   capacityR   (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pys	   <genexpr>w   s    i   R+   (   t   is_multigrapht   nxt   NetworkXErrort   DiGrapht   add_nodes_fromR   R   t   TrueR   t   sumt   is_directedt   mint   has_edget   add_edget   graph(   t   GR+   t   RR(   R)   R*   t	   edge_listt   r(    (   R+   R   s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR   I   s.    "9#c   	      C   sÀ   t  | g ƒ } t | g ƒ } |  j d } xŽ | r» | j ƒ  } xu |  | j ƒ  D]c \ } } | d | k rQ | | k rQ | | k r— t j d ƒ ‚ n  | j | ƒ | j | ƒ qQ qQ Wq. Wd S(   s/   Detect an infinite-capacity s-t path in R.
    R   R+   s-   Infinite capacity path, flow unbounded above.N(	   R    R   R7   t   popleftR   R-   t   NetworkXUnboundedt   addt   append(	   R9   t   st   tt   qt   seenR   R(   R)   R*   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR   ‘   s    	c         C   sX   i  } xK |  D]C } d „  |  | Dƒ | | <| | j  d „  | | j ƒ  Dƒ ƒ q W| S(   s5   Build a flow dictionary from a residual network.
    c         S   s   i  |  ] } d  | “ q S(   i    (    (   R'   R)   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pys
   <dictcomp>§   s   	 c         s   s5   |  ]+ \ } } | d  d k r | | d  f Vq d S(   t   flowi    N(    (   R'   R)   R*   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pys	   <genexpr>¨   s    	(   t   updateR   (   R8   R9   t	   flow_dictR(   (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyR   ¢   s
    )(   R   t
   __author__t   collectionsR    t   networkxR-   t   __all__t   objectR   R   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/networkx/algorithms/flow/utils.pyt   <module>   s   	
	H	