K idZddlmZddlZddlmZdgZededejd dZ d Z y) z/Functions for computing rich-club coefficients.) accumulateN)not_implemented_forrich_club_coefficientdirected multigraphc^tj|dkDr tdt|}|rv|j }|j }tj |||z||zdz|t|}|jD cic]\}} || ||z }}} |Scc} }w)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`. 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 rzDrich_club_coefficient is not implemented for graphs with self loops. ) max_triesseed)nxnumber_of_selfloops Exception _compute_rccopynumber_of_edgesdouble_edge_swapitems) G normalizedQr rcRErcrankvs b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/richclub.pyrr sX a 1$ R   QB FFH    Aq1uA FA*,((* 5$!QaU1Xo 5 5 I6sB)c tj}t| fdt|D}t fdj Dd}j }|dk(riS|j\}}i}t|D]J\}} ||kr/t|dk(rd}n|j\}}|dz}||kr/d|z| | dz zz ||<L|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. c3:K|]}|z dkDs |z yw)N).0cstotals r z_compute_rc..us F"urzA~52: Fs  c3\K|]#}ttj|%yw)N)sortedmapdegree)r"ers rr%z_compute_rc..{s!G6#ahh"23Gs),T)reverserr ) r degree_histogramsumrr'edgesrpop enumeratelen) rdeghistnks edge_degreesekk1k2rdnkr$s ` @rrrgs!!!$G LE G 7 3 FC GQWWYGQUVL   B Qw    FB B3)2Ag< A%!%%'FB !GB Ag B"Q-(1) I)TdN) __doc__ itertoolsrnetworkxr networkx.utilsr__all__ _dispatchablerrr!r;rrCsU5 . " #Z \"V#!Vr#r;