
    hY                     (   S r SSKrSSKrSSKJrJr  \rSSKJ	r	J
r
  / SQr\R                  " SSS.S	S
\" S5      00S	1S9SS j5       r\R                  " SSS.S	SS00S	1S9SS j5       r\R                  SS j5       r\R                  SS j5       rg)z
Flow based cut algorithms
    N)build_residual_networkedmonds_karp   )!build_auxiliary_edge_connectivity!build_auxiliary_node_connectivity)minimum_st_node_cutminimum_node_cutminimum_st_edge_cutminimum_edge_cut   )Gz
auxiliary?	auxiliarycapacityinf)graphspreserve_edge_attrspreserve_graph_attrsc                    ^ ^^ Uc  [         nUc  [        T 5      nOUnSX5S.n[        R                  " XaU40 UD6u  pU	u  n
m[	        5       nU 4S jU
 5        H"  u  mnUR                  UU4S jU 5       5        M$     U$ )a  Returns the edges of the cut-set of a minimum (s, t)-cut.

This function returns the set of edges of minimum cardinality that,
if removed, would destroy all paths among source and target in G.
Edge weights are not considered. See :meth:`minimum_cut` for
computing minimum cuts considering edge weights.

Parameters
----------
G : NetworkX graph

s : node
    Source node for the flow.

t : node
    Sink node for the flow.

auxiliary : NetworkX DiGraph
    Auxiliary digraph to compute flow based node connectivity. It has
    to have a graph attribute called mapping with a dictionary mapping
    node names in G and in the auxiliary digraph. If provided
    it will be reused instead of recreated. Default value: None.

flow_func : function
    A function for computing the maximum flow among a pair of nodes.
    The function has to accept at least three parameters: a Digraph,
    a source node, and a target node. And return a residual network
    that follows NetworkX conventions (see :meth:`maximum_flow` for
    details). If flow_func is None, the default maximum flow function
    (:meth:`edmonds_karp`) is used. See :meth:`node_connectivity` for
    details. The choice of the default function may change from version
    to version and should not be relied on. Default value: None.

residual : NetworkX DiGraph
    Residual network to compute maximum flow. If provided it will be
    reused instead of recreated. Default value: None.

Returns
-------
cutset : set
    Set of edges that, if removed from the graph, will disconnect it.

See also
--------
:meth:`minimum_cut`
:meth:`minimum_node_cut`
:meth:`minimum_edge_cut`
:meth:`stoer_wagner`
:meth:`node_connectivity`
:meth:`edge_connectivity`
:meth:`maximum_flow`
:meth:`edmonds_karp`
:meth:`preflow_push`
:meth:`shortest_augmenting_path`

Examples
--------
This function is not imported in the base NetworkX namespace, so you
have to explicitly import it from the connectivity package:

>>> from networkx.algorithms.connectivity import minimum_st_edge_cut

We use in this example the platonic icosahedral graph, which has edge
connectivity 5.

>>> G = nx.icosahedral_graph()
>>> len(minimum_st_edge_cut(G, 0, 6))
5

If you need to compute local edge cuts on several pairs of
nodes in the same graph, it is recommended that you reuse the
data structures that NetworkX uses in the computation: the
auxiliary digraph for edge connectivity, and the residual
network for the underlying maximum flow computation.

Example of how to compute local edge cuts among all pairs of
nodes of the platonic icosahedral graph reusing the data
structures.

>>> import itertools
>>> # You also have to explicitly import the function for
>>> # building the auxiliary digraph from the connectivity package
>>> from networkx.algorithms.connectivity import build_auxiliary_edge_connectivity
>>> H = build_auxiliary_edge_connectivity(G)
>>> # And the function for building the residual network from the
>>> # flow package
>>> from networkx.algorithms.flow import build_residual_network
>>> # Note that the auxiliary digraph has an edge attribute named capacity
>>> R = build_residual_network(H, "capacity")
>>> result = dict.fromkeys(G, dict())
>>> # Reuse the auxiliary digraph and the residual network by passing them
>>> # as parameters
>>> for u, v in itertools.combinations(G, 2):
...     k = len(minimum_st_edge_cut(G, u, v, auxiliary=H, residual=R))
...     result[u][v] = k
>>> all(result[u][v] == 5 for u, v in itertools.combinations(G, 2))
True

You can also use alternative flow algorithms for computing edge
cuts. For instance, in dense networks the algorithm
:meth:`shortest_augmenting_path` will usually perform better than
the default :meth:`edmonds_karp` which is faster for sparse
networks with highly skewed degree distributions. Alternative flow
functions have to be explicitly imported from the flow package.

>>> from networkx.algorithms.flow import shortest_augmenting_path
>>> len(minimum_st_edge_cut(G, 0, 6, flow_func=shortest_augmenting_path))
5

r   )r   	flow_funcresidualc              3   0   >#    U  H  oTU   4v   M     g 7fN ).0nr   s     W/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/connectivity/cuts.py	<genexpr>&minimum_st_edge_cut.<locals>.<genexpr>   s     1y!!Iys   c              3   :   >#    U  H  oT;   d  M
  TU4v   M     g 7fr   r   )r   vnon_reachableus     r   r   r      s     Ad=.@fq!fds   	)default_flow_funcr   nxminimum_cutsetupdate)r   str   r   r   Hkwargs	cut_value	partition	reachablecutsetnbrsr!   r"   s   `            @@r   r
   r
      s    h %	-a0$9SF>>!<V<I(I} UF1y14AdAA 2 M    id)r   preserve_node_attrsr   c                    Uc  [        U 5      nOUnUR                  R                  SS5      nUc  [        R                  " S5      eU R                  X5      (       d  U R                  X!5      (       a  0 $ X5US.n[        XgU    S3Xr    S340 UD6n	U	 V
Vs1 s H  o  H  oR                  U   S   iM     M     nn
nXU1-
  $ s  snn
f )aU  Returns a set of nodes of minimum cardinality that disconnect source
from target in G.

This function returns the set of nodes of minimum cardinality that,
if removed, would destroy all paths among source and target in G.

Parameters
----------
G : NetworkX graph

s : node
    Source node.

t : node
    Target node.

flow_func : function
    A function for computing the maximum flow among a pair of nodes.
    The function has to accept at least three parameters: a Digraph,
    a source node, and a target node. And return a residual network
    that follows NetworkX conventions (see :meth:`maximum_flow` for
    details). If flow_func is None, the default maximum flow function
    (:meth:`edmonds_karp`) is used. See below for details. The choice
    of the default function may change from version to version and
    should not be relied on. Default value: None.

auxiliary : NetworkX DiGraph
    Auxiliary digraph to compute flow based node connectivity. It has
    to have a graph attribute called mapping with a dictionary mapping
    node names in G and in the auxiliary digraph. If provided
    it will be reused instead of recreated. Default value: None.

residual : NetworkX DiGraph
    Residual network to compute maximum flow. If provided it will be
    reused instead of recreated. Default value: None.

Returns
-------
cutset : set
    Set of nodes that, if removed, would destroy all paths between
    source and target in G.

Examples
--------
This function is not imported in the base NetworkX namespace, so you
have to explicitly import it from the connectivity package:

>>> from networkx.algorithms.connectivity import minimum_st_node_cut

We use in this example the platonic icosahedral graph, which has node
connectivity 5.

>>> G = nx.icosahedral_graph()
>>> len(minimum_st_node_cut(G, 0, 6))
5

If you need to compute local st cuts between several pairs of
nodes in the same graph, it is recommended that you reuse the
data structures that NetworkX uses in the computation: the
auxiliary digraph for node connectivity and node cuts, and the
residual network for the underlying maximum flow computation.

Example of how to compute local st node cuts reusing the data
structures:

>>> # You also have to explicitly import the function for
>>> # building the auxiliary digraph from the connectivity package
>>> from networkx.algorithms.connectivity import build_auxiliary_node_connectivity
>>> H = build_auxiliary_node_connectivity(G)
>>> # And the function for building the residual network from the
>>> # flow package
>>> from networkx.algorithms.flow import build_residual_network
>>> # Note that the auxiliary digraph has an edge attribute named capacity
>>> R = build_residual_network(H, "capacity")
>>> # Reuse the auxiliary digraph and the residual network by passing them
>>> # as parameters
>>> len(minimum_st_node_cut(G, 0, 6, auxiliary=H, residual=R))
5

You can also use alternative flow algorithms for computing minimum st
node cuts. For instance, in dense networks the algorithm
:meth:`shortest_augmenting_path` will usually perform better than
the default :meth:`edmonds_karp` which is faster for sparse
networks with highly skewed degree distributions. Alternative flow
functions have to be explicitly imported from the flow package.

>>> from networkx.algorithms.flow import shortest_augmenting_path
>>> len(minimum_st_node_cut(G, 0, 6, flow_func=shortest_augmenting_path))
5

Notes
-----
This is a flow based implementation of minimum node cut. The algorithm
is based in solving a number of maximum flow computations to determine
the capacity of the minimum cut on an auxiliary directed network that
corresponds to the minimum node cut of G. It handles both directed
and undirected graphs. This implementation is based on algorithm 11
in [1]_.

See also
--------
:meth:`minimum_node_cut`
:meth:`minimum_edge_cut`
:meth:`stoer_wagner`
:meth:`node_connectivity`
:meth:`edge_connectivity`
:meth:`maximum_flow`
:meth:`edmonds_karp`
:meth:`preflow_push`
:meth:`shortest_augmenting_path`

References
----------
.. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms.
    http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf

NmappingzInvalid auxiliary digraph.r   r   r   BAr2   )r   graphgetr$   NetworkXErrorhas_edger
   nodes)r   r(   r)   r   r   r   r*   r5   r+   edge_cutedgenodenode_cuts                r   r   r      s    v -a0ggkk)T*G;<<zz!1::a++	$KF #1A&67:,a8HSFSH08JTTd#T#HJ!f Ks   $Cc                   ^  Ub  Ub  Uc  Ub  [         R                  " S5      eUbN  UbK  UT ;  a  [         R                  " SU S35      eUT ;  a  [         R                  " SU S35      e[        T XUS9$ T R                  5       (       aH  [         R                  " T 5      (       d  [         R                  " S5      e[
        R                  nU 4S jnOM[         R                  " T 5      (       d  [         R                  " S5      e[
        R                  nT R                  n[        T 5      n[        US5      nX6US.n[        T T R                  S	9n	[        T U	   5      n
[        T 5      [        U" U	5      5      -
  U	1-
   H,  n[        T X40 UD6n[        U
5      [        U5      :  d  M*  Un
M.     U" U" U	5      S
5       H9  u  pUT U   ;   a  M  [        T X40 UD6n[        U
5      [        U5      :  d  M7  Un
M;     U
$ )a,  Returns a set of nodes of minimum cardinality that disconnects G.

If source and target nodes are provided, this function returns the
set of nodes of minimum cardinality that, if removed, would destroy
all paths among source and target in G. If not, it returns a set
of nodes of minimum cardinality that disconnects G.

Parameters
----------
G : NetworkX graph

s : node
    Source node. Optional. Default value: None.

t : node
    Target node. Optional. Default value: None.

flow_func : function
    A function for computing the maximum flow among a pair of nodes.
    The function has to accept at least three parameters: a Digraph,
    a source node, and a target node. And return a residual network
    that follows NetworkX conventions (see :meth:`maximum_flow` for
    details). If flow_func is None, the default maximum flow function
    (:meth:`edmonds_karp`) is used. See below for details. The
    choice of the default function may change from version
    to version and should not be relied on. Default value: None.

Returns
-------
cutset : set
    Set of nodes that, if removed, would disconnect G. If source
    and target nodes are provided, the set contains the nodes that
    if removed, would destroy all paths between source and target.

Examples
--------
>>> # Platonic icosahedral graph has node connectivity 5
>>> G = nx.icosahedral_graph()
>>> node_cut = nx.minimum_node_cut(G)
>>> len(node_cut)
5

You can use alternative flow algorithms for the underlying maximum
flow computation. In dense networks the algorithm
:meth:`shortest_augmenting_path` will usually perform better
than the default :meth:`edmonds_karp`, which is faster for
sparse networks with highly skewed degree distributions. Alternative
flow functions have to be explicitly imported from the flow package.

>>> from networkx.algorithms.flow import shortest_augmenting_path
>>> node_cut == nx.minimum_node_cut(G, flow_func=shortest_augmenting_path)
True

If you specify a pair of nodes (source and target) as parameters,
this function returns a local st node cut.

>>> len(nx.minimum_node_cut(G, 3, 7))
5

If you need to perform several local st cuts among different
pairs of nodes on the same graph, it is recommended that you reuse
the data structures used in the maximum flow computations. See
:meth:`minimum_st_node_cut` for details.

Notes
-----
This is a flow based implementation of minimum node cut. The algorithm
is based in solving a number of maximum flow computations to determine
the capacity of the minimum cut on an auxiliary directed network that
corresponds to the minimum node cut of G. It handles both directed
and undirected graphs. This implementation is based on algorithm 11
in [1]_.

See also
--------
:meth:`minimum_st_node_cut`
:meth:`minimum_cut`
:meth:`minimum_edge_cut`
:meth:`stoer_wagner`
:meth:`node_connectivity`
:meth:`edge_connectivity`
:meth:`maximum_flow`
:meth:`edmonds_karp`
:meth:`preflow_push`
:meth:`shortest_augmenting_path`

References
----------
.. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms.
    http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf

)Both source and target must be specified.node  not in graph)r   Input graph is not connectedc                    > [         R                  R                  TR                  U 5      TR	                  U 5      /5      $ r   )	itertoolschainfrom_iterablepredecessors
successors)r    r   s    r   	neighbors#minimum_node_cut.<locals>.neighbors  s/    ??00!..2CQ\\RS_1UVVr1   r   )r   r   r   key   )r$   r;   r   is_directedis_weakly_connectedrH   permutationsis_connectedcombinationsrM   r   r   mindegreer&   len)r   r(   r)   r   	iter_funcrM   r*   Rr+   r    min_cutwthis_cutxys   `              r   r	   r	   1  s   | 	
!)q}JKK 	}A:""U1#]#;<<A:""U1#]#;<<"1ai@@ 	}}%%a((""#ABB**		W q!!""#ABB**	KK	 	*!,Aq*-A$!DF 	A188A!A$iGVc)A,''1#-&q!9&9w<3x=(G .
 )A,*!9&q!9&9w<3x=(G + Nr1   c                    Ub  Ub  Uc  Ub  [         R                  " S5      e[        U 5      n[        US5      nX5US.nUbN  UbK  X;  a  [         R                  " SU S35      eX ;  a  [         R                  " SU S35      e[	        XAU40 UD6$ U R                  5       (       a  [         R                  " U 5      (       d  [         R                  " S5      e[        X R                  S9n[        U R                  U5      5      n[        U 5      n	[        U	5      n
[        U
5       H5  n [	        XIU   XS-      40 UD6n[        U5      [        U5      ::  a  UnM5  M7     U$ [         R                  " U 5      (       d  [         R                  " S5      e[        X R                  S9n[        U R                  U5      5      nU  H0  n[         R                   " XS
9nUR#                  5       nU(       d  M0    O   U$ U H,  n[	        XNU40 UD6n[        U5      [        U5      ::  d  M*  UnM.     U$ ! [         a6    [	        XIU   U	S	   40 UD6n[        U5      [        U5      ::  a  Un GM@   GMD  f = f)a  Returns a set of edges of minimum cardinality that disconnects G.

If source and target nodes are provided, this function returns the
set of edges of minimum cardinality that, if removed, would break
all paths among source and target in G. If not, it returns a set of
edges of minimum cardinality that disconnects G.

Parameters
----------
G : NetworkX graph

s : node
    Source node. Optional. Default value: None.

t : node
    Target node. Optional. Default value: None.

flow_func : function
    A function for computing the maximum flow among a pair of nodes.
    The function has to accept at least three parameters: a Digraph,
    a source node, and a target node. And return a residual network
    that follows NetworkX conventions (see :meth:`maximum_flow` for
    details). If flow_func is None, the default maximum flow function
    (:meth:`edmonds_karp`) is used. See below for details. The
    choice of the default function may change from version
    to version and should not be relied on. Default value: None.

Returns
-------
cutset : set
    Set of edges that, if removed, would disconnect G. If source
    and target nodes are provided, the set contains the edges that
    if removed, would destroy all paths between source and target.

Examples
--------
>>> # Platonic icosahedral graph has edge connectivity 5
>>> G = nx.icosahedral_graph()
>>> len(nx.minimum_edge_cut(G))
5

You can use alternative flow algorithms for the underlying
maximum flow computation. In dense networks the algorithm
:meth:`shortest_augmenting_path` will usually perform better
than the default :meth:`edmonds_karp`, which is faster for
sparse networks with highly skewed degree distributions.
Alternative flow functions have to be explicitly imported
from the flow package.

>>> from networkx.algorithms.flow import shortest_augmenting_path
>>> len(nx.minimum_edge_cut(G, flow_func=shortest_augmenting_path))
5

If you specify a pair of nodes (source and target) as parameters,
this function returns the value of local edge connectivity.

>>> nx.edge_connectivity(G, 3, 7)
5

If you need to perform several local computations among different
pairs of nodes on the same graph, it is recommended that you reuse
the data structures used in the maximum flow computations. See
:meth:`local_edge_connectivity` for details.

Notes
-----
This is a flow based implementation of minimum edge cut. For
undirected graphs the algorithm works by finding a 'small' dominating
set of nodes of G (see algorithm 7 in [1]_) and computing the maximum
flow between an arbitrary node in the dominating set and the rest of
nodes in it. This is an implementation of algorithm 6 in [1]_. For
directed graphs, the algorithm does n calls to the max flow function.
The function raises an error if the directed graph is not weakly
connected and returns an empty set if it is weakly connected.
It is an implementation of algorithm 8 in [1]_.

See also
--------
:meth:`minimum_st_edge_cut`
:meth:`minimum_node_cut`
:meth:`stoer_wagner`
:meth:`node_connectivity`
:meth:`edge_connectivity`
:meth:`maximum_flow`
:meth:`edmonds_karp`
:meth:`preflow_push`
:meth:`shortest_augmenting_path`

References
----------
.. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms.
    http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf

rC   r   r6   rD   rE   rF   rO   r   r   )
start_with)r$   r;   r   r   r
   rR   rS   rW   rX   r&   edgeslistrY   range
IndexErrorrU   dominating_setpop)r   r(   r)   r   r*   r[   r+   r@   r\   r=   r   ir^   Dr    r]   s                   r   r   r     sO   @ 	
!)q}JKK 	*!,Aq*-A$!DF 	}:""U1#]#;<<:""U1#]#;<<"15f55 	}}%%a((""#ABB 1((#aggdm$QJqA'.q(Ea%LSFSx=CL0&G 1   q!!""#ABB 1((#aggdm$ D!!!5AAq	  NA*1=f=H8}G," 
 A  '.q(E!HOOx=CL0&G 1's   (/H))7I)(I))NNN)__doc__rH   networkxr$   networkx.algorithms.flowr   r   r#   utilsr   r   __all___dispatchablefloatr
   r   r	   r   r   r1   r   <module>rr      s      J   W !$$z5<&@A%
A
AH !$$tTl3%
G
GT N Nb ` `r1   