Ë
    D^(h×=  ã                   óT  — d Z ddlZddlZg d¢Z ej
                  d¬«      dd„«       Zd„ Z ej
                  d¬«      d„ «       Z ej
                  d¬«      d„ «       Z	 ej
                  d¬«      d	„ «       Z
 ej
                  d¬«      d
„ «       Z ej
                  d¬«      d„ «       Zy)zTest sequences for graphiness.é    N)Úis_graphicalÚis_multigraphicalÚis_pseudographicalÚis_digraphicalÚ%is_valid_degree_sequence_erdos_gallaiÚ%is_valid_degree_sequence_havel_hakimi)Úgraphsc                 óœ   — |dk(  rt        t        | «      «      }|S |dk(  rt        t        | «      «      }|S d}t        j                  |«      ‚)us  Returns True if sequence is a valid degree sequence.

    A degree sequence is valid if some graph can realize it.

    Parameters
    ----------
    sequence : list or iterable container
        A sequence of integer node degrees

    method : "eg" | "hh"  (default: 'eg')
        The method used to validate the degree sequence.
        "eg" corresponds to the ErdÅ‘s-Gallai algorithm
        [EG1960]_, [choudum1986]_, and
        "hh" to the Havel-Hakimi algorithm
        [havel1955]_, [hakimi1962]_, [CL1996]_.

    Returns
    -------
    valid : bool
        True if the sequence is a valid degree sequence and False if not.

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> sequence = (d for n, d in G.degree())
    >>> nx.is_graphical(sequence)
    True

    To test a non-graphical sequence:
    >>> sequence_list = [d for n, d in G.degree()]
    >>> sequence_list[-1] += 1
    >>> nx.is_graphical(sequence_list)
    False

    References
    ----------
    .. [EG1960] ErdÅ‘s and Gallai, Mat. Lapok 11 264, 1960.
    .. [choudum1986] S.A. Choudum. "A simple proof of the ErdÅ‘s-Gallai theorem on
       graph sequences." Bulletin of the Australian Mathematical Society, 33,
       pp 67-70, 1986. https://doi.org/10.1017/S0004972700002872
    .. [havel1955] Havel, V. "A Remark on the Existence of Finite Graphs"
       Casopis Pest. Mat. 80, 477-480, 1955.
    .. [hakimi1962] Hakimi, S. "On the Realizability of a Set of Integers as
       Degrees of the Vertices of a Graph." SIAM J. Appl. Math. 10, 496-506, 1962.
    .. [CL1996] G. Chartrand and L. Lesniak, "Graphs and Digraphs",
       Chapman and Hall/CRC, 1996.
    ÚegÚhhz`method` must be 'eg' or 'hh')r   Úlistr   ÚnxÚNetworkXException)ÚsequenceÚmethodÚvalidÚmsgs       ú[/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/graphical.pyr   r      sW   € ðb �‚~Ü5´d¸8³nÓEˆð €Lð 
�4ŠÜ5´d¸8³nÓEˆð €Lð .ˆÜ×"Ñ" 3Ó'Ð'ó    c                 óx  — t         j                  j                  | «      } t        | «      }dg|z  }d|ddf\  }}}}| D ]T  }|dk  s||k\  rt         j                  ‚|dkD  sŒ#t        ||«      t        ||«      ||z   |dz   f\  }}}}||xx   dz  cc<   ŒV |dz  s|||dz
  z  kD  rt         j                  ‚|||||fS )Nr   é   é   )r   ÚutilsÚmake_list_of_intsÚlenÚNetworkXUnfeasibleÚmaxÚmin)Údeg_sequenceÚpÚnum_degsÚdmaxÚdminÚdsumÚnÚds           r   Ú_basic_graphical_testsr'   L   sÞ   € ä—8‘8×-Ñ-¨lÓ;€LÜˆLÓ€AØˆs�Q‰w€HØ˜Q  1˜*Ñ€Dˆ$��aØò ˆàˆqŠ5�A˜’FÜ×'Ñ'Ð'à�‹UÜ"% d¨A£,´°D¸!³¸dÀQ¹hÈÈAÉÐ"MÑˆD�$˜˜aØ�Q‹K˜1ÑŒKðð ˆa‚x�4˜!˜q 1™u™+Ò%Ü×#Ñ#Ð#Ø��t˜Q Ð(Ð(r   c                 ó
  — 	 t        | «      \  }}}}}|dk(  sd|z  |z  ||z   dz   ||z   dz   z  k\  rydg|dz   z  }|dkD  rª||   dk(  r|dz  }||   dk(  rŒ||dz
  kD  ry||   dz
  |dz
  c||<   }d}|}t        |«      D ]<  }	||   dk(  r|dz  }||   dk(  rŒ||   dz
  |dz
  c||<   }|dkD  sŒ0|dz
  ||<   |dz  }Œ> t        |«      D ]  }	||	   }
