o
    ˷e$A                     @   s   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ddd	Ze
ddd
dZe
ddddZe
d								dddZe
ddddZe
dd ddZdS )!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   c           	         s   t d|tjd}| std du rdd  | dkr|S |dd ddg}td| D ]'} fd	d
|D }td||dd }||| |d ||  d7  < q.|S )aD  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 GraphNc                 S   s   | S N )xr   r   S/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/generators/directed.pykernelF   s   zgn_graph.<locals>.kernelr      c                    s   g | ]} |qS r   r   ).0dr   r   r   
<listcomp>Q   s    zgn_graph.<locals>.<listcomp>)distributionseed)	r   nxDiGraphis_directedNetworkXErroradd_edgeranger   append)	nr   create_usingr   Gdssourcedisttargetr   r   r   r      s    )

r   c                 C   s|   t d|tjd}| std| dkr|S td| D ]}|d|}| |k r5|dkr5t|	|}|
|| q|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 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 : 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*   r   r   r   r	   Z   s   &
r	   r   c                 C   sv   t d|tjd}| std| dkr|S td| D ]}|d|}||D ]}||| q)||| q|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+   r.   r!   )r$   r%   r   r&   r(   r*   succr   r   r   r      s   
r      =
ףp=?HzG?皙?皙?c	                    s0   fdd}	|durddl }
|
jdtdd |dur*t|dr*|dur'td	|}n|dur6t|dr6|}ntg d
}| rE| sJt	d|dkrRtd|dkrZtd|dkrbtdt
|| | d dkrrtd|dk rztd|dk rtdtdd | D g }tdd | D g }t| }dd |D }t|dkrtdd |D d }nd}t|| k r  }||k r|}|d7 }|| |	|||}n$||| k r|	|||}|	|||}n|	|||}|}|d7 }|| ||| || || t|| k s|S )u  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.
    create_using : NetworkX graph constructor, optional
        The default is a MultiDiGraph 3-cycle.
        If a graph instance, use it without clearing first.
        If a graph constructor, call it to construct an empty graph.

        .. deprecated:: 3.0

           create_using is deprecated, use `initial_graph` instead.

    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                    sD   |dkrt || }||t |   }  |k r |S  | S )Nr   )lenr,   choice)
candidates	node_listdeltabias_sump_deltar   r   r   _choose_node   s   

z&scale_free_graph.<locals>._choose_nodeNr   zThe create_using argument is deprecated and will be removed in the future.

To create a scale free graph from an existing MultiDiGraph, use
initial_graph instead.r   )
stacklevel_adjzFCannot set both create_using and initial_graph. Set create_using=None.))r   r   )r   r   )r   r   z&MultiDiGraph required in initial_graphz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                 s       | ]
\}}||g V  qd S r   r   r   idxcountr   r   r   	<genexpr>7      z#scale_free_graph.<locals>.<genexpr>c                 s   rA   r   r   rB   r   r   r   rE   8  rF   c                 S   s   g | ]
}t |tjr|qS r   )
isinstancenumbersNumberr   r$   r   r   r   r   >      z$scale_free_graph.<locals>.<listcomp>c                 s   s    | ]}t |jV  qd S r   )intrealrJ   r   r   r   rE   A  s    r   )warningswarnDeprecationWarninghasattr
ValueErrorr   MultiDiGraphr   is_multigraphr    abssum
out_degree	in_degreelistnodesr6   maxr,   r#   r!   )r$   alphabetagammadelta_in	delta_outr%   r   initial_graphr>   rN   r&   vswsr9   numeric_nodescursorrvwr   r=   r   r      sr   H




&r      Tc           	         sv   |rt  } fdd}nt  } fdd}t | |}t|}|D ]|fdd||D  q'|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&   s | h   fddt D S )Nc                 3   s    | ]
} t V  qd S r   )r7   rY   )r   i)rZ   r   r   r   rE     rF   z=random_uniform_k_out_graph.<locals>.sample.<locals>.<genexpr>)r"   rg   rZ   kr   
self_loops)rZ   r   sample  s   
z*random_uniform_k_out_graph.<locals>.samplec                    s   s|| h } t| S r   )ro   rY   rk   rl   r   r   ro     s   
c                 3   s    | ]} |fV  qd S r   r   r   rg   )ur   r   rE     s    z-random_uniform_k_out_graph.<locals>.<genexpr>)r   rS   r   r   setadd_edges_from)	r$   rm   rn   with_replacementr   r%   ro   r&   rZ   r   )rm   r   rn   rq   r   random_uniform_k_out_grapho  s   9 ru   c                    s    dk rt dtj| tjd}t fdd|D }t|  D ]4}|fdd| D }|s<t||| i}	nt }	t||	 |d}
|	||
 ||
  d	7  < q!|S )
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%   c                    s   i | ]}| qS r   r   rp   )r\   r   r   
<dictcomp>  s    z&random_k_out_graph.<locals>.<dictcomp>c                    s   g | ]
\}}| k r|qS r   r   )r   rg   r   )rm   r   r   r     rK   z&random_k_out_graph.<locals>.<listcomp>r=   r   )
rR   r   r   rS   r   r"   r7   rW   r   r!   )r$   rm   r\   rn   r   r&   weightsrj   rq   
adjustmentrg   r   )r\   rm   r   r
     s   Dr
   )NNN)NN)r2   r3   r4   r5   r   NNN)TTN)TN)__doc__rH   collectionsr   networkxr   networkx.generators.classicr   networkx.utilsr   r   r   __all__r   r	   r   r   ru   r
   r   r   r   r   <module>   s8    	B4& 9O