§
    bŠtji=  ã                   ó®   — d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	m
Z
mZmZmZ d	gZd
„ Z ej        d ed¦  «        id¬¦  «        	 dd„¦   «         ZdS )zA
Highest-label preflow-push algorithm for maximum flow problems.
é    )Údeque)ÚisliceNé   )Úarbitrary_elementé   )ÚCurrentEdgeÚGlobalRelabelThresholdÚLevelÚbuild_residual_networkÚdetect_unboundednessÚpreflow_pushc                 óF  ‡‡‡‡‡‡‡‡‡‡‡‡‡‡‡— ‰| vr%t          j        dt          ‰¦  «        › d�¦  «        ‚‰| vr%t          j        dt          ‰¦  «        › d�¦  «        ‚‰‰k    rt          j        d¦  «        ‚|€d}|dk     rt          j        d¦  «        ‚|€t          | |¦  «        Šn|Št	          ‰‰‰¦  «         ‰j        Š‰j        Š‰j        Š‰D ]/}d‰|         d<   ‰|                              ¦   «         D ]}d|d<   ŒŒ0ˆfd	„Š ‰‰¦  «        Š‰‰vrd‰j	        d
<   ‰S t          ‰¦  «        Št          ˆˆfd„‰D ¦   «         ¦  «        Š‰‰‰<   t          ‰‰                     ¦   «         |¦  «        Š‰D ]:}|‰v r‰|         n‰dz   ‰|         d<   t          ‰|         ¦  «        ‰|         d<   Œ;ˆˆfd„Š‰‰                              ¦   «         D ] \  }}	|	d         }
|
dk    r ‰‰||
¦  «         Œ!d„ t!          d‰z  ¦  «        D ¦   «         Š‰D ]i}|‰k    ra|‰k    r[‰‰|         d                  }‰|         d         dk    r|j                             |¦  «         ŒO|j                             |¦  «         Œjˆˆˆˆfd„Šˆˆˆfd„Šˆˆˆˆˆˆfd„}ˆˆˆˆfd„}ˆˆˆˆˆˆˆfd„}‰}|dk    r¨	 ‰|         }|j        s|dz  }nŒ|}|}t)          |j        ¦  «        } ||d¦  «        }‰                     ¦   «         r" |d¦  «        }|Š‰                     ¦   «          n1|j        s|j        s ||¦  «         |dz
  }|Šnt          ‰|¦  «        ŠŒ¡|dk    °¨|r‰‰         d         ‰j	        d
<   ‰S  |d¦  «        }‰                     ¦   «          |‰k    rp	 ‰|         }|j        s|dz  }nTt)          |j        ¦  «        } ||d¦  «        }‰                     ¦   «         r |d¦  «        }‰                     ¦   «          Œi|‰k    °p‰‰         d         ‰j	        d
<   ‰S )z;Implementation of the highest-label preflow-push algorithm.znode z not in graphz!source and sink are the same nodeNr   z(global_relabel_freq must be nonnegative.ÚexcessÚflowc                 ó  •— | di}t          | dfg¦  «        }|rp|                     ¦   «         \  }}|dz  }‰|                              ¦   «         D ]7\  }}||vr.|d         |d         k     r|||<   |                     ||f¦  «         Œ8|°p|S )zYPerform 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          €úb/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/algorithms/flow/preflowpush.pyÚreverse_bfsz&preflow_push_impl.<locals>.reverse_bfs5   s³   ø€ ð ˜�(ˆÝ�C˜�8�*ÑÔˆØð 	*ØŸ	š	™œ‰IˆAˆvØ�a‰KˆFØ! !œ9Ÿ?š?Ñ,Ô,ð *ð *‘��4Ø˜GÐ#Ð#¨¨V¬°t¸JÔ7GÒ(GÐ(GØ!'�G˜A‘JØ—H’H˜a ˜[Ñ)Ô)Ð)øð ð 	*ð ˆó    Ú
flow_valuec              3   ó4   •K  — | ]}|‰k    ¯‰|         V — Œd S )N© )Ú.0r   r   Úss     €€r   ú	<genexpr>z$preflow_push_impl.<locals>.<genexpr>P   s+   øè è € Ð;Ð; A°A¸²F°F�W˜Q”Z°F°F°F°FÐ;Ð;r    r   r   Ú	curr_edgec                 óÐ   •— ‰|          |         dxx         |z  cc<   ‰|         |          dxx         |z  cc<   ‰|          dxx         |z  cc<   ‰|         dxx         |z  cc<   dS )z$Push flow units of flow from u to v.r   r   Nr#   )r   r   r   ÚR_nodesÚR_succs      €€r   Úpushzpreflow_push_impl.<locals>.pushZ   s‘   ø€ àˆqŒ	�!Œ�VÐÐÔ Ñ$ÐÐÑØˆqŒ	�!Œ�VÐÐÔ Ñ$ÐÐÑØ�Œ
�8ÐÐÔ Ñ$ÐÐÑØ�Œ
�8ÐÐÔ Ñ$ÐÐÑÐÐr    r   c                 ó*   — g | ]}t          ¦   «         ‘ŒS r#   )r
   )r$   Úis     r   ú
