ó ŸÃÒYc@ södZddlmZddlmZddlZddlZddlm Z ddl m Z ddl m Z dd d d d gZ dddd „Zddd„Zddd„Zdddddddd„Zeedd„Zedd„ZdS(sd Generators for some directed graphs, including growing network (GN) graphs and scale-free graphs. iÿÿÿÿ(tdivision(tCounterN(t empty_graph(tdiscrete_sequence(tweighted_choicetgn_grapht gnc_grapht gnr_graphtrandom_k_out_graphtscale_free_graphc C s(|dkrtjƒ}n|jƒs9tjdƒ‚n|dkrQd„}n|dk rmtj|ƒntd|ƒ}|dkrŒ|S|jddƒddg}xyt d|ƒD]h}g|D]}||ƒ^qÅ}t dd|ƒd} |j|| ƒ|j dƒ|| cd7>> D = nx.gn_graph(10) # the GN graph >>> G = D.to_undirected() # the undirected version To specify an attachment kernel, use the `kernel` keyword argument:: >>> D = nx.gn_graph(10, kernel=lambda x: x ** 1.5) # A_k = k^1.5 References ---------- .. [1] P. L. Krapivsky and S. Redner, Organization of Growing Random Networks, Phys. Rev. E, 63, 066123, 2001. s'Directed Graph required in create_usingcS s|S(N((tx((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pytkernelLsiiit distributionN( tNonetnxtDiGrapht is_directedt NetworkXErrortrandomtseedRtadd_edgetrangeRtappend( tnR t create_usingRtGtdstsourcetdtdistttarget((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyRs('        cC sê|dkrtjƒ}n|jƒs9tjdƒ‚n|dk rUtj|ƒntd|ƒ}|dkrt|Sxotd|ƒD]^}tj d|ƒ}tjƒ|krÒ|dkrÒt |j |ƒƒ}n|j ||ƒq„W|S(s‹Return the growing network with redirection (GNR) digraph with `n` nodes and redirection probability `p`. The GNR graph is built by adding nodes one at a time with a link to one previously added node. The previous target node is chosen uniformly at random. With probabiliy `p` the link is instead "redirected" to the successor node of the target. The graph is always a (directed) tree. Parameters ---------- n : int The number of nodes for the generated graph. p : float The redirection probability. create_using : graph, optional (default DiGraph) Return graph of this type. The instance will be cleared. seed : hashable object, optional The seed for the random number generator. Examples -------- To create the undirected GNR graph, use the :meth:`~DiGraph.to_directed` method:: >>> D = nx.gnr_graph(10, 0.5) # the GNR graph >>> G = D.to_undirected() # the undirected version References ---------- .. [1] P. L. Krapivsky and S. Redner, Organization of Growing Random Networks, Phys. Rev. E, 63, 066123, 2001. s'Directed Graph required in create_usingiiN( R RRRRRRRRt randrangetnextt successorsR(RtpRRRRR((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyRds$    cC sÞ|dkrtjƒ}n|jƒs9tjdƒ‚n|dk rUtj|ƒntd|ƒ}|dkrt|Sxctd|ƒD]R}tj d|ƒ}x'|j |ƒD]}|j ||ƒq¬W|j ||ƒq„W|S(sÂReturn the growing network with copying (GNC) digraph with `n` nodes. The GNC graph is built by adding nodes one at a time with a link to one previously added node (chosen uniformly at random) and to all of that node's successors. Parameters ---------- n : int The number of nodes for the generated graph. create_using : graph, optional (default DiGraph) Return graph of this type. The instance will be cleared. seed : hashable object, optional The seed for the random number generator. References ---------- .. [1] P. L. Krapivsky and S. Redner, Network Growth by Copying, Phys. Rev. E, 71, 036118, 2005k.}, s'Directed Graph required in create_usingiiN( R RRRRRRRRRR!R(RRRRRRtsucc((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyRžs    g= ×£p=Ú?gHáz®Gá?gš™™™™™©?gš™™™™™É?icC sd„}|d kr:tjƒ} | jd d d gƒn0|} | jƒoU| jƒsjtjdƒ‚n|dkr…tdƒ‚n|dkr tdƒ‚n|dkr»tdƒ‚n|||dkrÞtd ƒ‚ntj |ƒ| j ƒ} x t | ƒ|kr| |t | ƒ} | |t | ƒ} tjƒ} | |krvt | ƒ}|| | j ƒ|| ƒ}np| ||kr¿|| | j ƒ|| ƒ}|| | j ƒ|| ƒ}n'|| | j ƒ|| ƒ}t | ƒ}| j||ƒ| d7} qúW| S(sØReturns a scale-free directed graph. Parameters ---------- n : integer Number of nodes in graph alpha : float Probability for adding a new node connected to an existing node chosen randomly according to the in-degree distribution. beta : float Probability for adding an edge between two existing nodes. One existing node is chosen randomly according the in-degree distribution and the other chosen randomly according to the out-degree distribution. gamma : float Probability for adding a new node connected to an existing node chosen randomly according to the out-degree distribution. delta_in : float Bias for choosing ndoes from in-degree distribution. delta_out : float Bias for choosing ndoes from out-degree distribution. create_using : graph, optional (default MultiDiGraph) Use this graph instance to start the process (default=3-cycle). seed : integer, optional Seed for random number generator Examples -------- Create a scale-free graph on one hundred nodes:: >>> G = nx.scale_free_graph(100) Notes ----- The sum of `alpha`, `beta`, and `gamma` must be 1. References ---------- .. [1] B. Bollobás, C. Borgs, J. Chayes, and O. Riordan, Directed scale-free graphs, Proceedings of the fourteenth annual ACM-SIAM Symposium on Discrete Algorithms, 132--139, 2003. cS sOd}tjƒ}x6|D].\}}||||7}||krPqqW|S(Ng(R(RR tdeltatpsumtcumsumtrRR((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyt _choose_nodeøs  iiis%MultiDiGraph required in create_usingsalpha must be >= 0.sbeta must be >= 0.gð?salpha+beta+gamma must equal 1.N(ii(ii(ii(R Rt MultiDiGraphtadd_edges_fromRt is_multigraphRt ValueErrorRRtnumber_of_edgestlent in_degreet out_degreeR(Rtalphatbetatgammatdelta_int delta_outRRR(RR-tpsum_intpsum_outR'tvtw((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyR Ês@.           c  s®tj|ƒ|r4tjƒ}‡‡fd†}ntjƒ}‡‡fd†}tj|d|ƒ}t|ƒ}x4|D],‰|j‡fd†|ˆ|ƒDƒƒqzW|S(s0Returns a random `k`-out graph with uniform attachment. A random `k`-out graph with uniform attachment is a multidigraph generated by the following algorithm. For each node *u*, choose `k` nodes *v* uniformly at random (with replacement). Add a directed edge joining *u* to *v*. Parameters ---------- n : int The number of nodes in the returned graph. k : int The out-degree of each node in the returned graph. self_loops : bool If True, self-loops are allowed when generating the graph. with_replacement : bool If True, neighbors are chosen with replacement and the returned graph will be a directed multigraph. Otherwise, neighbors are chosen without replacement and the returned graph will be a directed graph. seed: int If provided, this is used as the seed for the random number generator. Returns ------- NetworkX graph A `k`-out-regular directed graph generated according to the above algorithm. It will be a multigraph if and only if `with_replacement` is True. Raises ------ ValueError If `with_replacement` is False and `k` is greater than `n`. See also -------- random_k_out_graph Notes ----- The return digraph or multidigraph may not be strongly connected, or even weakly connected. If `with_replacement` is True, this function is similar to :func:`random_k_out_graph`, if that function had parameter `alpha` set to positive infinity. c s0ˆsˆ|h‰n‡fd†tˆƒDƒS(Nc3 s$|]}tjtˆƒƒVqdS(N(Rtchoicetlist(t.0ti(tnodes(sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pys ws(R(R8R>(tkt self_loops(R>sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pytsampletsc s&ˆs||h}ntj|ˆƒS(N(RRA(R8R>(R?R@(sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyRA|sRc3 s|]}ˆ|fVqdS(N((R<R8(tu(sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pys „s(RRRR)RRtsetR*( RR?R@twith_replacementRRRARR>((R?R@RBsr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pytrandom_uniform_k_out_graph6s9     *c  sˆdkrtdƒ‚ntj|ƒtj|dtjƒƒ}t‡fd†|Dƒƒ}x«t||ƒD]™}tjg|j ƒD]\}} | |kr‰|^q‰ƒ} |sÐti|| | 6ƒ} n tƒ} t || ƒ}|j | |ƒ||cd7 isalpha must be positiveRc si|]}ˆ|“qS(((R<R8(R1(sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pys Ïs i( R,RRRRR)RRR:R0RR( RR?R1R@RRtweightsR=R8RRBt adjustment((R1sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyRˆsC  : (t__doc__t __future__Rt collectionsRRtnetworkxRtnetworkx.generators.classicRtnetworkx.utilsRRt__all__R RRRR tTrueRER(((sr/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/generators/directed.pyts"    E:, k Q