U
    ¹mœd‰4  ã                   @   sb   d Z ddlZddlZddddddgZdd
d„Zdd„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
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Úegc                 C   s>   |dkrt t| ƒƒ}n$|dkr,tt| ƒƒ}nd}t |¡‚|S )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

    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   ÚnxZNetworkXException)ÚsequenceÚmethodZvalidÚmsg© r   úV/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/networkx/algorithms/graphical.pyr      s    *
c                 C   sÀ   t j | ¡} t| ƒ}dg| }d|ddf\}}}}| D ]\}|dk sJ||krRt j‚q6|dkr6t||ƒt||ƒ|| |d f\}}}}||  d7  < q6|d s¬|||d  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Údr   r   r   Ú_basic_graphical_testsE   s    
(r!   c                 C   sR  zt | ƒ\}}}}}W n tjk
r.   Y dS X |dks\d| | || d || d  kr`dS dg|d  }|dk�rN|| dkrŽ|d8 }qx||d krždS || d |d  ||< }d}|}t|ƒD ]P}	|| dkrâ|d8 }qÌ|| d |d  ||< }|dkrÈ|d ||< |d7 }qÈt|ƒD ](}	||	 }
||
 d |d  ||
< }�q"qnd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.

    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Ústubr   r   r   r   Y   s4    &,



 c                 C   s,  zt | ƒ\}}}}}W n tjk
r.   Y dS X |dks\d| | || d || d  kr`dS d\}}}}	t||d dƒD ]ª}
|
|d k r’ dS ||
 dkr|||
 }|
|| k rº|
| }|||
 7 }t|ƒD ],}||||  7 }|	|| |||   7 }	qÎ||7 }|||d  ||  |	 kr| dS q|dS )uT  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.

    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'   Zsum_degZsum_njZsum_jnjZdkZrun_sizeÚvr   r   r   r   ¥   s,    2,c                 C   sz   zt j | ¡}W n t jk
r(   Y dS X d\}}|D ]&}|dk rH dS || t||ƒ }}q6|d sr|d| k rvd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.

    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    r   r   r   r   ò   s    c                 C   sF   zt j | ¡}W n t jk
r(   Y dS X t|ƒd dkoDt|ƒ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.

    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   r   r   r   r     s
    c                 C   st  zt j | ¡}t j |¡}W n t jk
r4   Y dS X ddt|ƒt|ƒf\}}}}t||ƒ}d}	|dkrldS g g  }
}t|ƒD ]˜}d\}}||k rš|| }||k rª|| }|dk sº|dk rÀ dS || || t|	|ƒ  }}}	|dk�r |
 d| d| f¡ q~|dkr~| d| ¡ q~||k�r&dS t 	|
¡ t 	|¡ dg|	d  }|
�rpt 
|
¡\}}|d9 }|t|
ƒt|ƒ k�r~dS d}t|ƒD ]†}|�rÀ|
�r°|
d d |d k�rÀt 
|¡}d}nt 
|
¡\}}|dk�rÞ dS |d dk �sö|dk �rŠ|d |f||< |d7 }�qŠt|ƒD ]:}|| }|d dk �rBt |
|¡ nt ||d ¡ �q|dk �rHt ||¡ �qHdS )a4  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.

    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,   r*   r   )r   r   r   r-   r   r   r$   ÚappendÚheapqÚheapifyÚheappopÚheappush)Zin_sequenceZout_sequenceZin_deg_sequenceZout_deg_sequenceZsuminZsumoutZninZnoutZmaxnZmaxinZstubheapZzeroheapr   Zin_degZout_degr%   ZfreeoutZfreeinr&   r(   ZstuboutZstubinr)   r   r   r   r   8  sj    





"


)r   )Ú__doc__r0   Znetworkxr   Ú__all__r   r!   r   r   r   r   r   r   r   r   r   Ú<module>   s    ú

4LM%!