<listcomp>z%preflow_push_impl.<locals>.<listcomp>i   s   € Ð,Ð,Ð,˜!�e‰gŒgÐ,Ð,Ð,r    é   c                 óÎ   •— | ‰k    rY| ‰k    rU‰‰|          d                  }| |j         v r:|j                              | ¦  «         |j                             | ¦  «         dS dS dS dS )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   Úactivatez#preflow_push_impl.<locals>.activater   sw   ø€ à�Š6ˆ6�a˜1’f�fØ˜7 1œ: hÔ/Ô0ˆEØ�E”NÐ"Ð"Ø”×%Ò% aÑ(Ô(Ð(Ø”× Ò  Ñ#Ô#Ð#Ð#Ð#ð	 ˆ6�f�fà"Ð"r    c                 óÀ   •— ‰                      t          ‰|          ¦  «        ¦  «         t          ˆfd„‰|                               ¦   «         D ¦   «         ¦  «        dz   S )z,Relabel a node to create an admissible edge.c              3   ó^   •K  — | ]'\  }}|d          |d         k     ¯‰|         d         V — Œ(dS )r   r   r   Nr#   )r$   r   r   r)   s      €r   r&   z5preflow_push_impl.<locals>.relabel.<locals>.<genexpr>~   sP   øè è € ð ð á�A�tØ˜”< $ zÔ"2Ò2Ð2ð ˜”
˜8Ô$à2Ð2Ð2Ð2ðð r    r   )Úadd_workÚlenÚminr   )r   r)   r*   Úgrts    €€€r   Úrelabelz"preflow_push_impl.<locals>.relabelz   so   ø€ à�Š•S˜ œ‘^”^Ñ$Ô$Ð$åð ð ð ð à% aœyŸšÑ0Ô0ðñ ô ñ ô ð
 ñð	
r    c                 óÈ  •— ‰|          d         }‰|          d         }|}‰
|         j                              | ¦  «         	 |                     ¦   «         \  }}|‰|         d         dz   k    rˆ|d         |d         k     rvt          ‰|          d         |d         |d         z
  ¦  «        } ‰| ||¦  «          ‰	|¦  «         ‰|          d         dk    r!‰
|         j                             | ¦  «         nb	 |                     ¦   «          nJ# t          $ r=  ‰| ¦  «        }|r+|‰dz
  k    r"‰
