ó
|£*^c           @   sŽ   d  Z  d j d d d g ƒ Z d d d d d	 d
 g Z d d l Z d „  Z e Z d d d „ Z	 d d d „ Z
 d d d „ Z d d „ Z d S(   s  
Find the k-cores of a graph.

The k-core is found by recursively pruning nodes with degrees less than k.

See the following reference for details:

An O(m) Algorithm for Cores Decomposition of Networks
Vladimir Batagelj and Matjaz Zaversnik, 2003.
http://arxiv.org/abs/cs.DS/0310049

s   
s    Dan Schult (dschult@colgate.edu)s)   Jason Grout (jason-sage@creativetrax.com)s   Aric Hagberg (hagberg@lanl.gov)t   core_numbert   k_coret   k_shellt   k_crustt   k_coronat
   find_coresiÿÿÿÿNc            s  ˆ  j  ƒ  r t j d ƒ ‚ n  ˆ  j ƒ  d k rE t j d d ƒ ‚ n  ˆ  j ƒ  rr d d l ‰ ‡  ‡ f d †  ‰ n	 ˆ  j ‰ ˆ  j ƒ  } t | d | j	 ƒ} d g } d } xS t
 | ƒ D]E \ } } | | | k r¸ | j | g | | | ƒ | | } q¸ q¸ Wt d	 „  t
 | ƒ Dƒ ƒ } | } t ‡ f d
 †  ˆ  Dƒ ƒ }	 x¼ | D]´ } x« |	 | D]Ÿ }
 | |
 | | k rW|	 |
 j | ƒ | |
 } | | |
 } | | |
 <| | | | <| | | | | | <| | <| | |
 c d 7<| |
 c d 8<qWqWWqFW| S(   s�  Return the core number for each vertex.

    A k-core is a maximal subgraph that contains nodes of degree k or more.

    The core number of a node is the largest value k of a k-core containing
    that node.

    Parameters
    ----------
    G : NetworkX graph
       A graph or directed graph

    Returns
    -------
    core_number : dictionary
       A dictionary keyed by node to the core number.

    Raises
    ------
    NetworkXError
        The k-core is not defined for graphs with self loops or parallel edges.

    Notes
    -----
    Not implemented for graphs with parallel edges or self loops.

    For directed graphs the node degree is defined to be the
    in-degree + out-degree.

    References
    ----------
    .. [1] An O(m) Algorithm for Cores Decomposition of Networks
       Vladimir Batagelj and Matjaz Zaversnik, 2003.
       http://arxiv.org/abs/cs.DS/0310049
    s0   MultiGraph and MultiDiGraph types not supported.i    s;   Input graph has self loops; the core number is not defined.s7   Consider using G.remove_edges_from(G.selfloop_edges()).iÿÿÿÿNc            s(   ˆ j  j ˆ  j |  ƒ ˆ  j |  ƒ g ƒ S(   N(   t   chaint   from_iterablet   predecessors_itert   successors_iter(   t   v(   t   Gt	   itertools(    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pyt	   neighborsJ   s    t   keyc         s   s!   |  ] \ } } | | f Vq d  S(   N(    (   t   .0t   posR
   (    (    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pys	   <genexpr>X   s    c         3   s'   |  ] } | t  ˆ  | ƒ ƒ f Vq d  S(   N(   t   set(   R   R
   (   R   (    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pys	   <genexpr>[   s    i   (   t   is_multigrapht   nxt   NetworkXErrort   number_of_selfloopst   is_directedR   t   neighbors_itert   degreet   sortedt   gett	   enumeratet   extendt   dictt   remove(   R   t   degreest   nodest   bin_boundariest   curr_degreet   iR
   t   node_post   coret   nbrst   uR   t	   bin_start(    (   R   R   R   sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pyR       sD    $		

c            sk   ˆ  d k r t j |  ƒ ‰  n  ˆ d k r? t ˆ  j ƒ  ƒ ‰ n  ‡  ‡ f d †  ˆ  Dƒ } |  j | ƒ j ƒ  S(   sH  Return the k-core of G.

    A k-core is a maximal subgraph that contains nodes of degree k or more.

    Parameters
    ----------
    G : NetworkX graph
      A graph or directed graph
    k : int, optional
      The order of the core.  If not specified return the main core.
    core_number : dictionary, optional
      Precomputed core numbers for the graph G.

    Returns
    -------
    G : NetworkX graph
      The k-core subgraph

    Raises
    ------
    NetworkXError
      The k-core is not defined for graphs with self loops or parallel edges.

    Notes
    -----
    The main core is the core with the largest degree.

    Not implemented for graphs with parallel edges or self loops.

    For directed graphs the node degree is defined to be the
    in-degree + out-degree.

    Graph, node, and edge attributes are copied to the subgraph.

    See Also
    --------
    core_number

    References
    ----------
    .. [1] An O(m) Algorithm for Cores Decomposition of Networks
       Vladimir Batagelj and Matjaz Zaversnik,  2003.
       http://arxiv.org/abs/cs.DS/0310049
    c         3   s%   |  ] } ˆ  | ˆ k r | Vq d  S(   N(    (   R   t   n(   R    t   k(    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pys	   <genexpr>œ   s    N(   t   NoneR   R    t   maxt   valuest   subgrapht   copy(   R   R*   R    R    (    (   R    R*   sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pyR   k   s    -c            sk   ˆ  d k r t j |  ƒ ‰  n  ˆ d k r? t ˆ  j ƒ  ƒ ‰ n  ‡  ‡ f d †  ˆ  Dƒ } |  j | ƒ j ƒ  S(   sö  Return the k-shell of G.

    The k-shell is the subgraph of nodes in the k-core but not in the (k+1)-core.

    Parameters
    ----------
    G : NetworkX graph
      A graph or directed graph.
    k : int, optional
      The order of the shell.  If not specified return the main shell.
    core_number : dictionary, optional
      Precomputed core numbers for the graph G.


    Returns
    -------
    G : NetworkX graph
       The k-shell subgraph

    Raises
    ------
    NetworkXError
        The k-shell is not defined for graphs with self loops or parallel edges.

    Notes
    -----
    This is similar to k_corona but in that case only neighbors in the
    k-core are considered.

    Not implemented for graphs with parallel edges or self loops.

    For directed graphs the node degree is defined to be the
    in-degree + out-degree.

    Graph, node, and edge attributes are copied to the subgraph.

    See Also
    --------
    core_number
    k_corona


    References
    ----------
    .. [1] A model of Internet topology using k-shell decomposition
       Shai Carmi, Shlomo Havlin, Scott Kirkpatrick, Yuval Shavitt,
       and Eran Shir, PNAS  July 3, 2007   vol. 104  no. 27  11150-11154
       http://www.pnas.org/content/104/27/11150.full
    c         3   s%   |  ] } ˆ  | ˆ k r | Vq d  S(   N(    (   R   R)   (   R    R*   (    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pys	   <genexpr>Õ   s    N(   R+   R   R    R,   R-   R.   R/   (   R   R*   R    R    (    (   R    R*   sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pyR   Ÿ   s    2c            so   ˆ  d k r t j |  ƒ ‰  n  ˆ d k rC t ˆ  j ƒ  ƒ d ‰ n  ‡  ‡ f d †  ˆ  Dƒ } |  j | ƒ j ƒ  S(   sÿ  Return the k-crust of G.

    The k-crust is the graph G with the k-core removed.

    Parameters
    ----------
    G : NetworkX graph
       A graph or directed graph.
    k : int, optional
      The order of the shell.  If not specified return the main crust.
    core_number : dictionary, optional
      Precomputed core numbers for the graph G.

    Returns
    -------
    G : NetworkX graph
       The k-crust subgraph

    Raises
    ------
    NetworkXError
        The k-crust is not defined for graphs with self loops or parallel edges.

    Notes
    -----
    This definition of k-crust is different than the definition in [1]_.
    The k-crust in [1]_ is equivalent to the k+1 crust of this algorithm.

    Not implemented for graphs with parallel edges or self loops.

    For directed graphs the node degree is defined to be the
    in-degree + out-degree.

    Graph, node, and edge attributes are copied to the subgraph.

    See Also
    --------
    core_number

    References
    ----------
    .. [1] A model of Internet topology using k-shell decomposition
       Shai Carmi, Shlomo Havlin, Scott Kirkpatrick, Yuval Shavitt,
       and Eran Shir, PNAS  July 3, 2007   vol. 104  no. 27  11150-11154
       http://www.pnas.org/content/104/27/11150.full
    i   c         3   s%   |  ] } ˆ  | ˆ k r | Vq d  S(   N(    (   R   R)   (   R    R*   (    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pys	   <genexpr>  s    N(   R+   R   R    R,   R-   R.   R/   (   R   R*   R    R    (    (   R    R*   sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pyR   Ø   s    /c            sM   ˆ d k r t j ˆ  ƒ ‰ n  ‡  ‡ ‡ f d †  ˆ Dƒ } ˆ  j | ƒ j ƒ  S(   s�  Return the k-corona of G.

    The k-corona is the subgraph of nodes in the k-core which have
    exactly k neighbours in the k-core.

    Parameters
    ----------
    G : NetworkX graph
       A graph or directed graph
    k : int
       The order of the corona.
    core_number : dictionary, optional
       Precomputed core numbers for the graph G.

    Returns
    -------
    G : NetworkX graph
       The k-corona subgraph

    Raises
    ------
    NetworkXError
        The k-cornoa is not defined for graphs with self loops or
        parallel edges.

    Notes
    -----
    Not implemented for graphs with parallel edges or self loops.

    For directed graphs the node degree is defined to be the
    in-degree + out-degree.

    Graph, node, and edge attributes are copied to the subgraph.

    See Also
    --------
    core_number

    References
    ----------
    .. [1]  k -core (bootstrap) percolation on complex networks:
       Critical phenomena and nonlocal effects,
       A. V. Goltsev, S. N. Dorogovtsev, and J. F. F. Mendes,
       Phys. Rev. E 73, 056101 (2006)
       http://link.aps.org/doi/10.1103/PhysRevE.73.056101
    c         3   s^   |  ]T } ˆ | ˆ k r t  g  ˆ  | D] } ˆ | ˆ k r' | ^ q' ƒ ˆ k r | Vq d  S(   N(   t   len(   R   R)   R
   (   R   R    R*   (    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pys	   <genexpr>A  s    N(   R+   R   R    R.   R/   (   R   R*   R    R    (    (   R   R    R*   sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pyR     s    0(   t   __doc__t   joint
   __author__t   __all__t   networkxR   R    R   R+   R   R   R   R   (    (    (    sl   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/core.pyt   <module>   s   		N497