||
   dz   |dz   c||
<   }Œ |dkD  rŒªy# t        j                  $ r Y yw xY w)a–  Returns True if deg_sequence can be realized by a simple graph.

    The validation proceeds using the Havel-Hakimi theorem
    [havel1955]_, [hakimi1962]_, [CL1996]_.
    Worst-case run time is $O(s)$ where $s$ is the sum of the sequence.

    Parameters
    ----------
    deg_sequence : list
        A list of integers where each element specifies the degree of a node
        in a graph.

    Returns
    -------
    valid : bool
        True if deg_sequence is graphical and False if not.

    Examples
    --------
    >>> G = nx.Graph([(1, 2), (1, 3), (2, 3), (3, 4), (4, 2), (5, 1), (5, 4)])
    >>> sequence = (d for _, d in G.degree())
    >>> nx.is_valid_degree_sequence_havel_hakimi(sequence)
    True

    To test a non-valid sequence:
    >>> sequence_list = [d for _, d in G.degree()]
    >>> sequence_list[-1] += 1
    >>> nx.is_valid_degree_sequence_havel_hakimi(sequence_list)
    False

    Notes
    -----
    The ZZ condition says that for the sequence d if

    .. math::
        |d| >= \frac{(\max(d) + \min(d) + 1)^2}{4*\min(d)}

    then d is graphical.  This was shown in Theorem 6 in [1]_.

    References
    ----------
    .. [1] I.E. Zverovich and V.E. Zverovich. "Contributions to the theory
       of graphic sequences", Discrete Mathematics, 105, pp. 292-303 (1992).
    .. [havel1955] Havel, V. "A Remark on the Existence of Finite Graphs"
       Casopis Pest. Mat. 80, 477-480, 1955.
    .. [hakimi1962] Hakimi, S. "On the Realizability of a Set of Integers as
       Degrees of the Vertices of a Graph." SIAM J. Appl. Math. 10, 496-506, 1962.
    .. [CL1996] G. Chartrand and L. Lesniak, "Graphs and Digraphs",
       Chapman and Hall/CRC, 1996.
    Fr   é   r   T©r'   r   r   Úrange)r   r"   r#   r$   r%   r!   ÚmodstubsÚmslenÚkÚiÚstubs              r   r   r   `   s’  € ðhÜ(>¸|Ó(LÑ%ˆˆd�D˜!˜Xð 	ˆA‚v��T‘˜A‘ $¨¡+°¡/°d¸T±kÀA±oÑ!FÒFØàˆs�d˜Q‘hÑ€Hà
ˆaŠ%à�t‰n Ò!Ø�A‰IˆDð �t‰n Ó!ð �!�a‘%Š<Øð % T™N¨QÑ.°°A±Ðˆ�‰˜àˆØˆÜ�t“ò 	ˆAØ˜1‘+ Ò"Ø�Q‘�ð ˜1‘+ Ó"à% a™[¨1™_¨a°!©eˆNˆH�Q‰K˜Ø�1‹uØ"# a¡%�˜‘Ø˜‘
‘ð	ô �u“ò 	:ˆAØ˜A‘;ˆDØ (¨¡°Ñ 2°A¸±EÐˆH�T‰N™Að	:ð- ˆa‹%ð2 øôC × Ñ ò Ùðús   ‚C, Ã,DÄDc                 óº  — 	 t        | «      \  }}}}}|dk(  sd|z  |z  ||z   dz   ||z   dz   z  k\  ryd\  }}}}	t        ||dz
  d«      D ]v  }
