
    h$                         S r SSKrSSKJr  SSKJr  SSKJr  SSKr	SSK
JrJrJr  SSKJr  \rS	/r\	R$                  SS
 j5       rS rg)z0
Kanevsky all minimum node k cutsets algorithm.
    N)defaultdict)combinations)
itemgetter)build_residual_networkedmonds_karpshortest_augmenting_path   )!build_auxiliary_node_connectivityall_node_cutsc           
   #   :
  ^^"^##    [         R                  " U 5      (       d  [         R                  " S5      e[         R                  " U 5      S:X  a  S Sh  vN   g/ n[	        U 5      nUR
                  m"UR                  S   n[        R                  " UR                  5      n[        US5      nSUS.nUc  [        nU[        L a  SUS	'   Uc  [         R                  " XS
9n[        U R                  5       [        S5      SS9SU  V	V
s1 s H  u  pU	iM	     nn	n
[!        X5      (       a  UR#                  U5        Uv   U GH  n[%        U 5      U1-
  [%        X   5      -
  nU GH  nU" XEU    S3X^    S340 UD6nUR                  S   nX:X  d  M/  UR'                  SS9 VVV
s/ s H  u  nnoS   S:w  d  M  UU4PM     sn
nn=nnU VV	s1 s H  nU  H  oiM     M     sn	n=nnUR'                  SS9 VVV
s/ s H#  u  nnn
U
S   U
S   :X  d  U
S   S:X  d  M  UUU
4PM%     nnnn
UR)                  U5        [         R*                  " U5      n[         R,                  " U5      nUR                  S   n[/        [0        5      nUR3                  5        H  u  n	nUU   R#                  U	5        M     U V	s1 s H  n	UU	   iM
     nn	[         R4                  " U5       GHP  n[%        U5      R7                  U5      (       d  M%  [%        5       m#U H  nT#R9                  UU   5        M     [%        5       nT# H!  n	UR9                  UR                  U	   5        M#     T#R9                  U5        X\    S3T#;  d  X^    S3T#;   a  M  [%        5       nT# H"  mUR9                  U#U4S jUT    5       5        M$     [;        U"4S jU 5       5      (       a  M  U VV s1 s H  u  nn T"U   S   iM     n!nn [=        U!5      U:X  d  GM#  UU!;   d  UU!;   a  GM2  U!U;  d  GM;  U!v   UR#                  U!5        GMS     UR?                  X\    S3X^    S3SS9  UR?                  X^    S3X\    S3SS9  UR?                  X\    S3X^    S3SS9  UR?                  X^    S3X\    S3SS9  UR?                  X^    S3X\    S3SS9  UR?                  X\    S3X^    S3SS9  URA                  U5        GM     GM     g GNs  sn
n	f s  sn
nnf s  sn	nf s  sn
nnf s  sn	f s  sn nf 7f)a  Returns all minimum k cutsets of an undirected graph G.

This implementation is based on Kanevsky's algorithm [1]_ for finding all
minimum-size node cut-sets of an undirected graph G; ie the set (or sets)
of nodes of cardinality equal to the node connectivity of G. Thus if
removed, would break G into two or more connected components.

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

k : Integer
    Node connectivity of the input graph. If k is None, then it is
    computed. Default value: None.

flow_func : function
    Function to perform the underlying flow computations. Default value is
    :func:`~networkx.algorithms.flow.edmonds_karp`. This function performs
    better in sparse graphs with right tailed degree distributions.
    :func:`~networkx.algorithms.flow.shortest_augmenting_path` will
    perform better in denser graphs.


Returns
-------
cuts : a generator of node cutsets
    Each node cutset has cardinality equal to the node connectivity of
    the input graph.

Examples
--------
>>> # A two-dimensional grid graph has 4 cutsets of cardinality 2
>>> G = nx.grid_2d_graph(5, 5)
>>> cutsets = list(nx.all_node_cuts(G))
>>> len(cutsets)
4
>>> all(2 == len(cutset) for cutset in cutsets)
True
>>> nx.node_connectivity(G)
2

Notes
-----
This implementation is based on the sequential algorithm for finding all
minimum-size separating vertex sets in a graph [1]_. The main idea is to
compute minimum cuts using local maximum flow computations among a set
of nodes of highest degree and all other non-adjacent nodes in the Graph.
Once we find a minimum cut, we add an edge between the high degree
node and the target node of the local maximum flow computation to make
sure that we will not find that minimum cut again.

See also
--------
node_connectivity
edmonds_karp
shortest_augmenting_path

References
----------
.. [1]  Kanevsky, A. (1993). Finding all minimum-size separating vertex
        sets in a graph. Networks 23(6), 533--541.
        http://onlinelibrary.wiley.com/doi/10.1002/net.3230230604/abstract

zInput graph is disconnected.r	    Nmappingcapacity)r   residualT	two_phase)	flow_func)keyreverseBA
flow_value)dataflowr   c              3   :   >#    U  H  oT;  d  M
  TU4v   M     g 7f)Nr   ).0wSus     [/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/connectivity/kcutsets.py	<genexpr> all_node_cuts.<locals>.<genexpr>   s     %W6HUVJfq!f6Hs   	c              3   J   >#    U  H  u  pTU   S    TU   S    :g  v   M     g7f)idNr   )r   r   r   H_nodess      r   r    r!      s*     SFDA71:d+wqz$/??Fs    #r#   )r   )!nxis_connectedNetworkXErrordensityr
   nodesgraphcopy_predr   default_flow_funcr   node_connectivitysorteddegreer   _is_separating_setappendsetedgesremove_edges_fromtransitive_closurecondensationr   listitems
antichainsissubsetupdateanylenadd_edgeadd_edges_from)$Gkr   seenHr   original_H_predRkwargsndXxnon_adjacentvr   r   r   E1flowed_edgesedgeVE1incident_nodessaturated_edges	R_closureLcmapinv_cmapscc	antichainS_ancestorscutset_node_cutr$   r   s$                   `                 @@r   r   r      s    F ??1=>>
 
zz!} D 	*!,AggGggi G ii(Oq*-A$!4F%	,,"{ 	y  8 ahhjjmTJ2ANONtqNAO!A 1v|c!$i/A !
|1-'*Q/?J6JA.J -.GGG,>%,>y1aF)q.FQF,>% \ 79'Gbd$Q$b'GGn &'WW$W%7#%7	Aq}&	1Qz]a5G Q1I%7   #
 ##O411!4	 OOA&wwy)&t,"jjlFAsSM((+ + ),,1tAw, "$q!1I y>22377  A(#/  )"%%K#**9??1+=> HH[)!*Q'q0wzl!4D4I  UF%Woa6H%WW  SFSSS =CDVTQ
4 0VHD8}) =AM$#4/"*N KK1I "2Z 

gj\+
|1-=
J

gj\+
|1-=
J

gj\+
|1-=
J

gj\+
|1-=
J

gj\+
|1-=
J

gj\+
|1-=
J   1q 	 C 	6 	P$% (H# ->  Es   ATS3B4TS6A8TT(S<<S<TT
%T>T		T	(BT9TDTT.TTCT6%Tc                     [        U5      [        U 5      S-
  :X  a  g[        R                  " X/ 5      n[        R                  " U5      (       a  gg)z)Assumes that the input graph is connectedr	   TF)r>   r%   restricted_viewr&   )rA   cutrD   s      r   r1   r1      s@    
3x3q6A:
12&A	q    )NN)__doc__r+   collectionsr   	itertoolsr   operatorr   networkxr%   networkx.algorithms.flowr   r   r   utilsr
   r-   __all___dispatchabler   r1   r   ra   r   <module>rk      s\     # "    5   
 F2 F2Rra   