Ë
    D^(hP=  ã                   ó"  — d Z ddlZddlmZ ddlZddlmZ ddlm	Z	m
Z
mZ g d¢Z e
d«       ej                  dd¬	«      dd
„«       «       Z e
d«       ej                  dd¬	«      dd„«       «       Z e
d«       ej                  dd¬	«      dd„«       «       Z e
d«       ej                  dd¬	«      	 	 	 	 	 	 	 dd„«       «       Z e
d«       ej                  dd¬	«      dd„«       «       Z e
d«       ej                  dd¬	«      dd„«       «       Zy)zd
Generators for some directed graphs, including growing network (GN) graphs and
scale-free graphs.

é    N)ÚCounter)Úempty_graph)Údiscrete_sequenceÚpy_random_stateÚweighted_choice)Úgn_graphÚ	gnc_graphÚ	gnr_graphÚrandom_k_out_graphÚscale_free_graphé   T)ÚgraphsÚreturns_graphc                 ó¤  — t        d|t        j                  ¬«      }|j                  «       st        j                  d«      ‚|€d„ }| dk(  r|S |j                  dd«       ddg}t        d| «      D ]X  }|D �cg c]
  } ||«      ‘Œ }}t        d||¬«      d   }	|j                  ||	«       |j                  d«       ||	xx   dz  cc<   ŒZ |S c c}w )aB  Returns the growing network (GN) digraph with `n` nodes.

    The GN graph is built by adding nodes one at a time with a link to one
    previously added node.  The target node for the link is chosen with
    probability based on degree.  The default attachment kernel is a linear
    function of the degree of a node.

    The graph is always a (directed) tree.

    Parameters
    ----------
    n : int
        The number of nodes for the generated graph.
    kernel : function
        The attachment kernel.
    create_using : NetworkX graph constructor, optional (default DiGraph)
        Graph type to create. If graph instance, then cleared before populated.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Examples
    --------
    To create the undirected GN graph, use the :meth:`~DiGraph.to_directed`
    method::

    >>> 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.
    é   ©Údefaultú+create_using must indicate a Directed Graphc                 ó   — | S ©N© )Úxs    úZ/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/generators/directed.pyÚkernelzgn_graph.<locals>.kernelG   s   € ØˆHó    r   é   )ÚdistributionÚseed)	r   ÚnxÚDiGraphÚis_directedÚNetworkXErrorÚadd_edgeÚranger   Úappend)
Únr   Úcreate_usingr   ÚGÚdsÚsourceÚdÚdistÚtargets
             r   r   r      sÒ   € ôT 	�A�|¬R¯Z©ZÔ8€AØ�=‰=Œ?Ü×ÑÐLÓMÐMà€~ò	ð 	ˆA‚vØˆà‡J�Jˆq�!ÔØ
ˆQˆ€Bä˜˜1“+ò ˆà#%Ö&˜a‘�q•	Ð&ˆÐ&ä" 1°4¸dÔCÀAÑFˆØ	�
‰
�6˜6Ô"Ø
�	‰	�!ŒØ
ˆ6‹
�a‰Œ
ðð €Hùò 's   Á7Cc                 ód  — t        d|t        j                  ¬«      }|j                  «       st        j                  d«      ‚| dk(  r|S t        d| «      D ]X  }|j                  d|«      }|j                  «       |k  r|dk7  rt        |j                  |«      «      }|j                  ||«       ŒZ |S )aî  Returns 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 probability `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 : NetworkX graph constructor, optional (default DiGraph)
        Graph type to create. If graph instance, then cleared before populated.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    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.
    r   r   r   r   )r   r   r    r!   r"   r$   Ú	randrangeÚrandomÚnextÚ
