ó
 ,ľ[c           @   sˇ   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 d d l m Z m Z m Z e Z d	 j d
 g  Z d g Z d d d  Z d   Z d S(   s0   
Kanevsky all minimum node k cutsets algorithm.
i˙˙˙˙N(   t   defaultdict(   t   combinations(   t
   itemgetteri   (   t!   build_auxiliary_node_connectivity(   t   build_residual_networkt   edmonds_karpt   shortest_augmenting_paths   
s%   Jordi Torrents <jtorrents@milnou.net>t   all_node_cutsc   !   	   #   s˙  t  j |   s! t  j d   n  t  j |   d k ri x, t |  t |   d  D] } t |  VqP Wd Sg  } t |   } | j   | j	 d } t
 j
 | j  } t | d  } t d d d |  }	 | d k rÜ t } n  | t k rő t |	 d <n  | d k rt  j |  d | } n  d	   t |  j   d
 t d  d t |  D }
 t |  |
  ro| j |
  |
 Vn  x|
 D]} t |   |
 t |  |  } xZ| D]R} | | d | | d | | |	  } | j	 d } | | k rĄg  | j d t  D]+ \  } } | d d k rř | f ^ qř} } t g  | D] } | D] } | ^ qAq7 } } g  | j d t  D]B \  } } | d | d k sŁ| d d k rp | | f ^ qp} | j |  t  j |  } t  j |  } | j	 d } t t  } x+ | j   D] \ } } | | j |  q	Wt g  | D] } | | ^ q4 } xŻt  j |  D]} t |  j  |  s~q]n  t    x | D] }  j! | |  qWt   } x"  D] } | j! | j |  qšW j! |  d | |  k s]d | |  k rq]n  t   } x2  D]*  | j!   f d   |  D  q"Wt" g  | D]( \  }    d   | d k ^ qZ rq]n    f d   | D }  t |   | k r]| |  k s]| |  k r×q]n  |  | k rű|  V| j |   qűq]q]W| j# d | | d | | d d | j# d | | d | | d d | j# d | | d | | d d | j# d | | d | | d d | j# d | | d | | d d | j# d | | d | | d d | j$ |  qĄqĄWqvWd S(   s  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
        edmonds_karp. This function performs better in sparse graphs with
        right tailed degree distributions. 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

    s   Input graph is disconnected.i   Nt   mappingt   capacityt   residualt	   two_phaset	   flow_funcc         S   s   h  |  ] \ } } |  q S(    (    (   t   .0t   nt   d(    (    sH   lib/python2.7/site-packages/networkx/algorithms/connectivity/kcutsets.pys	   <setcomp>{   s   	 t   keyt   reverses   %sBs   %sAt
   flow_valuet   datat   flowi    c         3   s'   |  ] } |   k r  | f Vq d  S(   N(    (   R   t   w(   t   St   u(    sH   lib/python2.7/site-packages/networkx/algorithms/connectivity/kcutsets.pys	   <genexpr>ź   s   t   idc            s$   h  |  ] \ } }   | d   q S(   R   (    (   R   R   t   _(   t   H_nodes(    sH   lib/python2.7/site-packages/networkx/algorithms/connectivity/kcutsets.pys	   <setcomp>Ă   s   	 (%   t   nxt   is_connectedt   NetworkXErrort   densityR   t   lent   setR   t   nodest   grapht   copyt   _predR   t   dictt   Nonet   default_flow_funcR   t   Truet   node_connectivityt   sortedt   degreeR   t   _is_separating_sett   appendt   edgest   remove_edges_fromt   transitive_closuret   condensationR    t   listt   itemst
   antichainst   issubsett   updatet   anyt   add_edget   add_edges_from(!   t   Gt   kR   t   cut_sett   seent   HR   t   original_H_predt   Rt   kwargst   Xt   xt   non_adjacentt   vR   R   R   t   E1t   flowed_edgest   edgeR   t   VE1t   incident_nodest   saturated_edgest	   R_closuret   Lt   cmapt   inv_cmapt   scct	   antichaint   S_ancestorst   cutsett   node_cut(    (   R   R   R   sH   lib/python2.7/site-packages/networkx/algorithms/connectivity/kcutsets.pyR      s¨    A 		2%&0%#		(	5	c         C   sL   t  |  t  |   d k r  t St j |  | g   } t j |  rH t St S(   s)   Assumes that the input graph is connectedi   (   R   R(   R   t   restricted_viewR   t   False(   R:   t   cutR>   (    (    sH   lib/python2.7/site-packages/networkx/algorithms/connectivity/kcutsets.pyR,   ç   s    (   t   __doc__R#   t   collectionsR    t	   itertoolsR   t   operatorR   t   networkxR   t   utilsR   t   networkx.algorithms.flowR   R   R   R'   t   joint
   __author__t   __all__R&   R   R,   (    (    (    sH   lib/python2.7/site-packages/networkx/algorithms/connectivity/kcutsets.pyt   <module>   s   	Î