|         j                              | ¦  «         Y n
|}Y nw xY w�Œ|‰|          d<   |S )	zÃDischarge 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'   Tr   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+   r?   s           €€€€€€r   Ú	dischargez$preflow_push_impl.<locals>.discharge†   sž  ø€ ð
 ˜”˜HÔ%ˆØ˜A”J˜{Ô+ˆ	ð ˆØˆvŒÔ×$Ò$ QÑ'Ô'Ð'ð	%Ø—m’m‘o”o‰GˆAˆtØ˜ œ HÔ-°Ñ1Ò1Ð1°d¸6´lÀTÈ*ÔEUÒ6UÐ6UÝ˜7 1œ: hÔ/°°jÔ1AÀDÈÄLÑ1PÑQÔQ�Ø��Q˜˜4Ñ Ô Ð Ø�˜‘”�Ø˜1”:˜hÔ'¨1Ò,Ð,à˜6”NÔ+×/Ò/°Ñ2Ô2Ð2Øð%Ø×&Ò&Ñ(Ô(Ð(Ð(øÝ ð %ð %ð %ð !˜ ™œ�Øð  ¨1¨q©5¢ ð ˜6”NÔ)×-Ò-¨aÑ0Ô0Ð0Ø�Eð
 %���ð%øøøñ	%ð6  &ˆ�Œ
�8ÑØÐs   Ã6D ÄA EÅEÅEc                 ó   •— t          ‰| dz   ‰dz   ¦  «        D ]´}|j        D ]}‰dz   ‰|         d<   Œ|j        D ]}‰dz   ‰|         d<   Œ‰‰dz            j                             |j        ¦  «         |j                             ¦   «          ‰‰dz            j                             |j        ¦  «         |j                             ¦   «          ŒµdS )zApply the gap heuristic.r   r   N)r   r3   r1   ÚupdateÚclear)r   r5   r   r)   r6   Ú
max_heightrF   s      €€€€r   Úgap_heuristicz(preflow_push_impl.<locals>.gap_heuristic¯   së   ø€ õ ˜F F¨Q¡J°
¸Q±Ñ?Ô?ð 	#ð 	#ˆEØ”\ð -ð -�Ø'(¨1¡u�˜”
˜8Ñ$Ð$Ø”^ð -ð -�Ø'(¨1¡u�˜”
˜8Ñ$Ð$Ø�1�q‘5ŒMÔ ×'Ò'¨¬Ñ5Ô5Ð5ØŒL×ÒÑ Ô Ð Ø�1�q‘5ŒMÔ"×)Ò)¨%¬.Ñ9Ô9Ð9ØŒN× Ò Ñ"Ô"Ð"Ð"ð	#ð 	#r    c                 óŽ  •— | r‰n‰} ‰|¦  «        }| s|‰= t          |                     ¦   «         ¦  «        }| r$‰D ] }||vr‰|         d         ‰
k     r‰
dz   ||<   Œ!n|D ]}||xx         ‰
z  cc<   Œ|‰
z  }||= |                     ¦   «         D ]´\  }}‰|         d         }||k    r›|‰	|         j        v rA‰	|         j                             |¦  «         ‰	|         j                             |¦  «         n@‰	|         j                             |¦  «         ‰	|         j                             |¦  «         |‰|         d<   Œµ|S )z&Apply the global relabeling heuristic.r   r   )ÚmaxÚvaluesr   r3   r2   r4   r1   )Ú	from_sinkr   r   rK   r   Ú
new_heightÚ
old_heightÚRr)   r6   rF   r   r%   r7   s          €€€€€€€r   Úglobal_relabelz)preflow_push_impl.<locals>.global_relabel¼   s—  ø€ àÐ#ˆaˆa !ˆØ�+˜cÑ"Ô"ˆØð 	à˜�
Ý˜ŸšÑ)Ô)Ñ*Ô*ˆ
Øð 
	ð ð 'ð '�Ø˜GÐ#Ð#¨°¬
°8Ô(<¸qÒ(@Ð(@Ø!" Q¡�G˜A‘Jøð'ð
 ð  ð  �Ø˜�
�
”
˜a‘�
�
‘
�
Ø˜!‰OˆJØ�CˆLØ$Ÿ]š]™_œ_ð 		2ð 		2‰MˆAˆzØ  œ HÔ-ˆJØ˜ZÒ'Ð'Ø˜˜zÔ*Ô1Ð1Ð1Ø˜:Ô&Ô-×4Ò4°QÑ7Ô7Ð7Ø˜:Ô&Ô-×1Ò1°!Ñ4Ô4Ð4Ð4à˜:Ô&Ô/×6Ò6°qÑ9Ô9Ð9Ø˜:Ô&Ô/×3Ò3°AÑ6Ô6Ð6Ø'1�˜”
˜8Ñ$øØÐr    TF)ÚnxÚNetworkXErrorÚstrr   r   ÚnodesÚpredÚsuccrO   Úgraphr<   rN   r	   Úsizer   r   Úranger3   r4   r1   r   Ú
is_reachedÚ
clear_work)ÚGr%   r7   r   ÚresidualÚglobal_relabel_freqÚ
value_onlyr   Úer   r   r5   rG   rL   rT   r   rR   Ú	old_levelrS   r)   r   r*   r8   r>   r   r6   rK   rF   r+   r?   r   s    ``               @@@@@@@@@@@@@r   Úpreflow_push_implrf      sí  øøøøøøøøøøøøøøø€ à�€z€zÝÔÐ<¥s¨1¡v¤vÐ<Ð<Ð<Ñ=Ô=Ð=Ø�€z€zÝÔÐ<¥s¨1¡v¤vÐ<Ð<Ð<Ñ=Ô=Ð=ØˆA‚v€vÝÔÐBÑCÔCÐCàÐ"ØÐØ˜QÒÐÝÔÐIÑJÔJÐJàÐÝ" 1 hÑ/Ô/ˆˆàˆå˜˜A˜qÑ!Ô!Ð!àŒg€GØŒV€FØŒV€Fð ð ð ˆØ ˆ�Œ
�8ÑØ˜”×!Ò!Ñ#Ô#ð 	ð 	ˆAØˆAˆf‰IˆIð	ðð ð ð ð ð  ˆk˜!‰nŒn€Gà�ÐÐð !"ˆŒ�ÑØˆåˆA‰Œ€Aõ Ð;Ð;Ð;Ð;Ð;¨Ð;Ñ;Ô;Ñ;Ô;€JØ€GˆA�Jå
   A§F¢F¡H¤HÐ.AÑ
