
    shF                     8   S r SSKrSSKJr  SSKrSSKJr  SSKJ	r	J
r
JrJr  / SQr\" S5      \R                  " SSS	9SS
 j5       5       r\" S5      \R                  " SSS	9SS j5       5       r\" S5      \R                  " SSS	9SS j5       5       r\" S5      \R                  " SSS	9       SS j5       5       r\" S5      \R                  " SSS	9SS j5       5       r\R                  " SSS	9SS j5       r\
" S5      SS j5       r\" S5      SS j5       rg)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                    [        SU[        R                  S9nUR                  5       (       d  [        R                  " S5      eUc  S nU S:X  a  U$ UR                  SS5        SS/n[        SU 5       HU  nU Vs/ s H
  oq" U5      PM     nn[        SXS9S   n	UR                  Xi5        UR                  S5        XY==   S-  ss'   MW     U$ s  snf )a  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                     U $ N )xs    p/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/networkx/generators/directed.pykernelgn_graph.<locals>.kernelL   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|RZZ8A==??LMM~	 	AvJJq!
QB1+#%&2aq	2&"14CAF	

6"
		!

a
  H 's   <Cc                 r   [        SU[        R                  S9nUR                  5       (       d  [        R                  " S5      eU S:X  a  U$ [        SU 5       HZ  nUR                  SU5      nUR                  5       U:  a   US:w  a  [        UR                  U5      5      nUR                  XV5        M\     U$ )az  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|RZZ8A==??LMMAv1+6*;;=11!,,v./F	

6"	 
 Hr   r   c                 \   [        SU[        R                  S9nUR                  5       (       d  [        R                  " S5      eU S:X  a  U$ [        SU 5       HO  nUR                  SU5      nUR                  U5       H  nUR                  XF5        M     UR                  XE5        MQ     U$ )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&   r1   r4   r%   )r(   r)   r    r*   r,   r/   succs          r   r
   r
      s    2 	A|RZZ8A==??LMMAv1+6*LL(DJJv$ )	

6"	 
 Hr      c                   ^ U4S jnUbI  [        US5      (       a8  [        U[        R                  5      (       d  [        R                  " S5      eUn	O[        R                  " / SQ5      n	US::  a  [        S5      eUS::  a  [        S5      eUS::  a  [        S5      e[        X-   U-   S	-
  5      S
:  a  [        S5      eUS:  a  [        S5      eUS:  a  [        S5      e[        S U	R                  5        5       / 5      n
[        S U	R                  5        5       / 5      n[        U	R                  5       5      nU V s/ s H&  n [        U [        R                  5      (       d  M$  U PM(     nn [        U5      S:  a  [        S U 5       5      S-   nOSn[        U	5      W :  a  TR!                  5       nX:  a"  UnUS-  nUR#                  U5        U" XU5      nO<XU-   :  a  U" XU5      nU" XU5      nO!U" XU5      nUnUS-  nUR#                  U5        U	R%                  UU5        U
R#                  U5        UR#                  U5        [        U	5      U :  a  M  U	$ s  sn f )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.
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                    > US:  aC  [        U5      U-  nX3[        U 5      -   -  nTR                  5       U:  a  TR                  U5      $ TR                  U 5      $ )Nr   )lenr2   choice)
candidates	node_listdeltabias_sump_deltar    s        r   _choose_node&scale_free_graph.<locals>._choose_node   sU    19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   0   #    U  H  u  pX!/-  v   M     g 7fr   r   .0idxcounts      r   	<genexpr>#scale_free_graph.<locals>.<genexpr>  s     =n
eemn   c              3   0   #    U  H  u  pX!/-  v   M     g 7fr   r   rF   s      r   rJ   rK     s     <m
eemmrL   c              3   L   #    U  H  n[        UR                  5      v   M     g 7fr   )intreal)rG   r(   s     r   rJ   rK   '  s     8-QS[[-s   "$r   )hasattr
isinstancer!   MultiDiGraphr$   
ValueErrorabssum
out_degree	in_degreelistnodesnumbersNumberr;   maxr2   r'   r%   )r(   alphabetagammadelta_in	delta_outr    initial_graphrB   r*   vswsr>   numeric_nodescursorrvws         `           r   r   r      sC   |'  W]F%C%C-99""#JKK OO45z-..qy,--z-..
5<%#%&$.9::!|1221}233 
=alln=r	BB	<akkm<b	AB QWWYI !*K	1Z7>>-JQ	MK
=A8-881< 
a&1*KKM 9 AaKFQRH5A RI6ARH5A
 RI6AAaKFQ 	


1a 			!
		!I a&1*L H] Ls   #I!)I!   c                 *  ^^^^	 U(       a  [         R                  " 5       nUUU4S jnO[         R                  " 5       nUUU4S jn[         R                  " X5      n[	        U5      nU H%  m	UR                  U	4S jU" T	U5       5       5        M'     U$ )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                 L   >^ T(       d  TU 1-
  mUU4S j[        T5       5       $ )Nc              3   X   >#    U  H  nTR                  [        T5      5      v   M!     g 7fr   )r<   rY   )rG   irZ   r    s     r   rJ   =random_uniform_k_out_graph.<locals>.sample.<locals>.<genexpr>  s!     ?hDKKU,,hs   '*)r&   ri   rZ   kr    
self_loopss    `r   sample*random_uniform_k_out_graph.<locals>.sample  s     ?eAh??r   c                 R   > T(       d  X1-
  nTR                  [        U5      T5      $ r   )rt   rY   rq   s     r   rt   ru     s$    ;;tE{A..r   c              3   ,   >#    U  H	  nTU4v   M     g 7fr   r   )rG   ri   us     r   rJ   -random_uniform_k_out_graph.<locals>.<genexpr>  s     :)9A!Q)9s   )r!   rS   r"   r   setadd_edges_from)
r(   rr   rs   with_replacementr    r)   rt   r*   rZ   rx   s
    `` `    @r   random_uniform_k_out_graphr}   U  sv    t (	@ 	@ zz|	/
 	q'AFE	:5)9:: Hr   c                 z    US:  a  [        S5      e [        XX#U5      $ ! [         a    [        XX#U5      s $ f = f)aW  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)rT   _random_k_out_graph_numpyImportError_random_k_out_graph_python)r(   rr   r^   rs   r    s        r   r   r     sM    p qy122I(u$GG I)!4HHIs     ::c                     SS K n[        R                  " U [        R                  S9nUR	                  U 5      nUR                  U S5      nUR                  X5      n	X-  n
UR                  U 5      n[        X-  5       H  nUR                  Xx   5      nU(       a  UR                  XyU
-  S9nO X   nSX'   UR                  XyX-
  -  S9nXU'   UR                  X5        X==   S-  ss'   U
S-  n
X==   S-  ss'   X   U:X  d  M  SX'   M     U$ )Nr   r)   T)r5   r   F)
numpyr!   r   rS   arangefullzerosr&   r<   r%   )r(   rr   r^   rs   r    npr*   rZ   remaining_maskweightstotal_weightout_strengthsro   rx   ri   u_weights                   r   r   r     s   
qr7AIIaLEWWQ%NggaG9LHHQKM15\KK-.E|%;<AzHGJE0G%HIA!AJ	

1
a
Aq  %N! " Hr   c                    [         R                  " U [         R                  S9n[        [        R                  XR5      5      n[        [        R                  US5      5      n[        X-  5       H  nUR                  [        UR                  5       5      5      n	U(       d  UR                  U	5      n
[        XdS9nU(       d  W
Xi'   UR                  X5        Xk==   S-  ss'   Xy==   S-  ss'   Xy   U:X  d  M  UR                  U	5        M     U$ )Nr   r   )r    r   )r!   r   rS   r   dictfromkeysr&   r<   rY   keyspopr   r%   )r(   rr   r^   rs   r    r*   r   r   ro   rx   uweightri   s               r   r   r   %  s    
qr7AdmmA-.GDMM!Q/0M15\KK]//123 kk!nGG/ GJ	

1
a
Aq a ! " Hr   )NNN)NN)g=
ףp=?gHzG?g?g?r   NN)TTN)TN)__doc__r[   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  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\I 3\I~  <  r   