
    \MhF                        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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j        dd	          dd            Z e
d          dd            Z e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np_random_state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                  }|                                st          j        d          d | dk    r|S |                    dd           ddg}t          d|           D ]c}fd|D             }t          d||	          d         }|                    ||           |                    d           ||xx         dz  cc<   d|S )
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 GraphNc                     | S N )xs    \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/generators/directed.pykernelzgn_graph.<locals>.kernelL   s    H    r      c                 &    g | ]} |          S r   r   ).0dr   s     r   
<listcomp>zgn_graph.<locals>.<listcomp>W   s!    &&&aq		&&&r   )distributionseed)	r   nxDiGraphis_directedNetworkXErroradd_edgeranger   append)	nr   create_usingr#   Gdssourcedisttargets	    `       r   r	   r	      s   T 	A|RZ888A==?? NLMMM~	 	 	 	AvvJJq!
QB1++  &&&&2&&&"14dCCCAF	

66"""
		!
6


a



Hr   c                    t          d|t          j                  }|                                st          j        d          | dk    r|S t          d|           D ]n}|                    d|          }|                                |k     r(|dk    r"t          |	                    |                    }|
                    ||           o|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/   r1   s          r   r   r   `   s    N 	A|RZ888A==?? NLMMMAvv1++ # #6**;;==11!,,v..//F	

66""""Hr   r   c                 x   t          d|t          j                  }|                                st          j        d          | dk    r|S t          d|           D ]\}|                    d|          }|                    |          D ]}|                    ||           |                    ||           ]|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)   r3   r6   r(   )r+   r,   r#   r-   r/   r1   succs          r   r
   r
      s    2 	A|RZ888A==?? NLMMMAvv1++ # #6**LL(( 	% 	%DJJvt$$$$	

