
    h                          S r SSKJr  SSKrSSKJr  SSKJr  S/r	\R                  " S\" S5      0S	S
9SS j5       rS rg)z.
Dinitz' algorithm for maximum flow problems.
    )dequeN)build_residual_network)pairwisedinitzcapacityinfT)
edge_attrsreturns_graphc                 j    [        XX#XF5      nSUR                  S'   [        R                  " U5        U$ )a@  Find a maximum single-commodity flow using Dinitz' 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 m)$ for $n$ nodes and $m$
edges [1]_.


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.

value_only : bool
    If True compute only the value of the maximum flow. This parameter
    will be ignored by this algorithm because it is not applicable.

cutoff : integer, float
    If specified, the algorithm will terminate when the flow value reaches
    or exceeds the cutoff. In this case, it may be unable to immediately
    determine a minimum cut. Default value: None.

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:`preflow_push`
: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 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.

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

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 = dinitz(G, "x", "y")
>>> flow_value = nx.maximum_flow_value(G, "x", "y")
>>> flow_value
3.0
>>> flow_value == R.graph["flow_value"]
True

References
----------
.. [1] Dinitz' Algorithm: The Original Version and Even's Version.
       2006. Yefim Dinitz. In Theoretical Computer Science. Lecture
       Notes in Computer Science. Volume 3895. pp 218-240.
       https://doi.org/10.1007/11685654_10

r   	algorithm)dinitz_implgraphnx_clear_cache)Gstr   residual
value_onlycutoffRs           U/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/flow/dinitz_alg.pyr   r      s2    v 	A!x8A#AGGKOOAH    c                   ^^^^^ 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  [        X5      nOUnU H!  nXg   R	                  5        H  nSUS'   M
     M#     UR
                  S   mUc  TnUR                  mUR                  mUUU4S jn	UUUU4S jn
SnX:  a@  U	" 5       nTU;  a  O2U
" U5      nUS	-  T:  a  [         R                  " S
5      eX-  nX:  a  M@  XR
                  S'   U$ )Nznode z not in graphz!source and sink are the same noder   flowr   c                    > 0 n TS0n[        TS4/5      nU(       a  T	U ;   a   U $ UR                  5       u  p4TU   R                  5        Hg  u  pVUS   US   -
  S:  d  M  XP;   a"  X   US-   :X  a  X   R                  U5        M;  M=  [        U/5      X'   US-   X'   UR                  XTS-   45        Mi     U(       a  M  U $ )Nr   r   r      )r   popleftitemsappend)
parentsvertex_distqueueudistvattrR_succr   r   s
          r   breath_first_search(dinitz_impl.<locals>.breath_first_search   s    !f1vhG|  mmoGA!!9??,
#d6l2Q6|&>TAX5#J--a0 6 &+A3Z
)-a]3 -	 e r   c                   > SnT	nU/n [        X   5      S:  a  X   S   nUR                  U5        O9UR                  5         [        U5      S:X  a   U$ US   nX   R                  5         UT:X  a  Tn[	        U5       H&  u  p$[        UTU   U   S   TU   U   S   -
  5      nM(     [	        [        U5      5       H  u  p$TU   U   S==   U-  ss'   TU   U   S==   U-  ss'   TU   U   S   TU   U   S   -
  S:X  d  MH  X   R                  5         US   U:w  d  Me  UR                  5         US   U:w  a  M  M     X-  nUS   nUnGMF  )z-Build a path using DFS starting from the sinkr   r   r   )lenr    popr   r   minreversed)
r!   
total_flowr$   pathr&   r   INFR_predr   r   s
         r   depth_first_search'dinitz_impl.<locals>.depth_first_search   st    
s7:"JqMA
t9>& % H
""$Av$TNDAtVAYq\*%=q	!V@T%TUD +$Xd^4DA1IaL(D0(1IaL(D0(ay|J/&)A,v2FF!K
**,"2h!m HHJ #2h!m 5 "
HA3 r      z-Infinite capacity path, flow unbounded above.
flow_value)	r   NetworkXErrorstrr   valuesr   succpredNetworkXUnbounded)r   r   r   r   r   r   r   r$   er)   r5   r8   r!   	this_flowr3   r4   r(   s    ``           @@@r   r   r      sH   zs1vhm<==zs1vhm<==AvBCC"1/ AAfI   ''%.C~VVFVVF(" "H J

%'G&w/	q=3&&'VWW
 
 'GGLHr   )r   NFN)__doc__collectionsr   networkxr   networkx.algorithms.flow.utilsr   networkx.utilsr   __all___dispatchablefloatr   r    r   r   <module>rJ      sS      A #* j%,7tL} M}@_r   