
    wg$                         d Z ddlZddlmZ ddlmZ ddlmZ ddlZ	ddl
mZmZmZ ddlmZ eZd	gZe	j$                  dd
       Zd Zy)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           
   #   	  "#K   t        j                  |       st        j                  d      t        j                  |       dk(  rdE d{    yg }t	        |       }|j
                  "|j                  d   }t        j                  |j                        }t        |d      }d|d}|t        }|t        u rd|d	<   |t        j                  | |
      }t        | j                         t        d      d      d| D 	
ch c]  \  }	}
|		 }}	}
t!        | |      r|j#                  |       | |D ]  }t%        |       |hz
  t%        | |         z
  }|D ]h  } ||||    d||    dfi |}|j                  d   }||k(  s0|j'                  d      D 
cg c]  \  }}}
|
d   dk7  s||f c}
}}x}}|D 	ch c]  }|D ]  }	|	  c}	}x}}|j'                  d      D 
cg c]  \  }}}
|
d   |
d   k(  s|
d   dk(  r|||
f  }}}}
|j)                  |       t        j*                  |      }t        j,                  |      }|j                  d   }t/        t0              }|j3                         D ]  \  }	}||   j#                  |	        |D 	ch c]  }	||	   	 }}	t        j4                  |      D ]+  }t%        |      j7                  |      st%               #|D ]  }#j9                  ||           t%               }#D ]   }	|j9                  |j                  |	          " #j9                  |       ||    d#vs
||    d#v rt%               }#D ]   |j9                  #fd|   D               " t;        "fd|D              r|D  ch c]  \  }} "|   d    }!}} t=        |!      |k(  s||!v s||!v r|!|vs|! |j#                  |!       . |j?                  ||    d||    dd       |j?                  ||    d||    dd       |j?                  ||    d||    dd       |j?                  ||    d||    dd       |j?                  ||    d||    dd       |j?                  ||    d||    dd       |jA                  |       k  y7 }c c}
}	w c c}
}}w c c}	}w c c}
}}w c c}	w c c} }w w)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   0   K   | ]  }|vs|f  y w)Nr   ).0wSus     n/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/connectivity/kcutsets.py	<genexpr>z all_node_cuts.<locals>.<genexpr>   s     %WAUVJq!f%Ws   		c              3   F   K   | ]  \  }}|   d    |   d    k7    yw)idNr   )r   r   r   H_nodess      r   r    z all_node_cuts.<locals>.<genexpr>   s,     SDAq71:d+wqz$/??Ss   !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      sb    F ??1=>>
 
zz!} D 	*!,AggGggi G ii(Oq*-A$!4F%	,,"{ 	y  i8 ahhjjmTJ2ANOtq!OAO!QA \2 1v|c!A$i/ X	2A !
|1-'!*Q/?J6JA.JQ -.GGG,>% %(1a!F)q.QF% \ 79'Gd$'GQ'G'GGn &'WW$W%7# #!Aq}&	1Qz]a5G 1I# #
 ##O411!4	 OOA&wwy)&t,"jjl ,FAsSM((+, ),,1tAw,, "$q!1 $2I y>2237  A( 0#/0"%%K ?#**9??1+=>?HH[)!!*Q'q0wqzl!4D4I  UF X%Woa6H%WWX SFSS =CDTQ
4 0DHD8}) =AM$#4/"*N KK1I$2Z 

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

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

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

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

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

gaj\+
|1-=
J   1qX	2	\2C 	6 	P$% (H# ->  Es}   AS2S
B6S2SA9S2
S2 S2S8S2S
S20#S BS2 S',C9S2%S,7S2
S2C1S2%S2c                     t        |      t        |       dz
  k(  ryt        j                  | |g       }t        j                  |      ryy)z)Assumes that the input graph is connectedr	   TF)r=   r$   restricted_viewr%   )r@   cutrC   s      r   r0   r0      s?    
3x3q6A:
1c2&A	q    )NN)__doc__r*   collectionsr   	itertoolsr   operatorr   networkxr$   networkx.algorithms.flowr   r   r   utilsr
   r,   __all___dispatchabler   r0   r   r`   r   <module>rj      s\     # "    5   
 F2 F2Rr`   