ó
|£*^c           @   s­   d  Z  d d l m Z d d l Z d d l Z d j d d d g ƒ Z d d	 d
 d d d d g Z d d „ Z	 e	 Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s   Test sequences for graphiness.
iÿÿÿÿ(   t   defaultdictNs   
s   Aric Hagberg (hagberg@lanl.gov)s   Pieter Swart (swart@lanl.gov)s�   Dan Schult (dschult@colgate.edu)Joel Miller (joel.c.miller.research@gmail.com)Ben EdwardsBrian Cloteaux <brian.cloteaux@nist.gov>t   is_graphicalt   is_multigraphicalt   is_pseudographicalt   is_digraphicalt%   is_valid_degree_sequence_erdos_gallait%   is_valid_degree_sequence_havel_hakimit   is_valid_degree_sequencet   egc         C   s[   | d k r! t  t |  ƒ ƒ } n6 | d k rB t t |  ƒ ƒ } n d } t j | ƒ ‚ | S(   sF  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"
        The method used to validate the degree sequence.
        "eg" corresponds to the ErdÅ‘s-Gallai algorithm, and
        "hh" to the Havel-Hakimi algorithm.

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

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> sequence = G.degree().values()
    >>> nx.is_valid_degree_sequence(sequence)
    True

    References
    ----------
    ErdÅ‘s-Gallai
        [EG1960]_, [choudum1986]_

    Havel-Hakimi
        [havel1955]_, [hakimi1962]_, [CL1996]_
    R   t   hhs   `method` must be 'eg' or 'hh'(   R   t   listR   t   nxt   NetworkXException(   t   sequencet   methodt   validt   msg(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyR      s    $c         C   s  t  j j |  ƒ s t  j ‚ n  t |  ƒ } d g | } d | d d f \ } } } } x‰ |  D]� } | d k  sz | | k r† t  j ‚ q\ | d k r\ t | | ƒ t | | ƒ | | | d f \ } } } } | | c d 7<q\ q\ W| d sÿ | | | d k rt  j ‚ n  | | | | | f S(   Ni    i   i   (   R   t   utilst   is_list_of_intst   NetworkXUnfeasiblet   lent   maxt   min(   t   deg_sequencet   pt   num_degst   dmaxt   dmint   dsumt   nt   d(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyt   _basic_graphical_testsL   s    8c         C   s¯  y t  |  ƒ \ } } } } } Wn t j k
 r6 t SX| d k sk d | | | | d | | d k ro t Sd g | d } x(| d k rªx | | d k r¯ | d 8} q’ W| | d k rÄ t S| | d | d | | <} d } | } xy t | ƒ D]k }	 x | | d k r | d 8} qW| | d | d | | <} | d k rú | d | | <| d 7} qú qú Wx; t | ƒ D]- }	 | |	 }
 | |
 d | d | |
 <} qvWqƒ Wt S(   s€  Returns True if deg_sequence can be realized by a simple graph.

    The validation proceeds using the Havel-Hakimi theorem.
    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]_, [hakimi1962]_, [CL1996]_

    i    i   i   (   R   R   R   t   Falset   Truet   range(   R   R   R   R   R   R   t   modstubst   mslent   kt   it   stub(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyR   `   s4    "4
%c         C   ss  y t  |  ƒ \ } } } } } Wn t j k
 r6 t SX| d k sk d | | | | d | | d k ro t Sd \ } } } }	 xë t | | d d ƒ D]Ó }
 |
 | d k  r² t S| |
 d k r˜ | |
 } |
 | | k  ré |
 | } n  | | |
 7} x@ t | ƒ D]2 } | | | | 7} |	 | | | | | 7}	 qW| | 7} | | | d | | |	 k rkt Sq˜ q˜ Wt S(   s>  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 \geq 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]_, [choudum1986]_
    i    i   i   iÿÿÿÿ(   i    i    i    i    (   R   R   R   R    R!   R"   (   R   R   R   R   R   R   R%   t   sum_degt   sum_njt   sum_jnjt   dkt   run_sizet   v(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyR   ¨   s,    34

 c         C   s‹   t  |  ƒ } t j j | ƒ s" t Sd \ } } x8 | D]0 } | d k  rK t S| | t | | ƒ } } q5 W| d sƒ | d | k  r‡ t St S(   s,  Returns True if some multigraph can realize the sequence.

    Parameters
    ----------
    deg_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).
    i    i   (   i    i    (   R
   R   R   R   R    R   R!   (   R   R   R   R   R   (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyR   õ   s    c         C   sH   t  |  ƒ } t j j | ƒ s" t St | ƒ d d k oG t | ƒ d k S(   s¹  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).
    i   i    (   R
   R   R   R   R    t   sumR   (   R   t   s(    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyR     s    c         C   s>  t  |  ƒ } t  | ƒ } t j j | ƒ s. t St j j | ƒ sD t Sd d t | ƒ t | ƒ f \ } } } } t | | ƒ } d }	 | d k r“ t Sg  g  }
 } xÝ t | ƒ D]Ï } d \ } } | | k  rØ | | } n  | | k  rñ | | } n  | d k  s	| d k  rt S| | | | t |	 | ƒ } } }	 | d k r\|
 j	 d | d | f ƒ q­ | d k r­ | j	 d | ƒ q­ q­ W| | k r�t St
 j |
 ƒ t
 j | ƒ d g |	 d } x||
 r9t
 j |
 ƒ \ } } | d 9} | t |
 ƒ t | ƒ k rt Sd } x³ t | ƒ D]¥ } | rY|
 sA|
 d d | d k rYt
 j | ƒ } d } n t
 j |
 ƒ \ } } | d k r~t S| d d k  sš| d k  r| d | f | | <| d 7} qqWxU t | ƒ D]G } | | } | d d k  rÿt
 j |
 | ƒ qÌt
 j | | d ƒ qÌW| d k  r¾t
 j | | ƒ q¾q¾Wt S(   s)  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 * 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)
    i    iÿÿÿÿi   (   i    i    (   i    i    (   R
   R   R   R   R    R   R   R!   R"   t   appendt   heapqt   heapifyt   heappopt   heappush(   t   in_sequencet   out_sequencet   in_deg_sequencet   out_deg_sequencet   sumint   sumoutt   nint   noutt   maxnt   maxint   stubheapt   zeroheapR   t   in_degt   out_degR#   t   freeoutt   freeinR$   R&   t   stuboutt   stubinR'   (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyR   7  sl    *%	
%	
(   t   __doc__t   collectionsR    R1   t   networkxR   t   joint
   __author__t   __all__R   R   R   R   R   R   R   R   (    (    (    sq   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/graphical.pyt   <module>   s*   		-		H	M	#	