
    hi=                         S r SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	J
r
JrJrJr  S	/rS
 r\R                   " S\" S5      0SS9 SS j5       rg)zA
Highest-label preflow-push algorithm for maximum flow problems.
    )deque)isliceN   )arbitrary_element   )CurrentEdgeGlobalRelabelThresholdLevelbuild_residual_networkdetect_unboundednesspreflow_pushc                 X  ^^^^^^^^^^^^^^^ TU ;  a#  [         R                  " S[        T5       S35      eTU ;  a#  [         R                  " S[        T5       S35      eTT:X  a  [         R                  " S5      eUc  SnUS:  a  [         R                  " S5      eUc  [        X5      mOUm[	        TTT5        TR
                  mTR                  mTR                  mT H*  nSTU   S'   TU   R                  5        H  nSUS'   M
     M,     U4S jmT" T5      mTT;  a  STR                  S	'   T$ [        T5      m[        UU4S
 jT 5       5      mTTT'   [        TTR                  5       U5      mT H-  nUT;   a  TU   OTS-   TU   S'   [        TU   5      TU   S'   M/     UU4S jmTT   R                  5        H  u  pyU	S   n
U
S:  d  M  T" TXz5        M     [!        ST-  5       Vs/ s H  n[#        5       PM     snmT Hb  nUT:w  d  M  UT:w  d  M  TTU   S      nTU   S   S:  a  UR$                  R'                  U5        MG  UR(                  R'                  U5        Md     UUUU4S jmUUU4S jmUUUUUU4S jnUUUU4S jnUUUUUUU4S jnTnUS:  a   TU   nUR$                  (       d  US-  nOUnUn[+        UR$                  5      nU" US5      nTR-                  5       (       a  U" S5      nUmTR/                  5         O>UR$                  (       d!  UR(                  (       d  U" U5        US-
  nUmO[        TU5      mM  US:  a  M  U(       a  TT   S   TR                  S	'   T$ U" S5      nTR/                  5         UT:  ar   TU   nUR$                  (       d  US-  nOM[+        UR$                  5      nU" US5      nTR-                  5       (       a  U" S5      nTR/                  5         Mi  UT:  a  Mr  TT   S   TR                  S	'   T$ s  snf )z;Implementation of the highest-label preflow-push algorithm.znode z not in graphz!source and sink are the same noder   z(global_relabel_freq must be nonnegative.excessflowc                   > U S0n[        U S4/5      nU(       ag  UR                  5       u  p4US-  nTU   R                  5        H0  u  pVXQ;  d  M  US   US   :  d  M  XAU'   UR                  XT45        M2     U(       a  Mg  U$ )zIPerform a reverse breadth-first search from src in the residual
network.
r   r   r   capacity)r   popleftitemsappend)srcheightsquheightvattrR_preds          V/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/flow/preflowpush.pyreverse_bfs&preflow_push_impl.<locals>.reverse_bfs5   s     (C8*		IAaKF!!9??,#VtJ7G(G!'AJHHa[) - a     
flow_valuec              3   <   >#    U  H  oT:w  d  M
  TU   v   M     g 7f)N ).0r   r   ss     r   	<genexpr>$preflow_push_impl.<locals>.<genexpr>P   s     ;AFZWQZs   	r   r   	curr_edgec                    > TU    U   S==   U-  ss'   TU   U    S==   U-  ss'   TU    S==   U-  ss'   TU   S==   U-  ss'   g)z$Push flow units of flow from u to v.r   r   Nr$   )r   r   r   R_nodesR_succs      r   pushpreflow_push_impl.<locals>.pushZ   s[    q	!V$q	!V$
8$
8$r!   r      c                    > U T:w  aY  U T:w  aR  TTU    S      nXR                   ;   a7  UR                   R                  U 5        UR                  R                  U 5        gggg)zAMove a node from the inactive set to the active set of its level.r   N)inactiveremoveactiveadd)r   levelr+   levelsr&   ts     r   activate#preflow_push_impl.<locals>.activater   s]    6a1f71:h/0ENN"%%a(  # # 6r!   c                    > TR                  [        TU    5      5        [        U4S jTU    R                  5        5       5      S-   $ )z,Relabel a node to create an admissible edge.c              3   T   >#    U  H  u  pUS    US   :  d  M  TU   S   v   M     g7f)r   r   r   Nr$   )r%   r   r   r+   s      r   r'   5preflow_push_impl.<locals>.relabel.<locals>.<genexpr>~   s6      0GA<$z"22 %
8$0s   ((r   )add_worklenminr   )r   r+   r,   grts    r   relabel"preflow_push_impl.<locals>.relabelz   sF    S^$ %ay0 
 	
