ó žÃÒYc@ sdZddlmZddlZddlmZddlmZdgZedƒedƒe d d „ƒƒZ d „Z dS( s/Functions for computing rich-club coefficients.iÿÿÿÿ(tdivisionN(t accumulate(tnot_implemented_fortrich_club_coefficienttdirectedt multigraphidc sŸtj|ƒdkr$tdƒ‚nt|ƒ}|r›|jƒ}|jƒ}tj|||d||dƒt|ƒ‰‡fd†|jƒDƒ}n|S(sReturns 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. 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 isDrich_club_coefficient is not implemented for graphs with self loops.t max_triesi c s'i|]\}}|ˆ||“qS(((t.0tktv(trcran(sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/richclub.pys Zs (tnxtnumber_of_selfloopst Exceptiont _compute_rctcopytnumber_of_edgestdouble_edge_swaptitems(tGt normalizedtQtrctRtE((R sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/richclub.pyRs:   " c  s tjˆƒ}t|ƒ‰‡fd†t|ƒDƒ}t‡fd†ˆjƒDƒdtƒ}ˆjƒ}|jƒ\}}i}xt |ƒD]q\}} xH||kræt |ƒdkrÇd}Pn|jƒ\}}|d8}qŸWd|| | d||lsc3 s'|]}ttˆj|ƒƒVqdS(N(tsortedtmaptdegree(Rte(R(sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/richclub.pys rstreverseiii( R tdegree_histogramtsumRRtedgestTrueRtpopt enumeratetlen( Rtdeghisttnkst edge_degreestektk1tk2Rtdtnk((RRsr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/richclub.pyR^s"    ( t__doc__t __future__RtnetworkxR tnetworkx.utilsRRt__all__R#RR(((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/richclub.pyt s    G