
    h=                     2   S r SSKrSSKr/ SQr\R
                  " SS9SS j5       rS r\R
                  " SS9S 5       r\R
                  " SS9S 5       r	\R
                  " SS9S	 5       r
\R
                  " SS9S
 5       r\R
                  " SS9S 5       rg)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                     US:X  a  [        [        U 5      5      nU$ US:X  a  [        [        U 5      5      nU$ Sn[        R                  " U5      e)u  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       O/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/graphical.pyr   r      sW    b ~5d8nE L 
45d8nE L .""3''    c                 x   [         R                  R                  U 5      n [        U 5      nS/U-  nSUSS4u  p4pVU  HR  nUS:  d  Xq:  a  [         R                  eUS:  d  M&  [        X75      [        XG5      XW-   US-   4u  p4pVX'==   S-  ss'   MT     US-  (       d  XVUS-
  -  :  a  [         R                  eX4XVU4$ )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    88--l;LLAsQwHQ1*Dq5AF'''U"%d,DdhA"MDK1K  ax4q1u+%###t((r   c                     [        U 5      u  pp4nUS:X  d  SU-  U-  X-   S-   X-   S-   -  :  a  gS/US-   -  nUS:  a  XQ   S:X  a  US-  nXQ   S:X  a  M  XS-
  :  a  gXQ   S-
  US-
  sXQ'   nSnUn[        U5       H=  n	XX   S:X  a  US-  nXX   S:X  a  M  XX   S-
  US-
  sXX'   nUS:  d  M1  US-
  Xg'   US-  nM?     [        U5       H  n	Xi   n
XZ   S-   US-   sXZ'   nM     US:  a  M  g! [        R                   a     gf = f)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   `   sf   h(>|(L%DX 	AvTA$+/dkAo!FFsdQhH
a%n!AID n! a%< %NQ.AtA+"Q +"%[1_a!eNHK1u"#a%
  uA;D ( 2AEHNA - a%2 C    s   C- -DDc                     [        U 5      u  pp4nUS:X  d  SU-  U-  X-   S-   X-   S-   -  :  a  gSu  pgp[        XS-
  S5       Hq  n
XS-   :  a    gXZ   S:  d  M  XZ   nXU-   :  a  X-
  nX{U
-  -  n[        U5       H  nXXl-      -  nXU-   XVU-      -  -  n	M     Xk-  nXvUS-
  -  Xh-  -
  U	-   :  d  Mq    g   g! [        R                   a     gf = f)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      s   @(>|(L%DX 	AvTA$+/dkAo!FF #-AD(B'A:<!|HL 6"}$G8_15/)EX!e_44 % MAa!eqz1G;; ( -    s   B; ;CCc                      [         R                  R                  U 5      nSu  p#U H  nUS:  a    gX$-   [	        X45      p2M     US-  (       d	  USU-  :  a  gg! [         R                   a     gf = f)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     sy    Jxx11(; JDq5Xs4|d  ax4!d(?  s   A A0/A0c                      [         R                  R                  U 5      n[	        U5      S-  S:H  =(       a    [        U5      S:  $ ! [         R                   a     gf = f)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xx11(; |q A%@#l*;q*@@  s   A AAc                 (    [         R                  R                  U 5      n[         R                  R                  U5      nSS[	        U5      [	        U5      4u  pEpg[        Xg5      nSn	US:X  a  g/ / p[        U5       Hv  nSu  pX:  a  X<   nX:  a  X,   nUS:  d  US:  a    gXM-   X^-   [        X5      pnUS:  a  U
R                  SU-  SU-  45        MZ  US:  d  Mb  UR                  SU-  5        Mx     XE:w  a  g[        R                  " U
5        [        R                  " U5        S/U	S-   -  nU
(       Ga?  [        R                  " U
5      u  nnUS-  nU[	        U
5      [	        U5      -   :  a  gSn[        U5       Hz  nU(       a/  U
(       a  U
S   S   US   :  a  [        R                  " U5      nSnO[        R                  " U
5      u  nnUS:X  a    gUS-   S:  d  US:  d  Mk  US-   U4UU'   US-  nM|     [        U5       HD  nUU   nUS   S:  a  [        R                  " U
U5        M*  [        R                  " UUS   5        MF     US:  a  [        R                  " UU5        U
(       a  GM?  g! [         R                   a     gf = f)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  so   X((44[A8855lC
  !!S%93?O;PPE3s>DEqyRh4[8&)G7$'FA:1$~v/?UASuA:OOR'\2;78q[OOBL)  	MM(	MM(x519%H
!MM(3&"CMCM11 vAXa[^hqk-I--1$)MM($;!&!|{Q&1*#*Q;"7
  uAA;DAw{x.xa1  Q;NN8W-= (> {  s   >I: :JJ)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   