
    Dii                        d Z ddlZddlmZ g dZ ed          dd            Zd Z ed          dd
            Zd Z	 ed          dd            Z
 ed          dd            Z ed	          dd            ZdS )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dt          |           D             S )zK
    Return sample sequence of length n from a power law distribution.
    c                 @    g | ]}                     d z
            S    )paretovariate).0iexponentseeds     d/var/www/html/bet.cuttalo.com/ml/venv/lib/python3.11/site-packages/networkx/utils/random_sequence.py
<listcomp>z%powerlaw_sequence.<locals>.<listcomp>   s+    ???Dx!|,,???    )range)nr   r   s    ``r   r   r      s*    
 @????eAhh????r   c                     t          |           }t          |          }t          |          }d|z  |z
  dk    rdS |dgk    rt          d |D                       rdS dS )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   "   K   | ]
}|d k    V  dS )r   N )r   ds     r   	<genexpr>z0is_valid_tree_degree_sequence.<locals>.<genexpr><   s&      00qAF000000r   )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   !   su    * 

C#hhOHH?22a77II	00C00000PP8r   r   c                 B   |dk     rt          d          | dk    rt          d          | dz
  }d|z  }	 d|                                z
  }|                                }t          ||d|z   z  z            }dd|z  z   |z  }||z  |dz
  z  |dz
  z  ||z  k    rni|S )aw  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 axx$$$zz$$$	B	2A$++--KKMMqcBhK''((C!G_#q5AGC(AE11 Hr   c                 h    dg}d| D ]}|z  |                                fd|D             S )zFReturns normalized cumulative distribution from discrete distribution.g        c                     g | ]}|z  S r   r   )r   element
cumulatives     r   r   z+cumulative_distribution.<locals>.<listcomp>   s    444WGj 444r   )append)distributioncdfr7   r8   s      @r   r   r      sY     %CJ  g


:44444444r      c                     ddl ||n&|t          |          nt          j        d          fdt	          |           D             }fd|D             }|S )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 missingc                 8    g | ]}                                 S r   )r+   )r   r   r   s     r   r   z%discrete_sequence.<locals>.<listcomp>   s!    000!000r   c                 B    g | ]}                     |          d z
  S r   )bisect_left)r   sbisectr;   s     r   r   z%discrete_sequence.<locals>.<listcomp>   s.    
<
<
<a6c1%%)
<
<
<r   )rB   r   nxNetworkXErrorr   )r   r:   cdistributionr   inputseqr&   rB   r;   s      `  @@r   r   r      s     MMM 		!%l33F
 
 	

 1000uQxx000H =
<
<
<
<8
<
<
<CJr   c                    |t          |           k    rt          d          t                      }t          |          |k     r6|                    t	          | |                     t          |          |k     6t          |          S )zReturns 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	      su     	3w<<8999UUF
f++//

?7D11222 f++//<<r   c                     |                                 t          |                                           z  }|                                 D ]\  }}||z  }|dk     r|c S dS )zuReturns a single element from a weighted sample.

    The input is a dictionary of items with weights as values.
    r   N)r+   r#   valuesitems)rJ   r   rndrK   ws        r   r
   r
      sm     ++--#gnn..//
/C  1q77HHH  r   )r   N)r   N)NNN)N)__doc__networkxrC   networkx.utilsr   __all__r   r   r   r   r   r	   r
   r   r   r   <module>rV      sE   
     * * * * * *    @ @ @ @  @ = = = =@5 5 5    > 
 
 
 
 
 
 
 
 
 
r   