
    wg                     0   d Z ddlmZ ddlZg dZ G d d      Z G d d      Z G d	 d
      Z ej                  d e
d      id      d        Z ej                  ddd e
d      iid      d        Z ej                  ddddddii      d        Zy)z<
Utility classes and functions for network flow algorithms.
    )dequeN)CurrentEdgeLevelGlobalRelabelThresholdbuild_residual_networkdetect_unboundednessbuild_flow_dictc                   ,    e Zd ZdZdZd Zd Zd Zd Zy)r   zMechanism for iterating over out-edges incident to a node in a circular
    manner. StopIteration exception is raised when wraparound occurs.
    )_edges_it_currc                 L    || _         | j                   r| j                          y y N)r   _rewind)selfedgess     c/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/flow/utils.py__init__zCurrentEdge.__init__   s    ;;LLN     c                     | j                   S r   )r   r   s    r   getzCurrentEdge.get   s    zzr   c                 v    	 t        | j                        | _        y # t        $ r | j	                           w xY wr   )nextr   r   StopIterationr   r   s    r   move_to_nextzCurrentEdge.move_to_next"   s1    	dhhDJ 	LLN	s    8c                     t        | j                  j                               | _        t	        | j                        | _        y r   )iterr   itemsr   r   r   r   s    r   r   zCurrentEdge._rewind)   s*    ))+,$((^
r   N)	__name__
__module____qualname____doc__	__slots__r   r   r   r    r   r   r   r      s"     +I
$r   r   c                       e Zd ZdZdZd Zy)r   z%Active and inactive nodes in a level.)activeinactivec                 @    t               | _        t               | _        y r   )setr'   r(   r   s    r   r   zLevel.__init__3   s    er   N)r    r!   r"   r#   r$   r   r%   r   r   r   r   .   s    /&Ir   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   zVMeasurement of work before the global relabeling heuristic should be
    applied.
    c                 F    |r||z   |z  n
t        d      | _        d| _        y )Ninfr   )float
_threshold_work)r   nmfreqs       r   r   zGlobalRelabelThreshold.__init__=   s     ,01q5D.eEl
r   c                 .    | xj                   |z  c_         y r   r0   )r   works     r   add_workzGlobalRelabelThreshold.add_workA   s    

d
r   c                 4    | j                   | j                  k\  S r   )r0   r/   r   s    r   
is_reachedz!GlobalRelabelThreshold.is_reachedD   s    zzT__,,r   c                     d| _         y )Nr   r5   r   s    r   
clear_workz!GlobalRelabelThreshold.clear_workG   s	    
r   N)r    r!   r"   r#   r   r7   r9   r;   r%   r   r   r   r   8   s    -r   r   capacityr-   T)
edge_attrsreturns_graphc           	      H   | j                         rt        j                  d      t        j                         }d|_        |j                  |        t        d      | j                  d      D cg c]%  \  }}}||k7  r|j                        dkD  r|||f' }}}}dt        fd|D              z  xs d	| j                         rn|D ]h  \  }}}t        |j                              }|j                  ||      s)|j                  |||
       |j                  ||d
       ^|||   |   d<   j nO|D ]J  \  }}}t        |j                              }|j                  |||
       |j                  |||
       L |j                  d<   |S c c}}}w )a  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.

    z0MultiGraph and MultiDiGraph not supported (yet).Nr-   T)datar      c              3   H   K   | ]  \  }}}|v r|   k7  r|     y wr   r%   ).0uvattrr<   r-   s       r   	<genexpr>z)build_residual_network.<locals>.<genexpr>   s6      
1d4DNc$9 N
s   "   )r<   r<   )is_multigraphnxNetworkXErrorDiGraph__networkx_cache__add_nodes_fromr.   r   r   sumis_directedminhas_edgeadd_edgegraph)	Gr<   RrD   rE   rF   	edge_listrr-   s	    `      @r   r   r   K   s   4 	QRR


AAQ
,C ''t', Aq$6dhhx-1 
AtI   	

 
'
 
	
 	   	}}# 		(JAq$DHHXs+S1A::a# 

1a!
,

1a!
, '(!Q
#		( $ 	)JAq$DHHXs+S1AJJq!aJ(JJq!aJ(		) AGGENH[s   5*FrV   )graphspreserve_edge_attrspreserve_graph_attrsc                 6   t        |g      }|h}| j                  d   }|ry|j                         }| |   j                         D ]O  \  }}|d   |k(  s||vs||k(  rt	        j
                  d      |j                  |       |j                  |       Q |rxyy)z*Detect an infinite-capacity s-t path in R.r-   r<   z-Infinite capacity path, flow unbounded above.N)r   rT   popleftr   rJ   NetworkXUnboundedaddappend)	rV   stqseenr-   rD   rE   rF   s	            r   r   r      s     	qc
A3D
''%.C
IIKtzz| 	GAtJ3&1D=6..G  	 r   rH   )rU   rV   flow)rY   rZ   c                     i }| D ]E  }| |   D ci c]  }|d c}||<   ||   j                  d ||   j                         D               G |S c c}w )z0Build a flow dictionary from a residual network.r   c              3   B   K   | ]  \  }}|d    dkD  s||d    f  yw)re   r   Nr%   )rC   rE   rF   s      r   rG   z"build_flow_dict.<locals>.<genexpr>   s-      
")!TT&\A=MQV
s   )updater   )rU   rV   	flow_dictrD   rE   s        r   r	   r	      sj     I 
&'d+1+	!! 
-.qTZZ\
 	


 	 ,s   
A)r#   collectionsr   networkxrJ   __all__r   r   r   _dispatchabler.   r   r   r	   r%   r   r   <module>rn      s     $ $6  & j%,7tLO MOd z5<89

" qq)fd^?TU Vr   