
    Gd8                         d Z ddlmZ ddlZddlmZ dgZ ed           ed          dd
                        Zd Z	dS )z/Functions for computing rich-club coefficients.    )
accumulateN)not_implemented_forrich_club_coefficientdirected
multigraphTd   c                 h   t          j        |           dk    rt          d          t          |           }|rx|                                 }|                                }t          j        |||z  ||z  dz  |           t          |          fd|                                D             }|S )u  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, seed=42)
    >>> rc[0]
    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
    r   zDrich_club_coefficient is not implemented for graphs with self loops.
   )	max_triesseedc                 .    i | ]\  }}|||         z  S  r   ).0kvrcrans      <lib/python3.11/site-packages/networkx/algorithms/richclub.py
<dictcomp>z)rich_club_coefficient.<locals>.<dictcomp>T   s'    555$!QaU1X555    )nxnumber_of_selfloops	Exception_compute_rccopynumber_of_edgesdouble_edge_swapitems)G
normalizedQr   rcREr   s          @r   r   r      s    z 
a  1$$U
 
 	
 
QB 6 FFHH
Aq1uA
FFFFA5555"((**555Ir   c                    
 t          j                   }t          |          

fdt          |          D             }t	           fd                                 D             d          }                                 }|                                \  }}i }t          |          D ]T\  }}	||k    r8t          |          dk    rd}n"|                                \  }}|dz  }||k    8d|z  |	|	dz
  z  z  ||<   U|S )zReturns 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   4   K   | ]}|z
  d k    |z
  V  dS )   Nr   )r   cstotals     r   	<genexpr>z_compute_rc.<locals>.<genexpr>f   s/      
F
F"urzA~~52:~~~~
F
Fr   c              3   \   K   | ]&}t          t          j        |                    V  'd S )N)sortedmapdegree)r   er   s     r   r)   z_compute_rc.<locals>.<genexpr>l   s7      GG6#ah"2"233GGGGGGr   T)reverser   r&      )
r   degree_histogramsumr   r+   edgesr   pop	enumeratelen)r   deghistnksedge_degreesekk1k2r!   dnkr(   s   `         @r   r   r   X   s+    !!$$GLLE G
F
F
F
7 3 3
F
F
FC GGGGQWWYYGGGQUVVVL	
				BFB	B3 ) )2Agg<  A%%!%%''FB!GB Agg B"Q-(1Ir   )Tr   N)
__doc__	itertoolsr   networkxr   networkx.utilsr   __all__r   r   r   r   r   <module>rD      s    5 5                 . . . . . ."
# Z  \""H H H #" ! HV         r   