r!   c                 >  > TU    S   nTU    S   nUnT
U   R                   R                  U 5         UR                  5       u  pVUTU   S   S-   :X  ac  US   US   :  aW  [        TU    S   US   US   -
  5      nT" XU5        T	" U5        TU    S   S:X  a  T
U   R                  R                  U 5        O UR                  5         M  UTU    S'   U$ ! [         a>    T" U 5      nU(       a*  UTS-
  :  a!  T
U   R                   R                  U 5         MM  Un NSf = f)zDischarge a node until it becomes inactive or, during phase 1 (see
below), its height reaches at least n. The node is known to have the
largest height among active nodes.
r   r)   r   r   r   r   r   )r3   r2   getr?   r1   r4   move_to_nextStopIteration)r   	is_phase1r   r)   next_heightr   r   r   r+   r8   r6   nr-   rA   s           r   	discharge$preflow_push_impl.<locals>.discharge   sO   
 H%AJ{+	 v$$Q'mmoGAH-11d6lT*EU6U71:h/j1ADL1PQQ4 1:h'1,6N++//2%&&( 6  &
8! ! % !1q5 6N))--a0
 %%s   8C A DDDc                   > [        TU S-   TS-   5       H  nUR                   H  nTS-   TU   S'   M     UR                   H  nTS-   TU   S'   M     TTS-      R                  R                  UR                  5        UR                  R	                  5         TTS-      R                  R                  UR                  5        UR                  R	                  5         M     g)zApply the gap heuristic.r   r   N)r   r3   r1   updateclear)r   r5   r   r+   r6   
max_heightrI   s      r   gap_heuristic(preflow_push_impl.<locals>.gap_heuristic   s     FFQJ
Q?E\\'(1u
8$ "^^'(1u
8$ $1q5M  ''5LL 1q5M""))%..9NN  " @r!   c                 ~  > U (       a  TOTnT" U5      nU (       d  UT	 [        UR                  5       5      nU (       a&  T H  nXB;  d  M
  TU   S   T
:  d  M  T
S-   X$'   M!     OU H  nX$==   T
-  ss'   M     UT
-  nX!	 UR                  5        H  u  pETU   S   nXV:w  d  M  UT	U   R                  ;   a=  T	U   R                  R	                  U5        T	U   R                  R                  U5        O<T	U   R                  R	                  U5        T	U   R                  R                  U5        UTU   S'   M     U$ )z&Apply the global relabeling heuristic.r   r   )maxvaluesr   r3   r2   r4   r1   )	from_sinkr   r   rO   r   
new_height
old_heightRr+   r6   rI   r   r&   r7   s          r   global_relabel)preflow_push_impl.<locals>.global_relabel   s9   a!c"
)*
 #
8(<q(@!"QGJ 
 
a
 !OJL$]]_MA H-J'z*111:&--44Q7:&--11!4:&//66q9:&//33A6'1
8$ - r!   TF)nxNetworkXErrorstrr   r   nodespredsuccrT   graphr>   rS   r	   sizer   r   ranger
   r3   r4   r1   r   
is_reached
clear_work) Gr&   r7   r   residualglobal_relabel_freq
value_onlyr   er   r   ir5   rJ   rP   rY   r   rW   	old_levelrX   r+   r   r,   r8   r@   r   r6   rO   rI   r-   rA   r   s     ``                @@@@@@@@@@@@@r   preflow_push_implrm      s    zs1vhm<==zs1vhm<==AvBCC"QIJJ"1/Aq!ggGVVFVVF  
8!!#AAfI $ 
  !nG !"AA ;;;JGAJ
 AFFH.A
BC -.'\wqzq1u
8"-fQi"8
; % !9??$J!8A %  %QU|,|!eg|,F6a1f71:h/0Eqz(#a'  #""1% $ $

' 'R# # F F
1*6NE<< !JI!%,,/Aq$'F~~ (-#
 %%i.@.@
 j)#a#
 !V4