BÔ
B€Cð ð 9ð 9ˆØ-.°'¨\¨\˜w qœz˜z¸qÀ1¹uˆ�Œ
�8ÑÝ"-¨f°Q¬iÑ"8Ô"8ˆ�Œ
�;ÑÐð%ð %ð %ð %ð %ð %ð ˜!”9—?’?Ñ$Ô$ð ð ‰ˆˆ4Ø�JÔˆØ�!Š8ˆ8ØˆD��A�tÑÔÐøð -Ð,�u Q¨¡U™|œ|Ð,Ñ,Ô,€FØð &ð &ˆØ�Š6ˆ6�a˜1’f�fØ˜7 1œ: hÔ/Ô0ˆEØ�qŒz˜(Ô# aÒ'Ð'Ø”× Ò  Ñ#Ô#Ð#Ð#à”×"Ò" 1Ñ%Ô%Ð%øð$ð $ð $ð $ð $ð $ð $ð $ð

ð 

ð 

ð 

ð 

ð 

ð 

ð'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ðR#ð #ð #ð #ð #ð #ð #ð #ðð ð ð ð ð ð ð ð ð ð ðF €FØ
�1Š*ˆ*ð	5Ø˜6”NˆEØ”<ð ð ˜!‘�ØàˆJØˆIÝ! %¤,Ñ/Ô/ˆAØ�Y˜q $Ñ'Ô'ˆFØ�~Š~ÑÔð 5ð (˜¨Ñ-Ô-�Ø#�
Ø—’Ñ Ô Ð Ð ØÔ%ð 5¨iÔ.@ð 5ð
 �˜jÑ)Ô)Ð)Ø# a™�Ø#�
�
õ ! ¨VÑ4Ô4�
ð;	5ð �1Š*ˆ*ðF ð Ø '¨¤
¨8Ô 4ˆŒ�ÑØˆð ˆ^˜EÑ"Ô"€FØ‡N‚NÑÔÐð �1Š*ˆ*ð	!Ø˜6”NˆEØ”<ð ð ˜!‘�ØÝ! %¤,Ñ/Ô/ˆAØ�Y˜q %Ñ(Ô(ˆFØ�~Š~ÑÔð !à'˜¨Ñ.Ô.�Ø—’Ñ Ô Ð ð	!ð �1Š*ˆ*ð  $ AœJ xÔ0€A„GˆLÑØ€Hr    r   ÚinfT)Ú
edge_attrsÚreturns_graphFc           	      ól   — t          | ||||||¦  «        }d|j        d<   t          j        |¦  «         |S )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)rf   r[   rU   Ú_clear_cache)r`   r%   r7   r   ra   rb   rc   rS   s           r   r   r   #  s@   € õF 	˜!˜Q  8¨XÐ7JÈJÑWÔW€AØ)€A„GˆKÑÝ„O�AÑÔÐØ€Hr    )r   Nr   F)Ú__doc__Úcollectionsr   Ú	itertoolsr   ÚnetworkxrU   Úutilsr   r   r	   r
   r   r   Ú__all__rf   Ú_dispatchableÚfloatr   r#   r    r   ú<module>ru      s
  ððð ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &ðð ð ð ð ð ð ð ð ð ð ð ð ð ð Ð
€ðJð Jð JðZ €Ô˜j¨%¨%°©,¬,Ð7ÀtÐLÑLÔLàSXðEð Eð Eñ MÔLðEð Eð Er    