
    ܛ7i                         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y)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                 b    t        |       D cg c]  }|j                  |dz
         c}S c c}w )zK
    Return sample sequence of length n from a power law distribution.
       )rangeparetovariate)nexponentseedis       [/home/rose/Desktop/poly/venv/lib/python3.12/site-packages/networkx/utils/random_sequence.pyr   r      s/    
 7<Ah?hDx!|,h???s   ,c                     t        |       }t        |      }t        |      }d|z  |z
  dk7  ry|dgk7  rt        d |D              ryy)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    yw)r   N ).0ds     r   	<genexpr>z0is_valid_tree_degree_sequence.<locals>.<genexpr><   s     0CqA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   !   sS    * 
C#hOH?22a7I	0C00P    r   c                    |dk  rt        d      | dk  rt        d      | dz
  }d|z  }	 d|j                         z
  }|j                         }t        ||d|z   z  z        }dd|z  z   |z  }||z  |dz
  z  |dz
  z  ||z  k  r	 |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 ax$$z$$	B	2A
$++-KKMqcBhK''(C!G_#q5AGC(AE1H r$   c                 v    dg}d}| D ]  }||z  }|j                  |        |D cg c]  }||z  	 c}S c c}w )zFReturns normalized cumulative distribution from discrete distribution.g        )append)distributioncdf
cumulativeelements       r   r   r      sO     %CJg


:   144WGj 444s   6   c                    ddl }||}n#|t        |      }nt        j                  d      t	        |       D cg c]  }|j                          }}|D cg c]  }|j                  ||      dz
   }	}|	S c c}w c c}w )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   r3   cdistributionr   r9   r4   r   inputseqsr!   s
             r   r   r      s      		!%l3F
 	

 (-Qx0x!xH0 4<
<8a6c1%)8C
<J	 1 =s   A7A<c                     |t        |       kD  rt        d      t               }t        |      |k  r*|j                  t	        | |             t        |      |k  r*t        |      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	      sX     	3w<899UF
f+/

?7D12 f+/<r$   c                     |j                         t        | j                               z  }| j                         D ]  \  }}||z  }|dk  s|c S  y)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)rC   r   rndrD   ws        r   r
   r
      sI     ++-#gnn./
/C1q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>rO      s   
  *  @ @@ = =@5  > 
 
 
 
r$   