
 ,[c           @   s   d  Z  d d l Z d d l Z d j d d d g  Z d d d	 d
 d d g Z d d  Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d S(   s   Test sequences for graphiness.
iNs   
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   egc         C   s[   | d k r! t  t |    } n6 | d k rB t t |    } n d } t j |   | S(   s@  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 = (d for n, d in G.degree())
    >>> nx.is_graphical(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(    (    s<   lib/python2.7/site-packages/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(    (    s<   lib/python2.7/site-packages/networkx/algorithms/graphical.pyt   _basic_graphical_testsI   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(    (    s<   lib/python2.7/site-packages/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(   s7  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]_, [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(    (    s<   lib/python2.7/site-packages/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
    ----------
    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   (    (    s<   lib/python2.7/site-packages/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(    (    s<   lib/python2.7/site-packages/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(   s4  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)
    i    ii   (   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%   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/graphical.pyR   8  sl    *%	
%	
(   t   __doc__R/   t   networkxR	   t   joint
   __author__t   __all__R    R   R   R   R   R   R   (    (    (    s<   lib/python2.7/site-packages/networkx/algorithms/graphical.pyt   <module>   s$   		-		H	N	$	 