
    9i                         S r SSKrSSKJr  / SQr\" S5      SS j5       rS r\" S5      SS	 j5       rS
 r	\" S5      SS j5       r
\" S5      SS j5       r\" S5      SS j5       rg)zS
Utilities for generating random numbers, random sequences, and
random selections.
    N)py_random_state)powerlaw_sequenceis_valid_tree_degree_sequencezipf_rvcumulative_distributiondiscrete_sequencerandom_weighted_sampleweighted_choice   c                 f    [        U 5       Vs/ s H  o2R                  US-
  5      PM     sn$ s  snf )zC
Return sample sequence of length n from a power law distribution.
   )rangeparetovariate)nexponentseedis       ^/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/networkx/utils/random_sequence.pyr   r      s-    
 7<Ah?hx!|,h???s   .c                     [        U 5      n[        U5      n[        U5      nSU-  U-
  S:w  a  gUS/:w  a  [        S U 5       5      (       a  gg)a  Check if a degree sequence is valid for a tree.

Two conditions must be met for a degree sequence to be valid for a tree:

1. The number of nodes must be one more than the number of edges.
2. The degree sequence must be trivial or have only strictly positive
   node degrees.

Parameters
----------
degree_sequence : iterable
    Iterable of node degrees.

Returns
-------
bool
    Whether the degree sequence is valid for a tree.
str
    Reason for invalidity, or dummy string if valid.
r   )Fz1tree must have one more node than number of edgesr   c              3   *   #    U  H	  oS :*  v   M     g7f)r   N ).0ds     r   	<genexpr>0is_valid_tree_degree_sequence.<locals>.<genexpr><   s     0CqFCs   )Fz8nontrivial tree must have strictly positive node degrees)T )listlensumany)degree_sequenceseqnumber_of_nodestwice_number_of_edgess       r   r   r   !   sV    * 
C#hOH?22a7I	0C000P    r   c                    US:  a  [        S5      eU S::  a  [        S5      eU S-
  nSU-  n SUR                  5       -
  nUR                  5       n[        XSU-  * -  -  5      nSSU-  -   U-  nXg-  US-
  -  US-
  -  X-  ::  a   U$ M]  )a  Returns a random value chosen from the Zipf distribution.

The return value is an integer drawn from the probability distribution

.. math::

    p(x)=\frac{x^{-\alpha}}{\zeta(\alpha, x_{\min})},

where $\zeta(\alpha, x_{\min})$ is the Hurwitz zeta function.

Parameters
----------
alpha : float
  Exponent value of the distribution
xmin : int
  Minimum value
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
x : int
  Random value from Zipf distribution

Raises
------
ValueError:
  If xmin < 1 or
  If alpha <= 1

Notes
-----
The rejection algorithm generates random values for a the power-law
distribution in uniformly bounded expected time dependent on
parameters.  See [1]_ for details on its operation.

Examples
--------
>>> nx.utils.zipf_rv(alpha=2, xmin=3, seed=42)
8

References
----------
.. [1] Luc Devroye, Non-Uniform Random Variate Generation,
   Springer-Verlag, New York, 1986.
r   zxmin < 1za <= 1.0g      ?r   )
ValueErrorrandomint)	alphaxminr   a1buvxts	            r   r   r   A   s    b ax$$z$$	B	2A
$++-KKMcBhK''(C!G_#5AGC(AE1H r%   c                 |    S/nSnU  H  nX#-  nUR                  U5        M     U Vs/ s H  o3U-  PM	     sn$ s  snf )zFReturns normalized cumulative distribution from discrete distribution.g        )append)distributioncdf
cumulativeelements       r   r   r      sK     %CJ


:   144Wj 444s   9   c                    SSK nUb  UnO%Ub  [        U5      nO[        R                  " S5      e[	        U 5       Vs/ s H  ocR                  5       PM     nnU Vs/ s H  oR                  XX5      S-
  PM     n	nU	$ s  snf s  snf )a  
Return sample sequence of length n from a given discrete distribution
or discrete cumulative distribution.

One of the following must be specified.

distribution = histogram of values, will be normalized

cdistribution = normalized discrete cumulative distribution

r   Nz8discrete_sequence: distribution or cdistribution missingr   )bisectr   nxNetworkXErrorr   r(   bisect_left)
r   r4   cdistributionr   r:   r5   r   inputseqsr"   s
             r   r   r      s      		!%l3F
 	

 (-Qx0x!xH0 4<
<8ac%)8C
<J	 1 =s   A=Bc                     U[        U 5      :  a  [        S5      e[        5       n[        U5      U:  a+  UR                  [	        X5      5        [        U5      U:  a  M+  [        U5      $ )zxReturns k items without replacement from a weighted sample.

The input is a dictionary of items with weights as values.
zsample larger than population)r   r'   setaddr
   r   )mappingkr   samples       r   r	   r	      sV     	3w<899UF
f+/

?712 f+/<r%   c                     UR                  5       [        U R                  5       5      -  nU R                  5        H  u  p4X$-  nUS:  d  M  Us  $    g)zmReturns a single element from a weighted sample.

The input is a dictionary of items with weights as values.
r   N)r(   r   valuesitems)rD   r   rndrE   ws        r   r
   r
      sE     ++-#gnn./
/C7H  r%   )g       @N)r   N)NNN)N)__doc__networkxr;   networkx.utilsr   __all__r   r   r   r   r   r	   r
   r   r%   r   <module>rP      s   
  *  @ @@ = =@5  > 
 
 
 
r%   