Ë
    ÷Ç:j·  ã                   óÆ   — 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       ús/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/networkx/utils/random_sequence.pyr   r      s+   € ô
 7<¸A³hÖ?°ˆD×Ñ˜x¨!™|Õ,Ò?Ð?ùÒ?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   è ø€ Ò0 q˜A �FÑ0ùs   ‚)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Ü˜#“h€OÜ ›HÐàˆ?ÑÐ2Ñ2°aÒ7ØIØ	��ŠœÑ0¨CÔ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 ˆa‚xÜ˜Ó$Ð$Ø�‚zÜ˜Ó$Ð$Ø	�‰€BØ	ˆ2‰€AØ
Ø�$—+‘+“-ÑˆØ�K‰K‹MˆÜ��q˜c B™h˜KÑ'Ñ'Ó(ˆØ�C˜!‘G‰_ Ñ#ˆØˆq‰5�A˜‘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   ‚   sP   € ð ˆ%€CØ€JØò ˆØ�gÑˆ
Ø�
‰
�:Õðð 14Ö4 WˆG�jÓ Ò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ó
ð 	
ô
 (-¨Q£xÖ0 !�—‘•Ð0€HÐ0ð 4<Ö
<¨aˆ6×Ñ˜c 1Ó%¨Ó)Ð
<€CÐ
<Ø€Jùò	 1ùò =s   ºA7ÁA<c                 óÎ   — |t        | «      kD  rt        d«      ‚t        «       }t        |«      |k  r*|j                  t	        | |«      «       t        |«      |k  rŒ*t        |«      S )z€Returns 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   € ð 	Œ3ˆw‹<ÒÜÐ8Ó9Ð9Ü‹U€FÜ
ˆf‹+˜Š/Ø�
‰
”? 7¨DÓ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
   »   sN   € ð �+‰+‹-œ#˜gŸn™nÓ.Ó/Ñ
/€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$   