66""""Hr      =
ףp=?HzG?皙?皙?c                 H   fd}|At          |d          r1t          |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 |	                                D             g           }
t          d |		                                D             g           }t          |	                                          }d |D             }t          |          dk    rt          d |D                       dz   }nd}t          |	          | k     r                                }||k     r*|}|dz  }|                    |            ||||          }nM|||z   k     r ||
||          } ||||          }n) ||
||          }|}|dz  }|                    |           |	                    ||           |
                    |           |                    |           t          |	          | k     |	S )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k    rTt          |          |z  }||t          |           z   z  }                                |k     r                    |          S                     |           S )Nr   )lenr4   choice)
candidates	node_listdeltabias_sump_deltar#   s        r   _choose_nodez&scale_free_graph.<locals>._choose_node   sh    1999~~-H(S__"<=G{{}}w&&{{9---{{:&&&r   N_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  V  d S r   r   r   idxcounts      r   	<genexpr>z#scale_free_graph.<locals>.<genexpr>  s,      ==
Uesem======r   c              3   (   K   | ]\  }}||gz  V  d S r   r   rK   s      r   rN   z#scale_free_graph.<locals>.<genexpr>  s,      <<
Uesem<<<<<<r   c                 F    g | ]}t          |t          j                  |S r   )
isinstancenumbersNumberr   r+   s     r   r!   z$scale_free_graph.<locals>.<listcomp>$  s)    KKK1Z7>-J-JKQKKKr   c              3   >   K   | ]}t          |j                  V  d S r   )intrealrT   s     r   rN   z#scale_free_graph.<locals>.<genexpr>'  s*      88QS[[888888r   r   )hasattrrQ   r$   MultiDiGraphr'   
ValueErrorabssum
out_degree	in_degreelistnodesrA   maxr4   r*   r(   )r+   alphabetagammadelta_in	delta_outr#   initial_graphrH   r-   vswsrD   numeric_nodescursorrvws         `           r   r   r      s   |' ' ' ' '  W]F%C%C -99 	L"#JKKK O44455zz-...qyy,---zz-...
54<%#%&&$..9:::!||12221}}2333 
==allnn===r	B	BB	<<akkmm<<<b	A	AB QWWYYI LK	KKKM
=A88-888881< 
a&&1**KKMM u99 AaKFQRH55AA RI66ARH55AA
 RI66AAaKFQ 	


1a 			!
		!I a&&1**L Hr      c                 $  	 |rt          j                    }fd}nt          j                    }fd}t          j        | |          }t	          |          }|D ]-	|                    	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                 N    s| hz
  fdt                    D             S )Nc              3   \   K   | ]&}                     t                              V  'd S r   )rB   r_   )r   ir`   r#   s     r   rN   z=random_uniform_k_out_graph.<locals>.sample.<locals>.<genexpr>  s5      ??DKKU,,??????r   )r)   rm   r`   kr#   
self_loopss    `r   samplez*random_uniform_k_out_graph.<locals>.sample  s:     $?????eAhh????r   c                 Z    s|| hz
  }                     t          |                    S r   )rw   r_   rt   s     r   rw   z*random_uniform_k_out_graph.<locals>.sample  s0     $;;tE{{A...r   c              3       K   | ]}|fV  	d S r   r   )r   rm   us     r   rN   z-random_uniform_k_out_graph.<locals>.<genexpr>  s'      ::A!Q::::::r   )r$   rY   r%   r   setadd_edges_from)
r+   ru   rv   with_replacementr#   r,   rw   r-   r`   rz   s
    `` `    @r   random_uniform_k_out_graphr~   U  s    t  /((	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ z||	/ 	/ 	/ 	/ 	/ 	/ 	/
 	q,''AFFE ; ;	::::5)9)9:::::::Hr   c                     |dk     rt          d          	 t          | ||||          S # t          $ r t          | ||||          cY S w xY w)ac  Returns a random `k`-out graph with preferential attachment.

    .. versionchanged:: 3.5
       Different implementations will be used based on whether NumPy is
       available. See Notes for details.

    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.

    `random_k_out_graph` has two implementations: an array-based formulation that
    uses `numpy` (``_random_k_out_graph_numpy``), and a pure-Python
    implementation (``_random_k_out_graph_python``).
    The NumPy implementation is more performant, especially for large `n`, and is
    therefore used by default. If NumPy is not installed in the environment,
    then the pure Python implementation is executed.
    However, you can explicitly control which implementation is executed by directly
    calling the corresponding function::

        # Use numpy if available, else Python
        nx.random_k_out_graph(1000, 5, alpha=1)

        # Use the numpy-based implementation (raises ImportError if numpy not installed)
        nx.generators.directed._random_k_out_graph_numpy(1000, 5, alpha=1)

        # Use the Python-based implementation
        nx.generators.directed._random_k_out_graph_python(1000, 5, alpha=1)

    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)rZ   _random_k_out_graph_numpyImportError_random_k_out_graph_python)r+   ru   rb   rv   r#   s        r   r   r     st    p qyy1222I(Auj$GGG I I I)!Qz4HHHHHIs   * A
	A
c                    dd l }t          j        | t          j                  }|                    |           }|                    | d          }|                    | |          }	| |z  }
|                    |           }t          || z            D ]}|                    ||                   }|r|                    ||	|
z            }n/|	|         }d|	|<   |                    ||	|
|z
  z            }||	|<   |	                    ||           |	|xx         dz  cc<   |
dz  }
||xx         dz  cc<   ||         |k    rd||<   |S )Nr   r,   T)r7   r   F)
numpyr$   r   rY   arangefullzerosr)   rB   r(   )r+   ru   rb   rv   r#   npr-   r`   remaining_maskweightstotal_weightout_strengthsrs   rz   rm   u_weights                   r   r   r     sm   
qr777AIIaLLEWWQ%%NggaGu9LHHQKKM1q5\\ & &KKn-.. 	"EW|%;<<AAqzHGAJEWx0G%HIIA!GAJ	

1a


a


aAq   %N1Hr   c                    t          j        | t           j                  }t          t                              ||                    }t          t                              |d                    }t          || z            D ]}|                    t          |	                                                    }	|s|
                    |	          }
t          ||          }|s|
||	<   |                    |	|           ||xx         dz  cc<   ||	xx         dz  cc<   ||	         |k    r|
                    |	           |S )Nr   r   )r#   r   )r$   r   rY   r   dictfromkeysr)   rB   r_   keyspopr   r(   )r+   ru   rb   rv   r#   r-   r   r   rs   rz   uweightrm   s               r   r   r   %  s7   
qr777AdmmAu--..GDMM!Q//00M1q5\\ ! !KK]//112233  	%kk!nnGG$/// 	! GAJ	

1a


a


aAq  a   Hr   )NNN)NN)r;   r<   r=   r>   r   NN)TTN)TN)__doc__rR   collectionsr   networkxr$   networkx.generators.classicr   networkx.utilsr   r   r   r   __all___dispatchabler	   r   r
   r   r~   r   r   r   r   r   r   <module>r      s               3 3 3 3 3 3              T222? ? ? 32 ?D T2221 1 1 32 1h T222# # # 32 #L T222 	
	R R R 32 Rj T222L L L 32 L^ T222\I \I \I 32\I~    <      r   