Yc@ sdZddlmZddlZddlZddlZddlZddlm Z m Z m Z ddl m Z ddlmZdd d d d d ddddddddddddgZdedZdedZeZeZddZdedZddZddZd dd!Zdd"Zd#Zdd$Zdd%Zdd&Z dd'Z!dd(Z"d)dd d*Z#d)dd d+Z$ddd,Z%dS(-s Generators for random graphs. i(tdivisionNi(t empty_grapht path_graphtcomplete_graph(tdegree_sequence_tree(t defaultdicttfast_gnp_random_graphtgnp_random_graphtdense_gnm_random_graphtgnm_random_graphterdos_renyi_graphtbinomial_graphtnewman_watts_strogatz_graphtwatts_strogatz_graphtconnected_watts_strogatz_graphtrandom_regular_graphtbarabasi_albert_graphtextended_barabasi_albert_graphtpowerlaw_cluster_graphtrandom_lobstertrandom_shell_graphtrandom_powerlaw_treetrandom_powerlaw_tree_sequencetrandom_kernel_graphc C st|}|dk r(tj|n|dks@|dkrVtj||d|Sd}tjd|}|rYtj|}d}xh||krUtjdtj}|dt ||}||kr|d}nxM||ko|knr2||}|d}||kr|d}qqW||kr|j ||qqWnd}x||krtjdtj}|dt ||}x0||kr||kr||}|d}qW||krb|j ||qbqbW|S(s>Returns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph or a binomial graph. Parameters ---------- n : int The number of nodes. p : float Probability for edge creation. seed : int, optional Seed for random number generator (default=None). directed : bool, optional (default=False) If True, this function returns a directed graph. Notes ----- The $G_{n,p}$ graph algorithm chooses each of the $[n (n - 1)] / 2$ (undirected) or $n (n - 1)$ (directed) possible edges with probability $p$. This algorithm [1]_ runs in $O(n + m)$ time, where `m` is the expected number of edges, which equals $p n (n - 1) / 2$. This should be faster than :func:`gnp_random_graph` when $p$ is small and the expected number of edges is small (that is, the graph is sparse). See Also -------- gnp_random_graph References ---------- .. [1] Vladimir Batagelj and Ulrik Brandes, "Efficient generation of large random networks", Phys. Rev. E, 71, 036113, 2005. iitdirectedig?N( RtNonetrandomtseedtnxRtmathtlogtDiGraphtinttadd_edge( tntpRRtGtwtlptvtlr((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyR0s@#          cC s|rtj}n tj}|jt||dkrD|S|dkr`t|d|S|dk r|tj|n|j rt j t|d}nt j t|d}x0|D](}tj|kr|j |qqW|S(sReturns a $G_{n,p}$ random graph, also known as an Erdős-Rényi graph or a binomial graph. The $G_{n,p}$ model chooses each of the possible edges with probability $p$. The functions :func:`binomial_graph` and :func:`erdos_renyi_graph` are aliases of this function. Parameters ---------- n : int The number of nodes. p : float Probability for edge creation. seed : int, optional Seed for random number generator (default=None). directed : bool, optional (default=False) If True, this function returns a directed graph. See Also -------- fast_gnp_random_graph Notes ----- This algorithm [2]_ runs in $O(n^2)$ time. For sparse graphs (that is, for small values of $p$), :func:`fast_gnp_random_graph` is a faster algorithm. References ---------- .. [1] P. Erdős and A. Rényi, On Random Graphs, Publ. Math. 6, 290 (1959). .. [2] E. N. Gilbert, Random Graphs, Ann. Math. Stat., 30, 1141 (1959). iit create_usingiN(RRtGraphtadd_nodes_fromtrangeRRRRt is_directedt itertoolst permutationst combinationsR!(R"R#RRR$tedgeste((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyR|s""      c C s||dd}||kr-t|}n t|}|dksQ||krU|S|dk rqtj|nd}d}d}d}xtrtj||||kr|j|||d7}||kr|Sn|d7}|d7}||kr|d7}|d}qqWdS(sFReturns a $G_{n,m}$ random graph. In the $G_{n,m}$ model, a graph is chosen uniformly at random from the set of all graphs with $n$ nodes and $m$ edges. This algorithm should be faster than :func:`gnm_random_graph` for dense graphs. Parameters ---------- n : int The number of nodes. m : int The number of edges. seed : int, optional Seed for random number generator (default=None). See Also -------- gnm_random_graph() Notes ----- Algorithm by Keith M. Briggs Mar 31, 2006. Inspired by Knuth's Algorithm S (Selection sampling technique), in section 3.4.2 of [1]_. References ---------- .. [1] Donald E. Knuth, The Art of Computer Programming, Volume 2/Seminumerical algorithms, Third Edition, Addison-Wesley, 1997. iiiN(RRRRRtTruet randrangeR!( R"tmRtmmaxR$tuR'tttk((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyRs.!          c C s"|rtj}n tj}|jt||dk rPtj|n|dkr`|S||d}|s|d}n||krt|d|St |}d}xl||krtj |}tj |} || ks|j || rqq|j || |d}qW|S(sVReturns a $G_{n,m}$ random graph. In the $G_{n,m}$ model, a graph is chosen uniformly at random from the set of all graphs with $n$ nodes and $m$ edges. This algorithm should be faster than :func:`dense_gnm_random_graph` for sparse graphs. Parameters ---------- n : int The number of nodes. m : int The number of edges. seed : int, optional Seed for random number generator (default=None). directed : bool, optional (default=False) If True return a directed graph See also -------- dense_gnm_random_graph ig@R)iN( RRR*R+R,RRRRtlisttchoicethas_edgeR!( R"R5RRR$t max_edgestnlistt edge_countR7R'((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyR s.      cC s||dk rtj|n||kr:tjdnt|}t|j}|}xitd|ddD]P}|||d|!}x2tt |D]} |j || || qWqvWt|j } x| D]\} } tj|krtj |} xa| | ks1|j | | r`tj |} |j| |dkrPqqW|j | | qqW|S(sReturn a Newman–Watts–Strogatz small-world graph. Parameters ---------- n : int The number of nodes. k : int Each node is joined with its `k` nearest neighbors in a ring topology. p : float The probability of adding a new edge for each edge. seed : int, optional The seed for the random number generator (the default is None). Notes ----- First create a ring over $n$ nodes [1]_. Then each node in the ring is connected with its $k$ nearest neighbors (or $k - 1$ neighbors if $k$ is odd). Then shortcuts are created by adding new edges as follows: for each edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest neighbors" with probability $p$ add a new edge $(u, w)$ with randomly-chosen existing node $w$. In contrast with :func:`watts_strogatz_graph`, no edges are removed. See Also -------- watts_strogatz_graph() References ---------- .. [1] M. E. J. Newman and D. J. Watts, Renormalization group analysis of the small-world network model, Physics Letters A, 263, 341, 1999. http://dx.doi.org/10.1016/S0375-9601(99)00757-4 s"k>=n, choose smaller k or larger niiiN(RRRRt NetworkXErrorRR:tnodesR,tlenR!R1R;R<tdegree(R"R9R#RR$R>tfromvtjttovtiR2R7R'R%((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyR 1s*$    !c C s||krtjdn|dk r:tj|ntj}tt|}xJtd|ddD]1}|||d|!}|jt ||qpWxtd|ddD]}|||d|!}xt ||D]\}} tj|krtj |} xq| |ks6|j || retj |} |j ||dkrPqqW|j || |j|| qqWqW|S(sReturn a Watts–Strogatz small-world graph. Parameters ---------- n : int The number of nodes k : int Each node is joined with its `k` nearest neighbors in a ring topology. p : float The probability of rewiring each edge seed : int, optional Seed for random number generator (default=None) See Also -------- newman_watts_strogatz_graph() connected_watts_strogatz_graph() Notes ----- First create a ring over $n$ nodes [1]_. Then each node in the ring is joined to its $k$ nearest neighbors (or $k - 1$ neighbors if $k$ is odd). Then shortcuts are created by replacing some edges as follows: for each edge $(u, v)$ in the underlying "$n$-ring with $k$ nearest neighbors" with probability $p$ replace it with a new edge $(u, w)$ with uniformly random choice of existing node $w$. In contrast with :func:`newman_watts_strogatz_graph`, the random rewiring does not increase the number of edges. The rewired graph is not guaranteed to be connected as in :func:`connected_watts_strogatz_graph`. References ---------- .. [1] Duncan J. Watts and Steven H. Strogatz, Collective dynamics of small-world networks, Nature, 393, pp. 440--442, 1998. s"k>=n, choose smaller k or larger niiiN(RR@RRRR*R:R,tadd_edges_fromtzipR;R<RCt remove_edgeR!( R"R9R#RR$RAREttargetsR7R'R%((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyR rs*'   !idcC sRx<t|D].}t||||}tj|r |Sq WtjddS(sReturns a connected Watts–Strogatz small-world graph. Attempts to generate a connected graph by repeated generation of Watts–Strogatz small-world graphs. An exception is raised if the maximum number of tries is exceeded. Parameters ---------- n : int The number of nodes k : int Each node is joined with its `k` nearest neighbors in a ring topology. p : float The probability of rewiring each edge tries : int Number of attempts to generate a connected graph. seed : int, optional The seed for random number generator. See Also -------- newman_watts_strogatz_graph() watts_strogatz_graph() s Maximum number of tries exceededN(R,R Rt is_connectedR@(R"R9R#ttriesRRGR$((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyRs c sddkr&tjdndko=knsTtjdndkrjtS|dk rtj|ndfd}|}x|dkr|}qWtj}|j||S(sReturns a random $d$-regular graph on $n$ nodes. The resulting graph has no self-loops or parallel edges. Parameters ---------- d : int The degree of each node. n : integer The number of nodes. The value of $n \times d$ must be even. seed : hashable object The seed for random number generator. Notes ----- The nodes are numbered from $0$ to $n - 1$. Kim and Vu's paper [2]_ shows that this algorithm samples in an asymptotically uniform way from the space of random graphs when $d = O(n^{1 / 3 - \epsilon})$. Raises ------ NetworkXError If $n \times d$ is odd or $d$ is greater than or equal to $n$. References ---------- .. [1] A. Steger and N. Wormald, Generating random regular graphs quickly, Probability and Computing 8 (1999), 377-396, 1999. http://citeseer.ist.psu.edu/steger99generating.html .. [2] Jeong Han Kim and Van H. Vu, Generating random regular graphs, Proceedings of the thirty-fifth ACM symposium on Theory of computing, San Diego, CA, USA, pp 213--222, 2003. http://portal.acm.org/citation.cfm?id=780542.780576 iisn * d must be evens+the 0 <= d < n inequality must be satisfiedcS sr|s tSxa|D]Y}xP|D]H}||kr4Pn||krP||}}n||f|krtSqWqWtS(N(R3tFalse(R1tpotential_edgests1ts2((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyt _suitables     c  s3t}tt}x |r.td}tj|t|}xt||D]|\}}||kr||}}n||kr||f|kr|j||fq`||cd7<||cd7)si( tsetR:R,RRtshuffletiterRItaddRtitems( R1tstubsROtstubiterRPRQtnodet potentialt_(RRtdR"(sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyt _try_creation"s$     #N(RR@RRRRR*RH(R^R"RR_R1R$((RRR^R"sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyRs )        cC sBt}x2t||kr=tj|}|j|q W|S(s Return m unique elements from seq. This differs from random.sample which can return repeated elements if seq holds repeated elements. (RTRBRR;RW(tseqR5RKtx((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyt_random_subsetIs  cC s|dks||kr4tjd||fn|dk rPtj|nt|}tt|}g}|}xg||kr|jt |g|||j ||j |g|t ||}|d7}q}W|S(sReturns a random graph according to the Barabási–Albert preferential attachment model. A graph of $n$ nodes is grown by attaching new nodes each with $m$ edges that are preferentially attached to existing nodes with high degree. Parameters ---------- n : int Number of nodes m : int Number of edges to attach from a new node to existing nodes seed : int, optional Seed for random number generator (default=None). Returns ------- G : Graph Raises ------ NetworkXError If `m` does not satisfy ``1 <= m < n``. References ---------- .. [1] A. L. Barabási and R. Albert "Emergence of scaling in random networks", Science 286, pp 509-512, 1999. isEBarabási–Albert network must have m >= 1 and m < n, m = %d, n = %dN( RR@RRRRR:R,RHRItextendRb(R"R5RR$RKtrepeated_nodestsource((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyRVs     cC sf|dks||kr:d}tj|||fn||dkrld}tj|||fn|dk rtj|nt|}g}|jt||}x||kratj} t|d} t|| d} | |kr9|j | |kr9g|j D]\} } | | kr!| ^q!}xt|D]}tj |}t ||}|j |tj g|D]} | |kr| ^q}|j|||j ||j ||j || kr|j|n|j || krR||krR|j|qRqRWq|| koT||knr||j kov| knrg|j D].\} } d| ko| knr| ^q}xt|D]3}tj |}t ||}tj |}|j |tj g|D]} | |kr| ^q}|j|||j|||j||j ||j |dkr||kr|j|n||kr|j || kr|j|qq|j |dkr|j |qqWqt||}|jt|g|||j||j|g|d|d7}qW|S(sAReturns an extended Barabási–Albert model graph. An extended Barabási–Albert model graph is a random graph constructed using preferential attachment. The extended model allows new egdes, rewired edges or new nodes. Based on the probabilities $p$ and $q$ with $p + q < 1$, the growing behavior of the graph is determined as: 1) With $p$ probability, $m$ new edges are added to the graph, starting from randomly chosen existing nodes and attached preferentially at the other end. 2) With $q$ probability, $m$ existing edges are rewired by randomly chosing an edge and rewiring one end to a preferentially chosen node. 3) With $(1 - p - q)$ probability, $m$ new nodes are added to the graph with edges attached preferentially. When $p = q = 0$, the model behaves just like the Barabási–Alber mo Parameters ---------- n : int Number of nodes m : int Number of edges with which a new node attaches to existing nodes p : float Probability value for adding an edge between existing nodes. p + q < 1 q : float Probability value of rewiring of existing edges. p + q < 1 seed : int (optional, default: None) Seed for random number generator Returns ------- G : Graph Raises ------ NetworkXError If `m` does not satisfy ``1 <= m < n`` or ``1 >= p + q`` References ---------- .. [1] Albert, R., & Barabási, A. L. (2000) Topology of evolving networks: local events and universality Physical review letters, 85(24), 5234. is?Extended Barabasi-Albert network needs m>=1 and m1 and m>> constructor = [(10, 20, 0.8), (20, 40, 0.8)] >>> G = nx.random_shell_graph(constructor) it first_labeliN(RRRRR RgRtconvert_node_labels_to_integersR t operatorstunionR,RBR:R;R<R!(t constructorRR$tglistt intra_edgestnnodesR"R5R^t inter_edgestgtgitnlist1tnlist2t total_edgesR?R7R'((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyRs8      icC s.t|d|d|d|}t|}|S(sReturns a tree with a power law degree distribution. Parameters ---------- n : int The number of nodes. gamma : float Exponent of the power law. seed : int, optional Seed for random number generator (default=None). tries : int Number of attempts to adjust the sequence to make it a tree. Raises ------ NetworkXError If no valid sequence is found within the maximum number of attempts. Notes ----- A trial power law degree sequence is chosen and then elements are swapped with new elements from a powerlaw distribution until the sequence makes a tree (by checking, for example, that the number of edges is one smaller than the number of nodes). tgammaRRM(RR(R"RRRMR`R$((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyRs c C s&|dk rtj|ntjj|d|}g|D]*}t|ttt |d^q;}tjj|d|}g|D]*}t|ttt |d^q}xR|D]J}d|t |dkr|Stj d|d} |j || >> def integral(u, w, z): ... return c * (z - w) >>> def root(u, w, r): ... return r / c + w >>> c = 1 >>> graph = nx.random_kernel_graph(1000, integral, root) See Also -------- gnp_random_graph expected_degree_graph References ---------- .. [1] Bollobás, Béla, Janson, S. and Riordan, O. "The phase transition in inhomogeneous random graphs", *Random Structures Algorithms*, 31, 3--122, 2007. .. [2] Hagberg A, Lemons N (2015), "Fast Generation of Sparse Random Kernel Graphs". PLoS ONE 10(9): e0135177, 2015. doi:10.1371/journal.pone.0135177 iNc s+fd}j|dS(Nc s|S(N((tb(tatkernel_integraltrty(sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyt my_functionsi(tbrentq(RRRR(Rtoptimize(RRRsw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyt kernel_rootsi(ii(RRRtscipy.optimizeRRR*R+R,RRR tceilR!(R"RRRtgraphRGRER((RRsw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyRBs:     -(&t__doc__t __future__RR.RRtnetworkxRtclassicRRRt degree_seqRt collectionsRt__all__RRNRRR R RR R R RRRbRRRRRRRR(((sw/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/random_graphs.pyt sZ     L; >8 A F" o ; Y # :"8