
    shh                        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                   :    \ rS 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     r/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/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   c                 h    [        U SS 5      U R                  4[        USS 5      UR                  4:H  $ )Nr   )getattrr   )r   others     r   __eq__CurrentEdge.__eq__-   s6    gt,dkk:UGT*ELL9
 	
r   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__	__slots__r   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   3   z%Active and inactive nodes in a level.)activeinactivec                 @    [        5       U l        [        5       U l        g r   )setr4   r5   r   s    r   r   Level.__init__8   s    er   N)r*   r+   r,   r-   r.   r/   r   r0   r1   r   r   r   r   3   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   =   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__B   s    ,015D.eEl
r   c                 .    U =R                   U-  sl         g r   r?   )r   works     r   add_workGlobalRelabelThreshold.add_workF   s    

d
r   c                 4    U R                   U R                  :  $ r   )r?   r>   r   s    r   
is_reached!GlobalRelabelThreshold.is_reachedI   s    zzT__,,r   c                     SU l         g )Nr   rE   r   s    r   
clear_work!GlobalRelabelThreshold.clear_workL   s	    
r   )r>   r?   N)
r*   r+   r,   r-   r.   r   rG   rJ   rM   r0   r1   r   r   r   r   =   s    -r   r   capacityr<   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).Nr<   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   r1   ).0uvattrrO   r<   s       r   	<genexpr>)build_residual_network.<locals>.<genexpr>   s;      
'
d4 $(Nc$9 DN's   +++   )rO   rO   )is_multigraphnxNetworkXErrorDiGraph__networkx_cache__add_nodes_fromr=   r   r   sumis_directedminhas_edgeadd_edgegraph)	GrO   RrW   rX   rY   	edge_listrr<   s	    `      @r   r   r   P   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(rj   )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.r<   rO   z-Infinite capacity path, flow unbounded above.N)r   rh   popleftr#   r^   NetworkXUnboundedaddappend)	rj   stqseenr<   rW   rX   rY   s	            r   r   r      s     	qc
A3D
''%.C
IIKtzz|GAJ3&1=6..G   $ !r   r\   )ri   rj   flow)rm   rn   c                     0 nU  HG  n[         R                  X   S5      X#'   X#   R                  S X   R                  5        5       5        MI     U$ )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)ry   r   Nr1   )rV   rX   rY   s      r   rZ   "build_flow_dict.<locals>.<genexpr>   s*      
-9'!T&\A=MQV\s   "")dictfromkeysupdater#   )ri   rj   	flow_dictrW   s       r   r	   r	      sT     I}}QT1-	 
-.TZZ\
 	
 
 r   )r.   collectionsr   networkxr^   __all__r   r   r   _dispatchabler=   r   r   r	   r1   r   r   <module>r      s     
 
@  & j%,7tLO MOd z5<89

" qq)fd^?TU Vr   