ó
 ,µ[c           @   s‚   d  Z  d d l m Z d d l Z d d l m Z d d l m Z d g Z e d ƒ e d ƒ e	 d	 d d
 „ ƒ ƒ Z d „  Z d S(   s/   Functions for computing rich-club coefficients.iÿÿÿÿ(   t   divisionN(   t
   accumulate(   t   not_implemented_fort   rich_club_coefficientt   directedt
   multigraphid   c            s¥   t  j |  ƒ d k r$ t d ƒ ‚ n  t |  ƒ } | r¡ |  j ƒ  } | j ƒ  } t  j | | | d | | d d | ƒt | ƒ ‰  ‡  f d †  | j ƒ  Dƒ } n  | S(   s  Returns the rich-club coefficient of the graph `G`.

    For each degree *k*, the *rich-club coefficient* is the ratio of the
    number of actual to the number of potential edges for nodes with
    degree greater than *k*:

    .. math::

        \phi(k) = \frac{2 E_k}{N_k (N_k - 1)}

    where `N_k` is the number of nodes with degree larger than *k*, and
    `E_k` is the number of edges among those nodes.

    Parameters
    ----------
    G : NetworkX graph
        Undirected graph with neither parallel edges nor self-loops.
    normalized : bool (optional)
        Normalize using randomized network as in [1]_
    Q : float (optional, default=100)
        If `normalized` is True, perform `Q * m` double-edge
        swaps, where `m` is the number of edges in `G`, to use as a
        null-model for normalization.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    rc : dictionary
       A dictionary, keyed by degree, with rich-club coefficient values.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3), (1, 4), (4, 5)])
    >>> rc = nx.rich_club_coefficient(G, normalized=False)
    >>> rc[0] # doctest: +SKIP
    0.4

    Notes
    -----
    The rich club definition and algorithm are found in [1]_.  This
    algorithm ignores any edge weights and is not defined for directed
    graphs or graphs with parallel edges or self loops.

    Estimates for appropriate values of `Q` are found in [2]_.

    References
    ----------
    .. [1] Julian J. McAuley, Luciano da Fontoura Costa,
       and TibÃ©rio S. Caetano,
       "The rich-club phenomenon across complex network hierarchies",
       Applied Physics Letters Vol 91 Issue 8, August 2007.
       https://arxiv.org/abs/physics/0701290
    .. [2] R. Milo, N. Kashtan, S. Itzkovitz, M. E. J. Newman, U. Alon,
       "Uniform generation of random graphs with arbitrary degree
       sequences", 2006. https://arxiv.org/abs/cond-mat/0312028
    i    sD   rich_club_coefficient is not implemented for graphs with self loops.t	   max_triesi
   t   seedc            s'   i  |  ] \ } } | ˆ  | | “ q S(    (    (   t   .0t   kt   v(   t   rcran(    s;   lib/python2.7/site-packages/networkx/algorithms/richclub.pys
   <dictcomp>]   s   	 (   t   nxt   number_of_selfloopst	   Exceptiont   _compute_rct   copyt   number_of_edgest   double_edge_swapt   items(   t   Gt
   normalizedt   QR   t   rct   Rt   E(    (   R   s;   lib/python2.7/site-packages/networkx/algorithms/richclub.pyR      s    =(c   
         s	  t  j ˆ  ƒ } t | ƒ ‰ ‡ f d †  t | ƒ Dƒ } t ‡  f d †  ˆ  j ƒ  Dƒ d t ƒ} ˆ  j ƒ  } | j ƒ  \ } } i  } x t	 | ƒ D]q \ } }	 xH | | k ræ t
 | ƒ d k rÇ d } Pn  | j ƒ  \ } } | d 8} qŸ Wd | |	 |	 d | | <q W| S(   sØ   Returns the rich-club coefficient for each degree in the graph
    `G`.

    `G` is an undirected graph without multiedges.

    Returns a dictionary mapping degree to rich-club coefficient for
    that degree.

    c         3   s)   |  ] } ˆ  | d  k r ˆ  | Vq d S(   i   N(    (   R   t   cs(   t   total(    s;   lib/python2.7/site-packages/networkx/algorithms/richclub.pys	   <genexpr>o   s    c         3   s'   |  ] } t  t ˆ  j | ƒ ƒ Vq d  S(   N(   t   sortedt   mapt   degree(   R   t   e(   R   (    s;   lib/python2.7/site-packages/networkx/algorithms/richclub.pys	   <genexpr>u   s    t   reversei    i   i   (   R   t   degree_histogramt   sumR   R   t   edgest   TrueR   t   popt	   enumeratet   len(
   R   t   deghistt   nkst   edge_degreest   ekt   k1t   k2R   t   dt   nk(    (   R   R   s;   lib/python2.7/site-packages/networkx/algorithms/richclub.pyR   a   s"    
	(   t   __doc__t
   __future__R    t   networkxR   t   networkx.utilsR   R   t   __all__R$   t   NoneR   R   (    (    (    s;   lib/python2.7/site-packages/networkx/algorithms/richclub.pyt   <module>   s   			J