|
|dz   k  r y||
   dkD  sŒ||
   }|
||z   k  r|
|z
  }|||
z  z  }t        |«      D ]  }||||z      z  }|	||z   |||z      z  z  }	Œ  ||z  }|||dz
  z  ||z  z
  |	z   kD  sŒv y y# t        j                  $ r Y yw xY w)uï  Returns True if deg_sequence can be realized by a simple graph.

    The validation is done using the ErdÅ‘s-Gallai theorem [EG1960]_.

    Parameters
    ----------
    deg_sequence : list
        A list of integers

    Returns
    -------
    valid : bool
        True if deg_sequence is graphical and False if not.

    Examples
    --------
    >>> G = nx.Graph([(1, 2), (1, 3), (2, 3), (3, 4), (4, 2), (5, 1), (5, 4)])
    >>> sequence = (d for _, d in G.degree())
    >>> nx.is_valid_degree_sequence_erdos_gallai(sequence)
    True

    To test a non-valid sequence:
    >>> sequence_list = [d for _, d in G.degree()]
    >>> sequence_list[-1] += 1
    >>> nx.is_valid_degree_sequence_erdos_gallai(sequence_list)
    False

    Notes
    -----

    This implementation uses an equivalent form of the ErdÅ‘s-Gallai criterion.
    Worst-case run time is $O(n)$ where $n$ is the length of the sequence.

    Specifically, a sequence d is graphical if and only if the
    sum of the sequence is even and for all strong indices k in the sequence,

     .. math::

       \sum_{i=1}^{k} d_i \leq k(k-1) + \sum_{j=k+1}^{n} \min(d_i,k)
             = k(n-1) - ( k \sum_{j=0}^{k-1} n_j - \sum_{j=0}^{k-1} j n_j )

    A strong index k is any index where d_k >= k and the value n_j is the
    number of occurrences of j in d.  The maximal strong index is called the
    Durfee index.

    This particular rearrangement comes from the proof of Theorem 3 in [2]_.

    The ZZ condition says that for the sequence d if

    .. math::
        |d| >= \frac{(\max(d) + \min(d) + 1)^2}{4*\min(d)}

    then d is graphical.  This was shown in Theorem 6 in [2]_.

    References
    ----------
    .. [1] A. Tripathi and S. Vijay. "A note on a theorem of ErdÅ‘s & Gallai",
       Discrete Mathematics, 265, pp. 417-420 (2003).
    .. [2] I.E. Zverovich and V.E. Zverovich. "Contributions to the theory
       of graphic sequences", Discrete Mathematics, 105, pp. 292-303 (1992).
    .. [EG1960] ErdÅ‘s and Gallai, Mat. Lapok 11 264, 1960.
    Fr   r)   r   T)r   r   r   r   éÿÿÿÿr*   )r   r"   r#   r$   r%   r!   r.   Úsum_degÚsum_njÚsum_jnjÚdkÚrun_sizeÚvs                r   r   r   º   sJ  € ð@Ü(>¸|Ó(LÑ%ˆˆd�D˜!˜Xð 	ˆA‚v��T‘˜A‘ $¨¡+°¡/°d¸T±kÀA±oÑ!FÒFØð #-Ñ€A€w�˜Ü�D˜$ ™( BÓ'ò ˆØ��A‘Š:ÙØ�B‰<˜!ÓØ ‘|ˆHØ�A˜‘LÒ Ø ™6�Ø�x "‘}Ñ$ˆGÜ˜8“_ò 5�Ø˜( 1 q¡5™/Ñ)�Ø˜A ™E X¨a°!©e¡_Ñ4Ñ4‘ð5ð �‰MˆAØ˜˜a !™e™ q¨6¡zÑ1°GÑ;Ó;Ùðð øô- × Ñ ò Ùðús   ‚C ÃCÃCc                 óÚ   — 	 t         j                  j                  | «      }d\  }}|D ]  }|dk  r y||z   t	        ||«      }}Œ |dz  s|d|z  k  ryy# t         j                  $ r Y yw xY w)a­  Returns True if some multigraph can realize the sequence.

    Parameters
    ----------
    sequence : list
        A list of integers

    Returns
    -------
    valid : bool
        True if deg_sequence is a multigraphic degree sequence and False if not.

    Examples
    --------
    >>> G = nx.MultiGraph([(1, 2), (1, 3), (2, 3), (3, 4), (4, 2), (5, 1), (5, 4)])
    >>> sequence = (d for _, d in G.degree())
    >>> nx.is_multigraphical(sequence)
    True

    To test a non-multigraphical sequence:
    >>> sequence_list = [d for _, d in G.degree()]
    >>> sequence_list[-1] += 1
    >>> nx.is_multigraphical(sequence_list)
    False

    Notes
    -----
    The worst-case run time is $O(n)$ where $n$ is the length of the sequence.

    References
    ----------
    .. [1] S. L. Hakimi. "On the realizability of a set of integers as
       degrees of the vertices of a linear graph", J. SIAM, 10, pp. 496-506
       (1962).
    F©r   r   r   r   T)r   r   r   ÚNetworkXErrorr   )r   r   r$   r"   r&   s        r   r   r     s†   € ðJÜ—x‘x×1Ñ1°(Ó;ˆð �J€Dˆ$Øò ,ˆØˆqŠ5ÙØ˜A‘Xœs 4¨›|ˆd‰ð,ð ˆa‚x�4˜!˜d™(’?ØØøô ×Ñò Ùðús   ‚A ÁA*Á)A*c                 ó¶   — 	 t         j                  j                  | «      }t	        |«      dz  dk(  xr t        |«      dk\  S # t         j                  $ r Y yw xY w)a:  Returns True if some pseudograph can realize the sequence.

    Every nonnegative integer sequence with an even sum is pseudographical
    (see [1]_).

    Parameters
    ----------
    sequence : list or iterable container
        A sequence of integer node degrees

    Returns
    -------
    valid : bool
      True if the sequence is a pseudographic degree sequence and False if not.

    Examples
    --------
    >>> G = nx.Graph([(1, 2), (1, 3), (2, 3), (3, 4), (4, 2), (5, 1), (5, 4)])
    >>> sequence = (d for _, d in G.degree())
    >>> nx.is_pseudographical(sequence)
    True

    To test a non-pseudographical sequence:
    >>> sequence_list = [d for _, d in G.degree()]
    >>> sequence_list[-1] += 1
    >>> nx.is_pseudographical(sequence_list)
    False

    Notes
    -----
    The worst-case run time is $O(n)$ where n is the length of the sequence.

    References
    ----------
    .. [1] F. Boesch and F. Harary. "Line removal algorithms for graphs
       and their degree lists", IEEE Trans. Circuits and Systems, CAS-23(12),
       pp. 778-782 (1976).
    Fr   r   )r   r   r   r;   Úsumr   )r   r   s     r   r   r   H  s\   € ðPÜ—x‘x×1Ñ1°(Ó;ˆô ˆ|Ó˜qÑ  AÑ%Ò@¬#¨lÓ*;¸qÑ*@Ð@øô ×Ñò Ùðús   ‚A ÁAÁAc                 óÖ  — 	 t         j                  j                  | «      }t         j                  j                  |«      }ddt	        |«      t	        |«      f\  }}}}t        ||«      }d}	|dk(  ryg g }}
t        |«      D ]v  }d\  }}||k  r||   }||k  r||   }|dk  s|dk  r y||z   ||z   t        |	|«      }	}}|dkD  r|
j                  d|z  d|z  f«       Œ]|dkD  sŒc|j                  d|z  «       Œx ||k7  ryt        j                  |
«       t        j                  |«       dg|	dz   z  }|
�rt        j                  |
«      \  }}|dz  }|t	        |
«      t	        |«      z   kD  ryd}t        |«      D ]h  }|r(|
r|
d   d   |d   kD  rt        j                  |«      }d}nt        j                  |
«      \  }}|dk(  r y|dz   dk  s|dk  sŒZ|dz   |f||<   |dz  }Œj t        |«      D ]?  }||   }|d   dk  rt        j                  |
|«       Œ't        j                  ||d   «       ŒA |dk  rt        j                  ||«       |
r�Œy# t         j                  $ r Y yw xY w)aè  Returns True if some directed graph can realize the in- and out-degree
    sequences.

    Parameters
    ----------
    in_sequence : list or iterable container
        A sequence of integer node in-degrees

    out_sequence : list or iterable container
        A sequence of integer node out-degrees

    Returns
    -------
    valid : bool
      True if in and out-sequences are digraphic False if not.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 3), (3, 4), (4, 2), (5, 1), (5, 4)])
    >>> in_seq = (d for n, d in G.in_degree())
    >>> out_seq = (d for n, d in G.out_degree())
    >>> nx.is_digraphical(in_seq, out_seq)
    True

    To test a non-digraphical scenario:
    >>> in_seq_list = [d for n, d in G.in_degree()]
    >>> in_seq_list[-1] += 1
    >>> nx.is_digraphical(in_seq_list, out_seq)
    False

    Notes
    -----
    This algorithm is from Kleitman and Wang [1]_.
    The worst case runtime is $O(s \times \log n)$ where $s$ and $n$ are the
    sum and length of the sequences respectively.

    References
    ----------
    .. [1] D.J. Kleitman and D.L. Wang
       Algorithms for Constructing Graphs and Digraphs with Given Valences
       and Factors, Discrete Mathematics, 6(1), pp. 79-88 (1973)
    Fr   Tr:   r2   r   )r   r   r   r;   r   r   r+   ÚappendÚheapqÚheapifyÚheappopÚheappush)Úin_sequenceÚout_sequenceÚin_deg_sequenceÚout_deg_sequenceÚsuminÚsumoutÚninÚnoutÚmaxnÚmaxinÚstubheapÚzeroheapr%   Úin_degÚout_degr,   ÚfreeoutÚfreeinr-   r/   ÚstuboutÚstubinr0   s                          r   r   r   w  s˜  € ðXÜŸ(™(×4Ñ4°[ÓAˆÜŸ8™8×5Ñ5°lÓCÐð
  ! !¤S¨Ó%9¼3Ð?OÓ;PÐPÑ€Eˆ6�3˜Üˆs�D‹>€DØ€EØˆq‚yØØ˜Rˆh€HÜ�4‹[ò *ˆØ‰ˆ�ØˆtŠ8Ø& qÑ)ˆGØˆsŠ7Ø$ QÑ'ˆFØ�AŠ:˜ 1šÙØ$ v™~¨v¸Ñ/?ÄÀUÈFÓAS�uˆvˆØ�AŠ:Ø�O‰O˜R '™\¨2°©;Ð7Õ8Ø�q‹[Ø�O‰O˜B ™LÕ)ð*ð �‚ØÜ	‡M�M�(ÔÜ	‡M�M�(Ôàˆx˜5 1™9Ñ%€Hâ
ä!ŸM™M¨(Ó3Ñˆ�&Ø�"‰ˆØ”C˜“M¤C¨£MÑ1Ò1Øð ˆÜ�v“ò 	ˆAÙ¡¨X°a©[¸©^¸hÀq¹kÒ-IÜŸ-™-¨Ó1�Ø‘ä$)§M¡M°(Ó$;Ñ!�˜&Ø˜!Š|Ùà˜‰{˜QŠ &¨1£*Ø#*¨Q¡;°Ð"7�˜‘Ø˜‘
‘ð	ô �u“ò 	2ˆAØ˜A‘;ˆDØ�A‰w˜Š{Ü—‘˜x¨Õ.ä—‘˜x¨¨a©Õ1ð	2ð �QŠ;Ü�N‰N˜8 WÔ-ó= ð> øô{ ×Ñò Ùðús   ‚>I ÉI(É'I()r   )Ú__doc__r@   Únetworkxr   Ú__all__Ú_dispatchabler   r'   r   r   r   r   r   © r   r   ú<module>r[      sí   ðÙ $ã ã ò€ð €×Ñ˜Ôò7ó ð7òt)ð( €×Ñ˜ÔñVó ðVðr €×Ñ˜ÔñWó ðWðt €×Ñ˜Ôñ/ó ð/ðd €×Ñ˜Ôñ+Aó ð+Að\ €×Ñ˜Ôñkó ñkr   