
    h                        S r SSKJr  SSKr/ SQr " S S5      r " S S5      r " S	 S
5      r\R                  " S\
" S5      0SS9S 5       r\R                  " SSS\
" S5      00SS9S 5       r\R                  " SSS.SSS00S9S 5       rg)z<
Utility classes and functions for network flow algorithms.
    )dequeN)CurrentEdgeLevelGlobalRelabelThresholdbuild_residual_networkdetect_unboundednessbuild_flow_dictc                   4    \ rS rSrSrSrS rS rS rS r	Sr
g	)
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                 T    Xl         U R                   (       a  U R                  5         g g N)r   _rewind)selfedgess     P/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/flow/utils.py__init__CurrentEdge.__init__   s    ;;LLN     c                     U R                   $ r   )r   r   s    r   getCurrentEdge.get   s    zzr   c                 x     [        U R                  5      U l        g ! [         a    U R	                  5         e f = fr   )nextr   r   StopIterationr   r   s    r   move_to_nextCurrentEdge.move_to_next"   s1    	dhhDJ 	LLN	s    9c                     [        U R                  R                  5       5      U l        [	        U R                  5      U l        g r   )iterr   itemsr   r   r   r   s    r   r   CurrentEdge._rewind)   s*    ))+,$((^
r   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__	__slots__r   r   r   r   __static_attributes__ r   r   r   r      s"     +I
$r   r   c                   "    \ rS rSrSrSrS rSrg)r   .   z%Active and inactive nodes in a level.)activeinactivec                 @    [        5       U l        [        5       U l        g r   )setr/   r0   r   s    r   r   Level.__init__3   s    er   N)r%   r&   r'   r(   r)   r*   r   r+   r,   r   r   r   r   .   s    /&Ir   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   8   zNMeasurement of work before the global relabeling heuristic should be
applied.
c                 N    U(       a  X-   U-  O
[        S5      U l        SU l        g )Ninfr   )float
_threshold_work)r   nmfreqs       r   r   GlobalRelabelThreshold.__init__=   s    ,015D.eEl
r   c                 .    U =R                   U-  sl         g r   r:   )r   works     r   add_workGlobalRelabelThreshold.add_workA   s    

d
r   c                 4    U R                   U R                  :  $ r   )r:   r9   r   s    r   
is_reached!GlobalRelabelThreshold.is_reachedD   s    zzT__,,r   c                     SU l         g )Nr   r@   r   s    r   
clear_work!GlobalRelabelThreshold.clear_workG   s	    
r   )r9   r:   N)
r%   r&   r'   r(   r)   r   rB   rE   rH   r+   r,   r   r   r   r   8   s    -r   r   capacityr7   T)
edge_attrsreturns_graphc           	      ^  ^^ U R                  5       (       a  [        R                  " S5      e[        R                  " 5       nSUl        UR                  U 5        [        S5      mU R                  SS9 VVVs/ s H)  u  p4nX4:w  d  M  UR                  TT5      S:  d  M%  X4U4PM+     nnnnS[        UU4S jU 5       5      -  =(       d    S	mU R                  5       (       ak  U Hd  u  p4n[        UR                  TT5      T5      nUR                  X45      (       d"  UR                  X4US
9  UR                  XCSS
9  MZ  XrU   U   S'   Mf     OHU HB  u  p4n[        UR                  TT5      T5      nUR                  X4US
9  UR                  XCUS
9  MD     TUR                  S'   U$ s  snnnf )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).Nr7   T)datar      c              3   Z   >#    U  H   u  pnTU;   d  M  UT   T:w  d  M  UT   v   M"     g 7fr   r,   ).0uvattrrJ   r7   s       r   	<genexpr>)build_residual_network.<locals>.<genexpr>   s;      
'
d4 $(Nc$9 DN's   +++   )rJ   rJ   )is_multigraphnxNetworkXErrorDiGraph__networkx_cache__add_nodes_fromr8   r   r   sumis_directedminhas_edgeadd_edgegraph)	GrJ   RrR   rS   rT   	edge_listrr7   s	    `      @r   r   r   K   s   4 	QRR


AAQ
,C ''t',,JA$6 	hhx-1 	t,    	

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

1!
,

1!
, '(!Q
# $ $JA$DHHXs+S1AJJqaJ(JJqaJ(	 $ AGGENH[s   9F(	F(!F(re   )graphspreserve_edge_attrspreserve_graph_attrsc                 V   [        U/5      nU1nU R                  S   nU(       a  UR                  5       nX   R                  5        HT  u  pxUS   U:X  d  M  Xt;  d  M  Xr:X  a  [        R
                  " S5      eUR                  U5        UR                  U5        MV     U(       a  M  gg)z*Detect an infinite-capacity s-t path in R.r7   rJ   z-Infinite capacity path, flow unbounded above.N)r   rc   popleftr#   rY   NetworkXUnboundedaddappend)	re   stqseenr7   rR   rS   rT   s	            r   r   r      s     	qc
A3D
''%.C
IIKtzz|GAJ3&1=6..G   $ !r   rW   )rd   re   flow)rh   ri   c                     0 nU  HD  nX    Vs0 s H  oDS_M     snX#'   X#   R                  S X   R                  5        5       5        MF     U$ s  snf )z0Build a flow dictionary from a residual network.r   c              3   H   #    U  H  u  pUS    S:  d  M  XS    4v   M     g7f)rt   r   Nr,   )rQ   rS   rT   s      r   rU   "build_flow_dict.<locals>.<genexpr>   s*      
-9'!T&\A=MQV\s   "")updater#   )rd   re   	flow_dictrR   rS   s        r   r	   r	      s_     I&'d+d1d+	 
-.TZZ\
 	
 
 	 ,s   A)r)   collectionsr   networkxrY   __all__r   r   r   _dispatchabler8   r   r   r	   r,   r   r   <module>r~      s     $ $6  & j%,7tLO MOd z5<89

" qq)fd^?TU Vr   