
    \Mhh                     V   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dS )z<
Utility classes and functions for network flow algorithms.
    )dequeN)CurrentEdgeLevelGlobalRelabelThresholdbuild_residual_networkdetect_unboundednessbuild_flow_dictc                   4    e Zd ZdZdZd Zd Zd Zd Zd Z	dS )	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                 N    || _         | j         r|                                  d S d S N)r   _rewind)selfedgess     ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/algorithms/flow/utils.py__init__zCurrentEdge.__init__   s.    ; 	LLNNNNN	 	    c                     | j         S r   )r   r   s    r   getzCurrentEdge.get   s
    zr   c                     	 t          | j                  | _        d S # t          $ r |                                   w xY wr   )nextr   r   StopIterationr   r   s    r   move_to_nextzCurrentEdge.move_to_next"   sE    	dhDJJJ 	 	 	LLNNN	s     =c                     t          | j                                                  | _        t	          | j                  | _        d S r   )iterr   itemsr   r   r   r   s    r   r   zCurrentEdge._rewind)   s2    ))++,,$(^^


r   c                 f    t          | dd           | j        ft          |dd           |j        fk    S )Nr   )getattrr   )r   others     r   __eq__zCurrentEdge.__eq__-   s8    gt,,dk:UGT**EL9
 	
r   N)
__name__
__module____qualname____doc__	__slots__r   r   r   r   r#    r   r   r   r      sp          +I  
    $ $ $
 
 
 
 
r   r   c                       e Zd ZdZdZd ZdS )r   z%Active and inactive nodes in a level.)activeinactivec                 R    t                      | _        t                      | _        d S r   )setr+   r,   r   s    r   r   zLevel.__init__8   s    eer   N)r$   r%   r&   r'   r(   r   r)   r   r   r   r   3   s.        //&I    r   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )r   zVMeasurement of work before the global relabeling heuristic should be
    applied.
    c                 P    |r||z   |z  nt          d          | _        d| _        d S )Ninfr   )float
_threshold_work)r   nmfreqs       r   r   zGlobalRelabelThreshold.__init__B   s+    ,0B1q5D..eEll


r   c                 &    | xj         |z  c_         d S r   r4   )r   works     r   add_workzGlobalRelabelThreshold.add_workF   s    

d



r   c                 "    | j         | j        k    S r   )r4   r3   r   s    r   
is_reachedz!GlobalRelabelThreshold.is_reachedI   s    zT_,,r   c                     d| _         d S )Nr   r9   r   s    r   
clear_workz!GlobalRelabelThreshold.clear_workL   s    


r   N)r$   r%   r&   r'   r   r;   r=   r?   r)   r   r   r   r   =   sZ             - - -    r   r   capacityr1   T)
edge_attrsreturns_graphc                 `   |                                  rt          j        d          t          j                    }d|_        |                    |            t          d          fd|                     d          D             }dt          fd|D                       z  pd	| 	                                r|D ]\  }}}t          |                                        }|                    ||          s1|                    |||
           |                    ||d
           q|||         |         d<   n]|D ]Z\  }}}t          |                                        }|                    |||
           |                    |||
           [|j        d<   |S )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).Nr1   c                 b    g | ]+\  }}}||k    |                               d k    &|||f,S )r   )r   .0uvattrr@   r1   s       r   
<listcomp>z*build_residual_network.<locals>.<listcomp>s   sP       Aq$66dhhx--11 
At111r   T)data   c              3   P   K   | ] \  }}}|v 
|         k    |         V  !d S r   r)   rE   s       r   	<genexpr>z)build_residual_network.<locals>.<genexpr>   sR       
 
1d4DNc$9$9 N$9$9$9$9
 
r      )r@   r   r@   )is_multigraphnxNetworkXErrorDiGraph__networkx_cache__add_nodes_fromr2   r   sumis_directedminr   has_edgeadd_edgegraph)	Gr@   R	edge_listrG   rH   rI   rr1   s	    `      @r   r   r   P   s   4 	 SQRRR

AAQ
,,C    ''t',,  I  	

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

1a!
,,,

1a!
,,,, '(!Q
##		( $ 	) 	)JAq$DHHXs++S11AJJq!aJ(((JJq!aJ(((( AGENHr   r]   )graphspreserve_edge_attrspreserve_graph_attrsc                 b   t          |g          }|h}| j        d         }|r|                                }| |                                         D ]Y\  }}|d         |k    rH||vrD||k    rt	          j        d          |                    |           |                    |           Z|dS dS )z*Detect an infinite-capacity s-t path in R.r1   r@   z-Infinite capacity path, flow unbounded above.N)r   r[   popleftr   rQ   NetworkXUnboundedaddappend)	r]   stqseenr1   rG   rH   rI   s	            r   r   r      s     	qc

A3D
'%.C
 	IIKKtzz|| 	 	GAtJ3&&1D==66.G     	 	 	 	 	r   rO   )r\   r]   flow)r`   ra   c                     i }| D ]c}t                               | |         d          ||<   ||                             d ||                                         D                        d|S )z0Build a flow dictionary from a residual network.r   c              3   H   K   | ]\  }}|d          dk    ||d          fV  dS )rl   r   Nr)   )rF   rH   rI   s      r   rN   z"build_flow_dict.<locals>.<genexpr>   sF       
 
")!TT&\A=M=MQV=M=M=M=M
 
r   )dictfromkeysupdater   )r\   r]   	flow_dictrG   s       r   r	   r	      s     I 
 
}}QqT1--	!! 
 
-.qTZZ\\
 
 
 	
 	
 	
 	
 r   )r'   collectionsr   networkxrQ   __all__r   r   r   _dispatchabler2   r   r   r	   r)   r   r   <module>rw      s               
 
 
 
 
 
 
 
@              & j%%,,7tLLLO O MLOd z55<<89  
  
" qq))fd^?TUUU  VU  r   