
    wgx                     v    d Z ddlZddlmZ dgZd Zd Z ej                  d e	d      id	
      	 dd       Z
y)z3
Edmonds-Karp algorithm for maximum flow problems.
    N)build_residual_networkedmonds_karpc                    | j                   }| j                  | j                  | j                  d   fd}fd}d}||k  rz |       \  }}	}
|		 |S |g}|}|k7  r|	|   }|j	                  |       |k7  r|j                          |}|k7  r|
|   }|j	                  |       |k7  r| ||      z  }||k  rz|S )-Implementation of the Edmonds-Karp algorithm.infc                 H   }t        |       }t        |      }|D ]!  }|   |   }t        ||d   |d   z
        }|}# |dz  kD  rt        j                  d      t        |       }t        |      }|D ]*  }|   |   dxx   |z  cc<   |   |   dxx   |z  cc<   |}, |S )z&Augment flow along a path from s to t.capacityflow   z-Infinite capacity path, flow unbounded above.)iternextminnxNetworkXUnbounded)pathr
   ituvattrR_succr   s         i/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/flow/edmondskarp.pyaugmentz"edmonds_karp_core.<locals>.augment   s     $ZH 	A!9Q<DtT*-V<=DA	 !8c>&&'VWW$ZH 	A1IaL D( 1IaL D( A	     c                     
di} 
g}di}g}	 g }t        |      t        |      k  r\|D ]Q  }	|   j                         D ]9  \  }}|| vs|d   |d   k  s|| |<   ||v r	|| |fc c S |j                  |       ; S |sy|}n[|D ]Q  }|   j                         D ]9  \  }}||vs|d   |d   k  s|||<   || v r	|| |fc c S |j                  |       ; S |sy|})z:Bidirectional breadth-first search for an augmenting path.Nr
   r	   )NNN)lenitemsappend)predq_ssuccq_tqr   r   r   R_predr   sts           r   bidirectional_bfsz,edmonds_karp_core.<locals>.bidirectional_bfs(   s0   4yc4ycA3x3s8# (A#)!9??#4 (4D=T&\D<L-L&'DG Dy'($} 4HHQK(( + (A#)!9??#4 (4D=T&\D<L-L&'DG Dy'($} 4HHQK(( +/ r   r   )nodesr   r    graphr   reverse)Rr$   r%   cutoffR_nodesr   r&   
flow_valuer   r   r    r   r   r#   r   r   s    ``          @@@r   edmonds_karp_corer.      s    ggGVVFVVF
''%.C*@ J
v
)+4<  s1fQAKKN 1f 	1fQAKKN 1f 	gdm#
! v
$ r   c                    || vr"t        j                  dt        |       d      || vr"t        j                  dt        |       d      ||k(  rt        j                  d      |t        | |      }n|}|D ]  }||   j	                         D ]  }d|d<   	 ! |t        d      }t        ||||      |j                  d<   |S )r   znode z not in graphz!source and sink are the same noder   r
   r   r-   )r   NetworkXErrorstrr   valuesfloatr.   r(   )	Gr$   r%   r	   residualr+   r*   r   es	            r   edmonds_karp_implr7   ^   s    zs1vhm<==zs1vhm<==AvBCC"1h/  1 	AAfI	 ~u-aAv>AGGLHr   r	   r   T)
edge_attrsreturns_graphc                 n    t        | |||||      }d|j                  d<   t        j                  |       |S )a  Find a maximum single-commodity flow using the Edmonds-Karp 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 m^2)$ 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.

    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 edmonds_karp

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

    r   	algorithm)r7   r(   r   _clear_cache)r4   r$   r%   r	   r5   
value_onlyr+   r*   s           r   r   r   x   s8    l 	!Q8Xv>A)AGGKOOAHr   )r	   NFN)__doc__networkxr   networkx.algorithms.flow.utilsr   __all__r.   r7   _dispatchabler3   r    r   r   <module>rD      sX     A
Pf4 j%,7tLJNx Mxr   