§
    bŠtj×=  ã                   ób  — 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dS )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)ÚgraphsÚegc                 ó¾   — |dk    rt          t          | ¦  «        ¦  «        }n9|dk    rt          t          | ¦  «        ¦  «        }nd}t          j        |¦  «        ‚|S )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.
    r
   Úhhz`method` must be 'eg' or 'hh')r   Úlistr   ÚnxÚNetworkXException)ÚsequenceÚmethodÚvalidÚmsgs       ú[/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/algorithms/graphical.pyr   r      s\   € ðb �‚~€~Ý5µd¸8±n´nÑEÔEˆˆØ	�4ŠˆÝ5µd¸8±n´nÑEÔEˆˆà-ˆÝÔ" 3Ñ'Ô'Ð'Ø€Ló    c                 óˆ  — t           j                             | ¦  «        } t          | ¦  «        }dg|z  }d|ddf\  }}}}| D ]]}|dk     s||k    rt           j        ‚|dk    r=t          ||¦  «        t          ||¦  «        ||z   |dz   f\  }}}}||xx         dz  cc<   Œ^|dz  s|||dz
  z  k    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×-Ò-¨lÑ;Ô;€LÝˆLÑÔ€AØˆs�Q‰w€HØ˜Q  1˜*Ñ€Dˆ$��aØð ð ˆàˆqŠ5ˆ5�A˜’F�FÝÔ'Ð'à�ŠUˆUÝ"% d¨A¡,¤,µ°D¸!±´¸dÀQ¹hÈÈAÉÐ"MÑˆD�$˜˜aØ�QˆKˆKŒK˜1ÑˆKˆK‰Køàˆa�xð $�4˜!˜q 1™u™+Ò%Ð%ÝÔ#Ð#Ø��t˜Q Ð(Ð(r   c                 óX  — 	 t          | ¦  «        \  }}}}}n# t          j        $ r Y dS w xY w|dk    sd|z  |z  ||z   dz   ||z   dz   z  k    rdS dg|dz   z  }|dk    rÊ||         dk    r|dz  }||         dk    °||dz
  k    rdS ||         dz
  |dz
  c||<   }d}|}t          |¦  «        D ]F}	||         dk    r|dz  }||         dk    °||         dz
  |dz
  c||<   }|dk    r|dz
  ||<   |dz  }ŒGt          |¦  «        D ]}	||	         }
||
         dz   |dz   c||
<   }Œ|dk    °ÊdS )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Ô(LÑ%ˆˆd�D˜!˜X˜XøÝÔ ð ð ð Øˆuˆuðøøøð 	ˆA‚v€v��T‘˜A‘ $¨¡+°¡/°d¸T±kÀA±oÑ!FÒFÐFØˆtàˆs�d˜Q‘hÑ€Hà
ˆaŠ%ˆ%à�tŒn Ò!Ð!Ø�A‰IˆDð �tŒn Ò!Ð!ð �!�a‘%Š<ˆ<Ø�5ð % TœN¨QÑ.°°A±Ðˆ�‰˜àˆØˆÝ�t‘”ð 	ð 	ˆAØ˜1”+ Ò"Ð"Ø�Q‘�ð ˜1”+ Ò"Ð"à% aœ[¨1™_¨a°!©eˆNˆH�Q‰K˜Ø�1ŠuˆuØ"# a¡%�˜‘Ø˜‘
�øå�u‘”ð 	:ð 	:ˆAØ˜A”;ˆDØ (¨¤°Ñ 2°A¸±EÐˆH�T‰N˜A˜Að1 ˆaŠ%ˆ%ð2 ˆ4ó   ‚ ˜+ª+c                 óð  — 	 t          | ¦  «        \  }}}}}n# t          j        $ r Y dS w xY w|dk    sd|z  |z  ||z   dz   ||z   dz   z  k    rdS d\  }}}}	t          ||dz
  d¦  «        D ]‰}
|
|dz   k     r dS ||
         dk    ro||
         }|
||z   k     r|
|z
  }|||