successorsr#   )r&   Úpr'   r   r(   r*   r-   s          r   r
   r
   [   sŸ   € ôN 	�A�|¬R¯Z©ZÔ8€AØ�=‰=Œ?Ü×ÑÐLÓMÐMàˆA‚vØˆä˜˜1“+ò #ˆØ—‘  6Ó*ˆØ�;‰;‹=˜1Ò ¨1¢Ü˜!Ÿ,™, vÓ.Ó/ˆFØ	�
‰
�6˜6Õ"ð	#ð
 €Hr   r   c                 óP  — t        d|t        j                  ¬«      }|j                  «       st        j                  d«      ‚| dk(  r|S t        d| «      D ]N  }|j                  d|«      }|j                  |«      D ]  }|j                  ||«       Œ |j                  ||«       ŒP |S )a$  Returns 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 : NetworkX graph constructor, optional (default DiGraph)
        Graph type to create. If graph instance, then cleared before populated.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    References
    ----------
    .. [1] P. L. Krapivsky and S. Redner,
           Network Growth by Copying,
           Phys. Rev. E, 71, 036118, 2005k.},
    r   r   r   r   )	r   r   r    r!   r"   r$   r/   r2   r#   )r&   r'   r   r(   r*   r-   Úsuccs          r   r	   r	   ‘   s�   € ô2 	�A�|¬R¯Z©ZÔ8€AØ�=‰=Œ?Ü×ÑÐLÓMÐMàˆA‚vØˆä˜˜1“+ò #ˆØ—‘  6Ó*ˆØ—L‘L Ó(ò 	%ˆDØ�J‰J�v˜tÕ$ð	%à	�
‰
�6˜6Õ"ð	#ð
 €Hr   é   c                 ó”  ‡— ˆfd„}|�>t        |d«      r2t        |t        j                  «      st        j                  d«      ‚|}	nt        j                  g d¢«      }	|dk  rt        d«      ‚|dk  rt        d«      ‚|dk  rt        d«      ‚t        ||z   |z   d	z
  «      d
k\  rt        d«      ‚|dk  rt        d«      ‚|dk  rt        d«      ‚t        d„ |	j                  «       D «       g «      }
t        d„ |	j                  «       D «       g «      }t        |	j                  «       «      }|D � cg c]  } t        | t        j                  «      sŒ| ‘Œ! }} t        |«      dkD  rt        d„ |D «       «      dz   }nd}t        |	«       k  rº‰j!                  «       }||k  r#|}|dz  }|j#                  |«        ||||«      }n?|||z   k  r ||
||«      } ||||«      }n" ||
||«      }|}|dz  }|j#                  |«       |	j%                  ||«       |
j#                  |«       |j#                  |«       t        |	«      | k  rŒº|	S c c} w )uc  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 nodes from in-degree distribution.
    delta_out : float
        Bias for choosing nodes from out-degree distribution.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    initial_graph : MultiDiGraph instance, optional
        Build the scale-free graph starting from this initial MultiDiGraph,
        if provided.

    Returns
    -------
    MultiDiGraph

    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.
    c                 ó¶   •— |dkD  rCt        |«      |z  }||t        | «      z   z  }‰j                  «       |k  r‰j                  |«      S ‰j                  | «      S )Nr   )Úlenr0   Úchoice)Ú
candidatesÚ	node_listÚdeltaÚbias_sumÚp_deltar   s        €r   Ú_choose_nodez&scale_free_graph.<locals>._choose_node÷   sW   ø€ Ø�1Š9Ü˜9“~¨Ñ-ˆHØ (¬S°«_Ñ"<Ñ=ˆGØ�{‰{‹}˜wÒ&Ø—{‘{ 9Ó-Ð-Ø�{‰{˜:Ó&Ð&r   Ú_adjz%initial_graph must be a MultiDiGraph.))r   r   )r   r   )r   r   r   zalpha must be > 0.zbeta must be > 0.zgamma must be > 0.g      ð?g•Ö&è.>zalpha+beta+gamma must equal 1.zdelta_in must be >= 0.zdelta_out must be >= 0.c              3   ó.   K  — | ]  \  }}||gz  –— Œ y ­wr   r   ©Ú.0ÚidxÚcounts      r   ú	<genexpr>z#scale_free_graph.<locals>.<genexpr>  s   è ø€ Ò=¡
  Uˆe�s�e�mÑ=ùó   ‚c              3   ó.   K  — | ]  \  }}||gz  –— Œ y ­wr   r   rC   s      r   rG   z#scale_free_graph.<locals>.<genexpr>  s   è ø€ Ò<¡
  Uˆe�s�e�mÑ<ùrH   c              3   óF   K  — | ]  }t        |j                  «      –— Œ y ­wr   )ÚintÚreal)rD   r&   s     r   rG   z#scale_free_graph.<locals>.<genexpr>"  s   è ø€ Ò8 Q”S˜Ÿ™—[Ñ8ùs   ‚!r   )ÚhasattrÚ