;  1*F  '
8 4 E"FNN 1*6NE<< !!%,,/Aq%(F~~'.   1*  $AJx0AGGLHo -s   3P'r   infT)
edge_attrsreturns_graphc           	      l    [        XX#XEU5      nSUR                  S'   [        R                  " U5        U$ )a/  Find a maximum single-commodity flow using the highest-label
preflow-push algorithm.

This function returns the residual network resulting after computing
the maximum flow. See below for details about the conventions
NetworkX uses for defining residual networks.

This algorithm has a running time of $O(n^2 \sqrt{m})$ for $n$ nodes and
$m$ edges.


Parameters
----------
G : NetworkX graph
    Edges of the graph are expected to have an attribute called
    'capacity'. If this attribute is not present, the edge is
    considered to have infinite capacity.

s : node
    Source node for the flow.

t : node
    Sink node for the flow.

capacity : string
    Edges of the graph G are expected to have an attribute capacity
    that indicates how much flow the edge can support. If this
    attribute is not present, the edge is considered to have
    infinite capacity. Default value: 'capacity'.

residual : NetworkX graph
    Residual network on which the algorithm is to be executed. If None, a
    new residual network is created. Default value: None.

global_relabel_freq : integer, float
    Relative frequency of applying the global relabeling heuristic to speed
    up the algorithm. If it is None, the heuristic is disabled. Default
    value: 1.

value_only : bool
    If False, compute a maximum flow; otherwise, compute a maximum preflow
    which is enough for computing the maximum flow value. Default value:
    False.

Returns
-------
R : NetworkX DiGraph
    Residual network after computing the maximum flow.

Raises
------
NetworkXError
    The algorithm does not support MultiGraph and MultiDiGraph. If
    the input graph is an instance of one of these two classes, a
    NetworkXError is raised.

NetworkXUnbounded
    If the graph has a path of infinite capacity, the value of a
    feasible flow on the graph is unbounded above and the function
    raises a NetworkXUnbounded.

See also
--------
:meth:`maximum_flow`
:meth:`minimum_cut`
:meth:`edmonds_karp`
:meth:`shortest_augmenting_path`

Notes
-----
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 node :samp:`u` in :samp:`R`,
:samp:`R.nodes[u]['excess']` represents the difference between flow into
:samp:`u` and flow out of :samp:`u`.

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']`. 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.

Examples
--------
>>> from networkx.algorithms.flow import preflow_push

The functions that implement flow algorithms and output a residual
network, such as this one, are not imported to the base NetworkX
namespace, so you have to explicitly import them from the flow package.

>>> G = nx.DiGraph()
>>> G.add_edge("x", "a", capacity=3.0)
>>> G.add_edge("x", "b", capacity=1.0)
>>> G.add_edge("a", "c", capacity=3.0)
>>> G.add_edge("b", "c", capacity=5.0)
>>> G.add_edge("b", "d", capacity=4.0)
>>> G.add_edge("d", "e", capacity=2.0)
>>> G.add_edge("c", "y", capacity=2.0)
>>> G.add_edge("e", "y", capacity=3.0)
>>> R = preflow_push(G, "x", "y")
>>> flow_value = nx.maximum_flow_value(G, "x", "y")
>>> flow_value == R.graph["flow_value"]
True
>>> # preflow_push also stores the maximum flow value
>>> # in the excess attribute of the sink node t
>>> flow_value == R.nodes["y"]["excess"]
True
>>> # For some problems, you might only want to compute a
>>> # maximum preflow.
>>> R = preflow_push(G, "x", "y", value_only=True)
>>> flow_value == R.graph["flow_value"]
True
>>> flow_value == R.nodes["y"]["excess"]
True

r   	algorithm)rm   ra   r[   _clear_cache)rf   r&   r7   r   rg   rh   ri   rX   s           r   r   r   #  s4    F 	!XJWA)AGGKOOAHr!   )r   Nr   F)__doc__collectionsr   	itertoolsr   networkxr[   utilsr   r   r	   r
   r   r   __all__rm   _dispatchablefloatr   r$   r!   r   <module>r|      sc       &  
JZ j%,7tLSXE MEr!   