z  z  }t          |¦  «        D ]$}||||z            z  }|	||z   |||z            z  z  }	Œ%||z  }|||dz
  z  ||z  z
  |	z   k    r dS ŒŠdS )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   º   sz  € ð@Ý(>¸|Ñ(LÔ(LÑ%ˆˆd�D˜!˜X˜XøÝÔ ð ð ð Øˆuˆuðøøøð 	ˆA‚v€v��T‘˜A‘ $¨¡+°¡/°d¸T±kÀA±oÑ!FÒFÐFØˆtð #-Ñ€A€w�˜Ý�D˜$ ™( BÑ'Ô'ð ð ˆØ��A‘Š:ˆ:Ø�4�4Ø�BŒ<˜!ÒÐØ ”|ˆHØ�A˜‘LÒ Ð Ø ™6�Ø�x "‘}Ñ$ˆGÝ˜8‘_”_ð 5ð 5�Ø˜( 1 q¡5œ/Ñ)�Ø˜A ™E X¨a°!©e¤_Ñ4Ñ4��Ø�‰MˆAØ˜˜a !™e™ q¨6¡zÑ1°GÑ;Ò;Ð;Ø�u�uøØˆ4r1   c                 óä   — 	 t           j                             | ¦  «        }n# t           j        $ r Y dS w xY wd\  }}|D ] }|dk     r dS ||z   t	          ||¦  «        }}Œ!|dz  s	|d|z  k     rdS dS )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×1Ò1°(Ñ;Ô;ˆˆøÝÔð ð ð Øˆuˆuðøøøà�J€Dˆ$Øð ,ð ,ˆØˆqŠ5ˆ5Ø�5�5Ø˜A‘X�s 4¨™|œ|ˆdˆˆØˆa�xð �4˜!˜d™(’?�?ØˆuØˆ4ó   ‚" ¢5´5c                 óÂ   — 	 t           j                             | ¦  «        }n# t           j        $ r Y dS w xY wt	          |¦  «        dz  dk    ot          |¦  «        dk    S )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  sn   € ðPÝ”x×1Ò1°(Ñ;Ô;ˆˆøÝÔð ð ð Øˆuˆuðøøøåˆ|ÑÔ˜qÑ  AÒ%Ð@­#¨lÑ*;Ô*;¸qÒ*@Ð@r=   c                 ób  — 	 t           j                             | ¦  «        }t           j                             |¦  «        }n# t           j        $ r Y dS w xY wddt	          |¦  «        t	          |¦  «        f\  }}}}t          ||¦  «        }d}	|dk    rdS g g }}
t          |¦  «        D ]Ž}d\  }}||k     r||         }||k     r||         }|dk     s|dk     r dS ||z   ||z   t          |	|¦  «        }	}}|dk    r|
                     d|z  d|z  f¦  «         Œp|dk    r|                     d|z  ¦  «         Œ�||k    rdS t          j	        |
¦  «         t          j	        |¦  «         dg|	dz   z  }|
�r;t          j
        |
¦  «        \  }}|dz  }|t	          |
¦  «        t	          |¦  «        z   k    rdS d}t          |¦  «        D ]s}|r1|
r|
d         d         |d         k    rt          j
        |¦  «        }d}nt          j
        |
¦  «        \  }}|dk    r dS |dz   dk     s|dk     r|dz   |f||<   |dz  }Œtt          |¦  «        D ]G}||         }|d         dk     rt          j        |
|¦  «         Œ,t          j        ||d         ¦  «         ŒH|dk     rt          j        ||¦  «         |
�°;dS )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;   r3   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ÔAˆÝœ8×5Ò5°lÑCÔCÐÐøÝÔð ð ð Øˆuˆuðøøøð  ! !¥S¨Ñ%9Ô%9½3Ð?OÑ;PÔ;PÐPÑ€Eˆ6�3˜Ýˆs�D‰>Œ>€DØ€EØˆq‚y€yØˆtØ˜Rˆh€HÝ�4‰[Œ[ð *ð *ˆØ‰ˆ�ØˆtŠ8ˆ8Ø& qÔ)ˆGØˆsŠ7ˆ7Ø$ QÔ'ˆFØ�AŠ:ˆ:˜ 1š˜Ø�5�5Ø$ v™~¨v¸Ñ/?ÅÀUÈFÑASÔAS�uˆvˆØ�AŠ:ˆ:Ø�OŠO˜R '™\¨2°©;Ð7Ñ8Ô8Ð8Ð8Ø�qŠ[ˆ[Ø�OŠO˜B ™LÑ)Ô)Ð)øØ�‚€ØˆuÝ	„M�(ÑÔÐÝ	„M�(ÑÔÐàˆx˜5 1™9Ñ%€Hà
ñ .å!œM¨(Ñ3Ô3Ñˆ�&Ø�"‰ˆØ•C˜‘M”M¥C¨¡M¤MÑ1Ò1Ð1Ø�5ð ˆÝ�v‘”ð 	ð 	ˆAØð < ð <¨X°a¬[¸¬^¸hÀq¼kÒ-IÐ-IÝœ-¨Ñ1Ô1�Ø��å$)¤M°(Ñ$;Ô$;Ñ!�˜&Ø˜!Š|ˆ|Ø�u�uà˜‰{˜QŠˆ &¨1¢* *Ø#*¨Q¡;°Ð"7�˜‘Ø˜‘
�øõ �u‘”ð 	2ð 	2ˆAØ˜A”;ˆDØ�AŒw˜Š{ˆ{Ý”˜x¨Ñ.Ô.Ð.Ð.å”˜x¨¨a¬Ñ1Ô1Ð1Ð1Ø�QŠ;ˆ;ÝŒN˜8 WÑ-Ô-Ð-ð= ñ .ð> ˆ4s   ‚>A ÁAÁA)r
   )Ú__doc__rB   Únetworkxr   Ú__all__Ú_dispatchabler   r'   r   r   r   r   r   © r   r   ú<module>r]      sj  ðØ $Ð $à €€€à Ð Ð Ð ðð ð €ð €Ô˜ÐÑÔð7ð 7ð 7ñ Ôð7ðt)ð )ð )ð( €Ô˜ÐÑÔðVð Vñ ÔðVðr €Ô˜ÐÑÔðWð Wñ ÔðWðt €Ô˜ÐÑÔð/ð /ñ Ôð/ðd €Ô˜ÐÑÔð+Að +Añ Ôð+Að\ €Ô˜ÐÑÔðkð kñ Ôðkð kð kr   