isinstancer   ÚMultiDiGraphr"   Ú
ValueErrorÚabsÚsumÚ
out_degreeÚ	in_degreeÚlistÚnodesÚnumbersÚNumberr9   Úmaxr0   r%   r#   )r&   ÚalphaÚbetaÚgammaÚdelta_inÚ	delta_outr   Úinitial_graphr@   r(   ÚvsÚwsr<   Únumeric_nodesÚcursorÚrÚvÚws         `           r   r   r   ¹   sJ  ø€ ô|'ð Ð ¤W¨]¸FÔ%CÜ˜-¬¯©Ô9Ü×"Ñ"Ð#JÓKÐKØ‰ô �O‰OÒ4Ó5ˆà�‚zÜÐ-Ó.Ð.Øˆq‚yÜÐ,Ó-Ð-Ø�‚zÜÐ-Ó.Ð.ä
ˆ5�4‰<˜%Ñ #Ñ%Ó&¨$Ò.ÜÐ9Ó:Ð:à�!‚|ÜÐ1Ó2Ð2à�1‚}ÜÐ2Ó3Ð3ô 
Ñ=¨a¯l©l«nÔ=¸rÓ	B€BÜ	Ñ<¨a¯k©k«mÔ<¸bÓ	A€Bô �Q—W‘W“Y“€Ið !*ÖK˜1¬Z¸¼7¿>¹>Õ-J’QÐK€MÐKÜ
ˆ=Ó˜AÒäÑ8¨-Ô8Ó8¸1Ñ<‰ð ˆä
ˆa‹&�1Š*Ø�K‰K‹Mˆð ˆuŠ9ð ˆAØ�a‰KˆFà×Ñ˜QÔá˜R ¨HÓ5‰Aà�˜‘Òñ ˜R ¨IÓ6ˆAá˜R ¨HÓ5‰Añ
 ˜R ¨IÓ6ˆAàˆAØ�a‰KˆFà×Ñ˜QÔð 	
�
‰
�1�aÔð 	�	‰	�!ŒØ
�	‰	�!ŒôI ˆa‹&�1‹*ðL €Hùò] Ls   Ä/IÅIé   c                 ó  ‡‡‡‡	— |rt        j                  «       }ˆˆˆfd„}nt        j                  «       }ˆˆˆfd„}t        j                  | |«      }t	        |«      }|D ]#  Š	|j                  ˆ	fd„ |‰	|«      D «       «       Œ% |S )a_  Returns 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 : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    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                 ó@   •‡— ‰s‰| hz
  Šˆˆfd„t        ‰«      D «       S )Nc              3   óR   •K  — | ]  }‰j                  t        ‰«      «      –— Œ  y ­wr   )r:   rU   )rD   ÚirV   r   s     €€r   rG   z=random_uniform_k_out_graph.<locals>.sample.<locals>.<genexpr>�  s   øè ø€ Ò?°�D—K‘K¤ U£×,Ñ?ùs   ƒ$')r$   ©re   rV   Úkr   Ú
self_loopss    `€€€r   Úsamplez*random_uniform_k_out_graph.<locals>.sample�  s    ù€ ÙØ  ™�Ü?´e¸A³hÔ?Ð?r   c                 óJ   •— ‰s|| hz
  }‰j                  t        |«      ‰«      S r   )ro   rU   rl   s     €€€r   ro   z*random_uniform_k_out_graph.<locals>.sample•  s&   ø€ ÙØ  ™�Ø—;‘;œt E›{¨AÓ.Ð.r   c              3   ó&   •K  — | ]  }‰|f–— Œ
 y ­wr   r   )rD   re   Úus     €r   rG   z-random_uniform_k_out_graph.<locals>.<genexpr>�  s   øè ø€ Ò: A˜!˜QœÑ:ùs   ƒ)r   rO   r    r   ÚsetÚadd_edges_from)
r&   rm   rn   Úwith_replacementr   r'   ro   r(   rV   rr   s
    `` `    @r   Úrandom_uniform_k_out_graphrv   P  sw   û€ ñt Ü—‘Ó(ˆ÷	@ô —z‘z“|ˆö	/ô
 	�‰�q˜,Ó'€AÜ�‹F€EØò ;ˆØ	×ÑÓ:©°°5Ó)9Ô:Õ:ð;à€Hr   c           	      óè  — |dk  rt        d«      ‚t        j                  | t        j                  ¬«      }t	        |D �ci c]  }||“Œ c}«      }t        || z  «      D ]†  }|j                  |j                  «       D ��	cg c]  \  }}	|	|k  sŒ|‘Œ c}	}«      }
|st	        |
||
   i«      }n
