ó
 ,µ[c        
   @   sN  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z y d d l m Z Wn e	 k
 rm n Xd d l
 Z d d l m Z d	 Z d
 d d d d d d d d d g
 Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z d „  Z d d „ Z d d d d „ Z d d „ Z d d „ Z d d d „ Z d d d „ Z d d d „ Z d S(   s@  Functions for finding and manipulating cliques.

Finding the largest clique in a graph is NP-complete problem, so most of
these algorithms have an exponential running time; for more information,
see the Wikipedia article on the clique problem [1]_.

.. [1] clique problem:: https://en.wikipedia.org/wiki/Clique_problem

iÿÿÿÿ(   t   deque(   t   chain(   t   combinations(   t   islice(   t   ifilterN(   t   not_implemented_fors    Dan Schult (dschult@colgate.edu)t   find_cliquest   find_cliques_recursivet   make_max_clique_grapht   make_clique_bipartitet   graph_clique_numbert   graph_number_of_cliquest   node_clique_numbert   number_of_cliquest   cliques_containing_nodet   enumerate_all_cliquest   directedc      	   #   s÷   i  ‰  i  ‰ x< |  D]4 } t  ˆ  ƒ ˆ  | <‡  f d †  |  | Dƒ ˆ | <q Wt ‡  ‡ f d †  |  Dƒ ƒ } x† | rò t t | j ƒ  ƒ \ } } | VxY t | ƒ D]K \ } } | j t | | g ƒ t ˆ | j	 t
 | | d d ƒ ƒ f ƒ q  Wqm Wd S(   sE  Returns all cliques in an undirected graph.

    This function returns an iterator over cliques, each of which is a
    list of nodes. The iteration is ordered by cardinality of the
    cliques: first all cliques of size one, then all cliques of size
    two, etc.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.

    Returns
    -------
    iterator
        An iterator over cliques, each of which is a list of nodes in
        `G`. The cliques are ordered according to size.

    Notes
    -----
    To obtain a list of all cliques, use
    `list(enumerate_all_cliques(G))`. However, be aware that in the
    worst-case, the length of this list can be exponential in the number
    of nodes in the graph (for example, when the graph is the complete
    graph). This function avoids storing all cliques in memory by only
    keeping current candidate node lists in memory during its search.

    The implementation is adapted from the algorithm by Zhang, et
    al. (2005) [1]_ to output all cliques discovered.

    This algorithm ignores self-loops and parallel edges, since cliques
    are not conventionally defined with such edges.

    References
    ----------
    .. [1] Yun Zhang, Abu-Khzam, F.N., Baldwin, N.E., Chesler, E.J.,
           Langston, M.A., Samatova, N.F.,
           "Genome-Scale Computational Approaches to Memory-Intensive
           Applications in Systems Biology".
           *Supercomputing*, 2005. Proceedings of the ACM/IEEE SC 2005
           Conference, pp. 12, 12--18 Nov. 2005.
           <https://doi.org/10.1109/SC.2005.29>.

    c            s"   h  |  ] } | ˆ  k r | ’ q S(    (    (   t   .0t   v(   t   index(    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <setcomp>U   s   	 c         3   s1   |  ]' } | g t  ˆ | d  ˆ  j ƒf Vq d S(   t   keyN(   t   sortedt   __getitem__(   R   t   u(   R   t   nbrs(    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <genexpr>W   s    i   N(   t   lenR    t   mapt   listt   popleftt	   enumeratet   appendR   t   filtert   __contains__R   t   None(   t   GR   t   queuet   baset   cnbrst   i(    (   R   R   s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR   "   s    ."	c   
      #   s„  t  ˆ  ƒ d k r d S‡  f d †  ˆ  Dƒ ‰ d g } t ˆ  ƒ } t ˆ  ƒ ‰ t | d ‡ ‡ f d †  ƒ} ˆ ˆ | } g  } yí xæ t rj| rH| j ƒ  } ˆ j | ƒ | | d <ˆ | } | | @} | s× | Vqgˆ | @}	 |	 rg| j | ˆ | f ƒ | j d ƒ | } |	 ‰ t | d ‡ ‡ f d †  ƒ} ˆ ˆ | } qgq… | j ƒ  | j ƒ  \ } ‰ } q… WWn t k
 rn Xd S(   s]
  Returns all maximal cliques in an undirected graph.

    For each node *v*, a *maximal clique for v* is a largest complete
    subgraph containing *v*. The largest maximal clique is sometimes
    called the *maximum clique*.

    This function returns an iterator over cliques, each of which is a
    list of nodes. It is an iterative implementation, so should not
    suffer from recursion depth issues.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.

    Returns
    -------
    iterator
        An iterator over maximal cliques, each of which is a list of
        nodes in `G`. The order of cliques is arbitrary.

    See Also
    --------
    find_cliques_recursive
        A recursive version of the same algorithm.

    Notes
    -----
    To obtain a list of all maximal cliques, use
    `list(find_cliques(G))`. However, be aware that in the worst-case,
    the length of this list can be exponential in the number of nodes in
    the graph (for example, when the graph is the complete graph). This
    function avoids storing all cliques in memory by only keeping
    current candidate node lists in memory during its search.

    This implementation is based on the algorithm published by Bron and
    Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi
    (2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. It
    essentially unrolls the recursion used in the references to avoid
    issues of recursion stack depth (for a recursive implementation, see
    :func:`find_cliques_recursive`).

    This algorithm ignores self-loops and parallel edges, since cliques
    are not conventionally defined with such edges.

    References
    ----------
    .. [1] Bron, C. and Kerbosch, J.
       "Algorithm 457: finding all cliques of an undirected graph".
       *Communications of the ACM* 16, 9 (Sep. 1973), 575--577.
       <http://portal.acm.org/citation.cfm?doid=362342.362367>

    .. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi,
       "The worst-case time complexity for generating all maximal
       cliques and computational experiments",
       *Theoretical Computer Science*, Volume 363, Issue 1,
       Computing and Combinatorics,
       10th Annual International Conference on
       Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42
       <https://doi.org/10.1016/j.tcs.2006.06.015>

    .. [3] F. Cazals, C. Karande,
       "A note on the problem of reporting maximal cliques",
       *Theoretical Computer Science*,
       Volume 407, Issues 1--3, 6 November 2008, Pages 564--568,
       <https://doi.org/10.1016/j.tcs.2008.05.010>

    i    Nc            s-   i  |  ]# ‰  ‡  f d  †  ˆ ˆ  Dƒ ˆ  “ q S(   c            s"   h  |  ] } | ˆ  k r | ’ q S(    (    (   R   R   (   R   (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <setcomp>¯   s   	 (    (   R   (   R"   (   R   s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys
   <dictcomp>¯   s   	 R   c            s   t  ˆ ˆ  |  @ƒ S(   N(   R   (   R   (   t   adjt   cand(    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyt   <lambda>´   s    iÿÿÿÿc            s   t  ˆ ˆ  |  @ƒ S(   N(   R   (   R   (   R'   R(   (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR)   É   s    (	   R   R!   t   sett   maxt   Truet   popt   removeR   t
   IndexError(
   R"   t   Qt   subgR   t   ext_ut   stackt   qt   adj_qt   subg_qt   cand_q(    (   R"   R'   R(   s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR   f   s>    F		


	

c            sf   t  ˆ  ƒ d k r t g  ƒ S‡  f d †  ˆ  Dƒ ‰ g  ‰ ‡ ‡ ‡ f d †  ‰ ˆ t ˆ  ƒ t ˆ  ƒ ƒ S(   s¸	  Returns all maximal cliques in a graph.

    For each node *v*, a *maximal clique for v* is a largest complete
    subgraph containing *v*. The largest maximal clique is sometimes
    called the *maximum clique*.

    This function returns an iterator over cliques, each of which is a
    list of nodes. It is a recursive implementation, so may suffer from
    recursion depth issues.

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

    Returns
    -------
    iterator
        An iterator over maximal cliques, each of which is a list of
        nodes in `G`. The order of cliques is arbitrary.

    See Also
    --------
    find_cliques
        An iterative version of the same algorithm.

    Notes
    -----
    To obtain a list of all maximal cliques, use
    `list(find_cliques_recursive(G))`. However, be aware that in the
    worst-case, the length of this list can be exponential in the number
    of nodes in the graph (for example, when the graph is the complete
    graph). This function avoids storing all cliques in memory by only
    keeping current candidate node lists in memory during its search.

    This implementation is based on the algorithm published by Bron and
    Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi
    (2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. For a
    non-recursive implementation, see :func:`find_cliques`.

    This algorithm ignores self-loops and parallel edges, since cliques
    are not conventionally defined with such edges.

    References
    ----------
    .. [1] Bron, C. and Kerbosch, J.
       "Algorithm 457: finding all cliques of an undirected graph".
       *Communications of the ACM* 16, 9 (Sep. 1973), 575--577.
       <http://portal.acm.org/citation.cfm?doid=362342.362367>

    .. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi,
       "The worst-case time complexity for generating all maximal
       cliques and computational experiments",
       *Theoretical Computer Science*, Volume 363, Issue 1,
       Computing and Combinatorics,
       10th Annual International Conference on
       Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42
       <https://doi.org/10.1016/j.tcs.2006.06.015>

    .. [3] F. Cazals, C. Karande,
       "A note on the problem of reporting maximal cliques",
       *Theoretical Computer Science*,
       Volume 407, Issues 1--3, 6 November 2008, Pages 564--568,
       <https://doi.org/10.1016/j.tcs.2008.05.010>

    i    c            s-   i  |  ]# ‰  ‡  f d  †  ˆ ˆ  Dƒ ˆ  “ q S(   c            s"   h  |  ] } | ˆ  k r | ’ q S(    (    (   R   R   (   R   (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <setcomp>  s   	 (    (   R   (   R"   (   R   s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys
   <dictcomp>  s   	 c         3   s´   t  |  d ‡ ‡  f d †  ƒ} x ˆ  ˆ | D] } ˆ  j | ƒ ˆ j | ƒ ˆ | } |  | @} | sp ˆ Vn2 ˆ  | @} | r¢ x ˆ | | ƒ D] } | Vq Wn  ˆ j ƒ  q- Wd  S(   NR   c            s   t  ˆ ˆ  |  @ƒ S(   N(   R   (   R   (   R'   R(   (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR)     s    (   R+   R.   R   R-   (   R1   R(   R   R4   R5   R6   R7   t   clique(   R0   R'   t   expand(   R(   s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR9     s    

	
(   R   t   iterR*   (   R"   (    (   R"   R0   R'   R9   s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR   Ó   s    B
c         C   s   | d k r |  j ƒ  } n t j d | ƒ } t t d „  t |  ƒ Dƒ ƒ ƒ } | j d „  | Dƒ ƒ t | d ƒ } | j	 d „  | Dƒ ƒ | S(   sÈ  Returns the maximal clique graph of the given graph.

    The nodes of the maximal clique graph of `G` are the cliques of
    `G` and an edge joins two cliques if the cliques are not disjoint.

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

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    Returns
    -------
    NetworkX graph
        A graph whose nodes are the cliques of `G` and whose edges
        join two cliques if they are not disjoint.

    Notes
    -----
    This function behaves like the following code::

        import networkx as nx
        G = nx.make_clique_bipartite(G)
        cliques = [v for v in G.nodes() if G.nodes[v]['bipartite'] == 0]
        G = nx.bipartite.project(G, cliques)
        G = nx.relabel_nodes(G, {-v: v - 1 for v in G})

    It should be faster, though, since it skips all the intermediate
    steps.

    i    c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R*   (   R   t   c(    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <genexpr>S  s    c         s   s   |  ] \ } } | Vq d  S(   N(    (   R   R&   R;   (    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <genexpr>U  s    i   c         s   s7   |  ]- \ \ } } \ } } | | @r | | f Vq d  S(   N(    (   R   R&   t   c1t   jt   c2(    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <genexpr>X  s    N(
   R!   t	   __class__t   nxt   empty_graphR   R   R   t   add_nodes_fromR   t   add_edges_from(   R"   t   create_usingt   Bt   cliquest   clique_pairs(    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR   .  s    !"c            s‘   t  j d | ƒ } | j ƒ  | j |  d d ƒx[ t t |  ƒ ƒ D]G \ } } | d ‰  | j ˆ  d d ƒ| j ‡  f d †  | Dƒ ƒ qB W| S(   sÉ  Returns the bipartite clique graph corresponding to `G`.

    In the returned bipartite graph, the "bottom" nodes are the nodes of
    `G` and the "top" nodes represent the maximal cliques of `G`.
    There is an edge from node *v* to clique *C* in the returned graph
    if and only if *v* is an element of *C*.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.

    fpos : bool
        If True or not None, the returned graph will have an
        additional attribute, `pos`, a dictionary mapping node to
        position in the Euclidean plane.

    create_using : NetworkX graph constructor, optional (default=nx.Graph)
       Graph type to create. If graph instance, then cleared before populated.

    Returns
    -------
    NetworkX graph
        A bipartite graph whose "bottom" set is the nodes of the graph
        `G`, whose "top" set is the cliques of `G`, and whose edges
        join nodes of `G` to the cliques that contain them.

        The nodes of the graph `G` have the node attribute
        'bipartite' set to 1 and the nodes representing cliques
        have the node attribute 'bipartite' set to 0, as is the
        convention for bipartite graphs in NetworkX.

    i    t	   bipartitei   c         3   s   |  ] } | ˆ  f Vq d  S(   N(    (   R   R   (   t   name(    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <genexpr>ˆ  s    (   R@   RA   t   clearRB   R   R   t   add_nodeRC   (   R"   t   fposRD   RI   RE   R&   t   cl(    (   RI   s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR	   \  s    "
!c         C   s`   | d k r t |  ƒ } n  t |  j ƒ d k  r4 d St g  | D] } t | ƒ ^ q> p\ d g ƒ S(   s¢  Returns the clique number of the graph.

    The *clique number* of a graph is the size of the largest clique in
    the graph.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.

    cliques : list
        A list of cliques, each of which is itself a list of nodes. If
        not specified, the list of all cliques will be computed, as by
        :func:`find_cliques`.

    Returns
    -------
    int
        The size of the largest clique in `G`.

    Notes
    -----
    You should provide `cliques` if you have already computed the list
    of maximal cliques, in order to avoid an exponential time search for
    maximal cliques.

    i   i    N(   R!   R   R   t   nodesR+   (   R"   RF   R;   (    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR
   Œ  s
    c         C   s+   | d k r! t t |  ƒ ƒ } n  t | ƒ S(   sU  Returns the number of maximal cliques in the graph.

    Parameters
    ----------
    G : NetworkX graph
        An undirected graph.

    cliques : list
        A list of cliques, each of which is itself a list of nodes. If
        not specified, the list of all cliques will be computed, as by
        :func:`find_cliques`.

    Returns
    -------
    int
        The number of maximal cliques in `G`.

    Notes
    -----
    You should provide `cliques` if you have already computed the list
    of maximal cliques, in order to avoid an exponential time search for
    maximal cliques.

    N(   R!   R   R   R   (   R"   RF   (    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR   ¯  s    c         C   st  | d k rº | d k	 r¥ t | t ƒ rs i  } xq | D]8 } t j |  | ƒ } t d „  t | ƒ Dƒ ƒ | | <q4 Wn. t j |  | ƒ } t d „  t | ƒ Dƒ ƒ } | St t |  ƒ ƒ } n  | d k rÛ t |  j ƒ  ƒ } n  t | t ƒ s$| } t g  | D] } | | k rú t | ƒ ^ qú ƒ } nL i  } xC | D]; } t g  | D] } | | k rAt | ƒ ^ qAƒ | | <q1W| S(   sË    Returns the size of the largest maximal clique containing
    each given node.

    Returns a single or list depending on input nodes.
    Optional list of cliques can be input if already computed.
    c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R   (   R   R;   (    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <genexpr>Û  s    c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R   (   R   R;   (    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pys	   <genexpr>Þ  s    N(	   R!   t
   isinstanceR   R@   t	   ego_graphR+   R   RN   R   (   R"   RN   RF   t   dt   nt   HR   R;   (    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR   Í  s(    '49c         C   sÏ   | d k r! t t |  ƒ ƒ } n  | d k rB t |  j ƒ  ƒ } n  t | t ƒ s… | } t g  | D] } | | k ra d ^ qa ƒ } nF i  } x= | D]5 } t g  | D] } | | k r¢ d ^ q¢ ƒ | | <q’ W| S(   s°   Returns the number of maximal cliques for each node.

    Returns a single or list depending on input nodes.
    Optional list of cliques can be input if already computed.
    i   N(   R!   R   R   RN   RO   R   (   R"   RN   RF   R   R;   t   numcliq(    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR      s    .3c         C   sÃ   | d k r! t t |  ƒ ƒ } n  | d k rB t |  j ƒ  ƒ } n  t | t ƒ s | } g  | D] } | | k r^ | ^ q^ } n@ i  } x7 | D]/ } g  | D] } | | k r™ | ^ q™ | | <qŒ W| S(   s¾   Returns a list of cliques containing the given node.

    Returns a single list or list of lists depending on input nodes.
    Optional list of cliques can be input if already computed.
    N(   R!   R   R   RN   RO   (   R"   RN   RF   R   R;   t   vcliques(    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyR     s    (-(   t   __doc__t   collectionsR    t	   itertoolsR   R   R   R   R   t   ImportErrort   networkxR@   t   networkx.utilsR   t
   __author__t   __all__R   R   R   R!   R   R	   R
   R   R   R   R   (    (    (    s9   lib/python2.7/site-packages/networkx/algorithms/clique.pyt   <module>   s4   		Dm	[.0#3