
    h                         S r SSKJr  SSKrSSKJr  S/r\" S5      \" S5      \R                  S
S j5       5       5       r	S	 r
g)z/Functions for computing rich-club coefficients.    )
accumulateN)not_implemented_forrich_club_coefficientdirected
multigraphc                 d   [         R                  " U 5      S:  a  [        S5      e[        U 5      nU(       ar  U R	                  5       nUR                  5       n[         R                  " XRU-  X&-  S-  US9  [        U5      nUR                  5        VV	s0 s H  u  pXXx   -  _M     nnn	U$ s  sn	nf )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.

Raises
------
NetworkXError
    If `G` has fewer than four nodes and ``normalized=True``.
    A randomly sampled graph for normalization cannot be generated in this case.

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.

Normalization is done by computing the rich club coefficient for a randomly
sampled graph with the same degree distribution as `G` by
repeatedly swapping the endpoints of existing edges. For graphs with fewer than 4
nodes, it is not possible to generate a random graph with a prescribed
degree distribution, as the degree distribution fully determines the graph
(hence making the coefficients trivially normalized to 1).
This function raises an exception in this case.

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seed)nxnumber_of_selfloops	Exception_compute_rccopynumber_of_edgesdouble_edge_swapitems)
G
normalizedQr   rcRErcrankvs
             N/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/richclub.pyr   r      s    X 
a 1$R
 	
 
QB FFH
A1u
FA*,((*5*$!aUXo*5I 6s   B,c                   ^ ^
 [         R                  " T 5      n[        U5      m
U
4S j[        U5       5       n[	        U 4S jT R                  5        5       SS9nT R                  5       nUS:X  a  0 $ UR                  5       u  pV0 n[        U5       HI  u  pXX::  a0  [        U5      S:X  a  SnOUR                  5       u  pVUS-  nXX::  a  M0  SU-  XS-
  -  -  Xx'   MK     U$ )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   D   >#    U  H  nTU-
  S :  d  M  TU-
  v   M     g7f)   N ).0cstotals     r   	<genexpr>_compute_rc.<locals>.<genexpr>u   s#     
F 3"urzA~:52: 3s     c              3   b   >#    U  H$  n[        [        TR                  U5      5      v   M&     g 7f)N)sortedmapdegree)r"   er   s     r   r%   r&   {   s$     GY6#ahh"233Ys   ,/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   g   s     !!!$GLE G
7 3
FC GQWWYGQUVL	
			B	Qw	FB	B3g< A%!%%'FB!GB g B"Q-(   I    )Td   N)__doc__	itertoolsr   networkxr   networkx.utilsr   __all___dispatchabler   r   r!   r<   r   <module>rD      sU    5    ."
# Z \"V  # !Vr#r<   