t	        «       }t        ||z
  |¬«      }|j                  |
|«       ||xx   dz  cc<   Œˆ |S c c}w c c}	}w )aK  Returns a random `k`-out graph with preferential attachment.

    A random `k`-out graph with preferential attachment is a
    multidigraph generated by the following algorithm.

    1. Begin with an empty digraph, and initially set each node to have
       weight `alpha`.
    2. Choose a node `u` with out-degree less than `k` uniformly at
       random.
    3. Choose a node `v` from with probability proportional to its
       weight.
    4. Add a directed edge from `u` to `v`, and increase the weight
       of `v` by one.
    5. If each node has out-degree `k`, halt, otherwise repeat from
       step 2.

    For more information on this model of random graph, see [1].

    Parameters
    ----------
    n : int
        The number of nodes in the returned graph.

    k : int
        The out-degree of each node in the returned graph.

    alpha : float
        A positive :class:`float` representing the initial weight of
        each vertex. A higher number means that in step 3 above, nodes
        will be chosen more like a true uniformly random sample, and a
        lower number means that nodes are more likely to be chosen as
        their in-degree increases. If this parameter is not positive, a
        :exc:`ValueError` is raised.

    self_loops : bool
        If True, self-loops are allowed when generating the graph.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    :class:`~networkx.classes.MultiDiGraph`
        A `k`-out-regular multidigraph generated according to the above
        algorithm.

    Raises
    ------
    ValueError
        If `alpha` is not positive.

    Notes
    -----
    The returned multidigraph may not be strongly connected, or even
    weakly connected.

    References
    ----------
    [1]: Peterson, Nicholas R., and Boris Pittel.
         "Distance between two random `k`-out digraphs, with and without
         preferential attachment."
         arXiv preprint arXiv:1311.5961 (2013).
         <https://arxiv.org/abs/1311.5961>

    r   zalpha must be positive)r'   )r   r   )
rP   r   r   rO   r   r$   r:   rS   r   r#   )r&   rm   rZ   rn   r   r(   re   Úweightsrk   r+   rr   Ú
adjustments               r   r   r   ¡  sÞ   € ðJ ˆq‚yÜÐ1Ó2Ð2Ü
�‰�q¤r§¡Ô7€AÜ¨Ö+ A�q˜%‘xÒ+Ó,€GÜ�1�q‘5‹\ò 
ˆØ�K‰K q§|¡|£~×?™t˜q !¸¸Q»šÓ?Ó@ˆñ Ü  ! W¨Q¡Z Ó1‰Jä ›ˆJÜ˜G jÑ0°tÔ<ˆØ	�
‰
�1�aÔØ�‹
�a‰Œ
ð
ð €Hùò ,ùã?s   ¿
C)Á?C.ÂC.)NNN)NN)g=
×£p=Ú?gHáz®Gá?gš™™™™™©?gš™™™™™É?r   NN)TTN)TN)Ú__doc__rW   Úcollectionsr   Únetworkxr   Únetworkx.generators.classicr   Únetworkx.utilsr   r   r   Ú__all__Ú_dispatchabler   r
   r	   r   rv   r   r   r   r   ú<module>r�      sg  ðñó Ý ã Ý 3ß NÑ Nò€ñ �ÓØ€×Ñ˜¨TÔ2ò?ó 3ó ð?ñD �ÓØ€×Ñ˜¨TÔ2ò1ó 3ó ð1ñh �ÓØ€×Ñ˜¨TÔ2ò#ó 3ó ð#ñL �ÓØ€×Ñ˜¨TÔ2ð Ø	Ø
ØØØ	ØòRó 3ó ðRñj �ÓØ€×Ñ˜¨TÔ2òLó 3ó ðLñ^ �ÓØ€×Ñ˜¨TÔ2òRó 3ó ñRr   