ó
 ,µ[c           @   sq  d  Z  d d l m Z d d l Z d d l m Z d d g Z d „  Z d „  Z	 e
 e
 d	 „ Z d
 „  Z d „  Z d „  Z d e
 e
 d „ Z d d „ Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z  d „  Z! e d ƒ d d  „ ƒ Z" d! „  Z# d" „  Z$ e d# ƒ d$ d$ d d% „ ƒ Z% d S(&   s?   
Threshold Graphs - Creation, manipulation and identification.
iÿÿÿÿ(   t   sqrtN(   t   py_random_statet   is_threshold_grapht   find_threshold_graphc         C   s    t  t d „  |  j ƒ  Dƒ ƒ ƒ S(   s1   
    Returns True if G is a threshold graph.
    c         s   s   |  ] \ } } | Vq d  S(   N(    (   t   .0t   nt   d(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pys	   <genexpr>   s    (   t   is_threshold_sequencet   listt   degree(   t   G(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyR      s    c         C   sŠ   |  } | j  ƒ  xr | r… | d d k r= | j d ƒ q n  | d t | ƒ d k r[ t S| j ƒ  g  | D] } | d ^ ql } q Wt S(   s‘  
    Returns True if the sequence is a threshold degree seqeunce.

    Uses the property that a threshold graph must be constructed by
    adding either dominating or isolated nodes. Thus, it can be
    deconstructed iteratively by removing a node of degree zero or a
    node that connects to the remaining nodes.  If this deconstruction
    failes then the sequence is not a threshold sequence.
    i    iÿÿÿÿi   (   t   sortt   popt   lent   Falset   True(   t   degree_sequencet   dsR   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyR      s    

	
!c   
      C   sº  | r | r t  d ƒ ‚ n  t |  t ƒ rX g  |  j ƒ  D] \ } } | | g ^ q7 } n+ g  t |  ƒ D] \ } } | | g ^ qe } | j ƒ  g  } xï | r„| d d d k r| j d ƒ \ } }	 t | ƒ d k rð | j d |	 d f ƒ q– | j d |	 d f ƒ q– n  | d d t | ƒ d k r.d S| j ƒ  \ } }	 | j d |	 d f ƒ g  | D] } | d d | d g ^ q]} q– W| r| S| rŸt
 | ƒ Sg  | D] }	 |	 d ^ q¦S(   sæ  
    Determines the creation sequence for the given threshold degree sequence.

    The creation sequence is a list of single characters 'd'
    or 'i': 'd' for dominating or 'i' for isolated vertices.
    Dominating vertices are connected to all vertices present when it
    is added.  The first node added is by convention 'd'.
    This list can be converted to a string if desired using "".join(cs)

    If with_labels==True:
    Returns a list of 2-tuples containing the vertex number
    and a character 'd' or 'i' which describes the type of vertex.

    If compact==True:
    Returns the creation sequence in a compact form that is the number
    of 'i's and 'd's alternating.
    Examples:
    [1,2,2,3] represents d,i,i,d,d,i,i,i
    [3,1,2] represents d,d,d,i,d,d

    Notice that the first number is the first vertex to be used for
    construction and so is always 'd'.

    with_labels and compact cannot both be True.

    Returns None if the sequence is not a threshold sequence
    s#   compact sequences cannot be labeledi    t   iR   iÿÿÿÿi   N(   t
   ValueErrort
   isinstancet   dictt   itemst	   enumerateR   R   R   t   insertt   Nonet   make_compact(
   R   t   with_labelst   compactt   labelR	   R   R   R   t   cst   v(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   creation_sequence3   s0    .+
	/
c         C   sæ   |  d } t  | t ƒ r# |  } nN t  | t ƒ rR g  |  D] } | d ^ q9 } n t  | t ƒ re |  St d ƒ ‚ g  } d } xU t d t | ƒ ƒ D]> } | | | | d k r¾ | d 7} q“ | j | ƒ d } q“ W| j | ƒ | S(   sK  
    Returns the creation sequence in a compact form
    that is the number of 'i's and 'd's alternating.

    Examples
    --------
    >>> from networkx.algorithms.threshold import make_compact
    >>> make_compact(['d', 'i', 'i', 'd', 'd', 'i', 'i', 'i'])
    [1, 2, 2, 3]
    >>> make_compact(['d', 'd', 'd', 'i', 'd', 'd'])
    [3, 1, 2]

    Notice that the first number is the first vertex
    to be used for construction and so is always 'd'.

    Labeled creation sequences lose their labels in the
    compact representation.

    >>> make_compact([3, 1, 2])
    [3, 1, 2]
    i    i   s"   Not a valid creation sequence type(   R   t   strt   tuplet   intt	   TypeErrort   rangeR   t   append(   R    t   firstR   t   st   ccst   countR   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyR   n   s"    

 
c         C   s¯   |  d } t  | t ƒ r |  St  | t ƒ r0 |  St  | t ƒ rI |  } n t d ƒ ‚ g  } xM | rª | j | j d ƒ d g ƒ | r^ | j | j d ƒ d g ƒ q^ q^ W| S(   sÍ   
    Converts a compact creation sequence for a threshold
    graph to a standard creation sequence (unlabeled).
    If the creation_sequence is already standard, return it.
    See creation_sequence.
    i    s"   Not a valid creation sequence typeR   R   (   R   R!   R"   R#   R$   t   extendR   (   R    R'   t   ccscopyR   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt	   uncompactš   s    

	$c   
      C   s´  |  d } t  | t ƒ rA t  |  t ƒ r2 |  } qš t |  ƒ } nY t  | t ƒ rp g  |  D] } | d ^ qW } n* t  | t ƒ rŽ t |  ƒ } n t d ƒ ‚ | j ƒ  d } d } xX t | ƒ D]J \ } } | d k rè | | | <| } q½ | d k r½ | } | d 7} q½ q½ W| j ƒ  xX t | ƒ D]J \ } } | d k rM| | | <| } q"| d k r"| } | d 7} q"q"W| d k r‰| d 7} n  d t	 | ƒ } g  | D] }	 |	 | ^ q S(   sö   
    Returns a list of node weights which create the threshold
    graph designated by the creation sequence.  The weights
    are scaled so that the threshold is 1.0.  The order of the
    nodes is the same as that in the creation sequence.
    i    i   s"   Not a valid creation sequence typeR   R   g      ð?(
   R   R!   R   R"   R#   R-   R$   t   reverseR   t   float(
   R    R'   t   wseqR   t   wt   prevt   jR(   t   wscalet   ww(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   creation_sequence_to_weights²   s@    

 

	

	i   c         C   s¡  | r | r t  d ƒ ‚ n  t |  t ƒ rX g  |  j ƒ  D] \ } } | | g ^ q7 } n+ g  t |  ƒ D] \ } } | | g ^ qe } | j ƒ  g  } | | d d }	 xº | ra| d d |	 k  rí | j d ƒ \ } } | j | d f ƒ n7 | j ƒ  \ } } | j | d f ƒ | | d d }	 t | ƒ d k r¨ | j ƒ  \ } } | j | d f ƒ q¨ q¨ W| j	 ƒ  | rv| S| r†t
 | ƒ Sg  | D] }
 |
 d ^ qS(   sø  
    Returns a creation sequence for a threshold graph
    determined by the weights and threshold given as input.
    If the sum of two node weights is greater than the
    threshold value, an edge is created between these nodes.

    The creation sequence is a list of single characters 'd'
    or 'i': 'd' for dominating or 'i' for isolated vertices.
    Dominating vertices are connected to all vertices present
    when it is added.  The first node added is by convention 'd'.

    If with_labels==True:
    Returns a list of 2-tuples containing the vertex number
    and a character 'd' or 'i' which describes the type of vertex.

    If compact==True:
    Returns the creation sequence in a compact form that is the number
    of 'i's and 'd's alternating.
    Examples:
    [1,2,2,3] represents d,i,i,d,d,i,i,i
    [3,1,2] represents d,d,d,i,d,d

    Notice that the first number is the first vertex to be used for
    construction and so is always 'd'.

    with_labels and compact cannot both be True.
    s#   compact sequences cannot be labelediÿÿÿÿi    R   R   i   (   R   R   R   R   R   R   R   R&   R   R.   R   (   t   weightst	   thresholdR   R   R   R1   R0   R   R   t   cutoffR   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   weights_to_creation_sequenceá   s0    .+
	

c   	      C   s"  |  d } t  | t ƒ r. t t |  ƒ ƒ } nR t  | t ƒ rG |  } n9 t  | t ƒ rw t |  ƒ } t t | ƒ ƒ } n	 d GHd St j	 d | ƒ } | j
 ƒ  r° t j d ƒ ‚ n  d | _ xb | r| j d ƒ \ } } | d k rx' t | ƒ D] } | j | | ƒ qð Wn  | j | ƒ q¼ W| S(   sÌ  
    Create a threshold graph from the creation sequence or compact
    creation_sequence.

    The input sequence can be a

    creation sequence (e.g. ['d','i','d','d','d','i'])
    labeled creation sequence (e.g. [(0,'d'),(2,'d'),(1,'i')])
    compact creation sequence (e.g. [2,1,1,2,0])

    Use cs=creation_sequence(degree_sequence,labeled=True)
    to convert a degree sequence to a creation sequence.

    Returns None if the sequence is not valid
    i    s"   not a valid creation sequence types   Directed Graph not supporteds   Threshold GraphR   N(   R   R!   R   R   R"   R#   R-   R   t   nxt   empty_grapht   is_directedt   NetworkXErrort   nameR   t   add_edget   add_node(	   R    t   create_usingR'   t   ciR   R
   R   t	   node_typet   u(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   threshold_graph  s*    

		c         C   s£   xœ |  j  ƒ  D]Ž \ } } x |  j ƒ  D]q } |  j | | ƒ r& | | k r& xI |  j | ƒ D]5 } |  j | | ƒ r[ | | k r[ | | | | g Sq[ Wq& q& Wq Wt S(   s¯   
    Returns False if there aren't any alternating 4 cycles.
    Otherwise returns the cycle as [a,b,c,d] where (a,b)
    and (c,d) are edges and (a,c) and (b,d) are not.
    (   t   edgest   nodest   has_edget	   neighborsR   (   R
   RE   R   R1   t   x(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   find_alternating_4_cycleQ  s    c         C   s   t  t |  ƒ | ƒ S(   sˆ   
    Return a threshold subgraph that is close to largest in G.
    The threshold graph will contain the largest degree node in G.

    (   RF   t   find_creation_sequence(   R
   RB   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyR   `  s    c   	      C   s@  g  } |  } x#| j  ƒ  d k r1t | j ƒ  ƒ } g  | j ƒ  D] \ } } | | f ^ q@ } | j ƒ  | d d d k r® | j t | d g t | ƒ d d g ƒ ƒ Pn  x@ | d d d k rð | j d ƒ \ } } | j	 | d f ƒ q± W| j ƒ  \ } } | j	 | d f ƒ | j
 | j | ƒ ƒ } q W| j ƒ  | S(   s…   
    Find a threshold subgraph that is close to largest in G.
    Returns the labeled creation sequence of that threshold graph.
    i    iÿÿÿÿR   i   R   (   t   orderR   R	   R   R   R+   t   zipR   R   R&   t   subgraphRJ   R.   (	   R
   R   t   Ht   dsdictR   R   R   t   isot   bigv(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyRM   i  s"    +
.
c         C   s   |  } | j  d ƒ } | | d | d d } xI t | ƒ D]; \ } } | d k rm | | | d d 7} q< | d 8} q< W| S(   sb   
    Compute number of triangles in the threshold graph with the
    given creation sequence.
    R   i   i   i   R   (   R*   R   (   R    R   t   drt   ntriR   t   typ(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt	   triangles‰  s    c         C   sï   |  } g  } | j  d ƒ } | d | d d } d } d } x« t | ƒ D] \ } } | d k r | d 7} | | d | }	 nS |
 d k r¸ | | d | 7} d } | | 8} d } n  | d 7} | | d d }	 | j |	 ƒ | }
 qJ W| S(   sT   
    Return triangle sequence for the given threshold graph creation sequence.

    R   i   i   i    (   R*   R   R&   (   R    R   t   seqRU   t   dcurt   irunt   drunR   t   symt   trit   prevsym(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   triangle_sequence  s(    

	

c         C   s—   t  |  ƒ } t |  ƒ } g  } xr t | ƒ D]d \ } } | | } | d k r` | j d ƒ q+ n  | | d d } | j t | ƒ t | ƒ ƒ q+ W| S(   sR   
    Return cluster sequence for the given threshold graph creation sequence.
    i   i    i   (   R`   R   R   R&   R/   (   R    t   triseqt   degseqt   cseqR   t   degR^   t   max_size(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   cluster_sequence¹  s    
!c         C   ss   |  } g  } | j  d ƒ } xQ t | ƒ D]C \ } } | d k r^ | d 8} | j | | ƒ q( | j | ƒ q( W| S(   s]   
    Return degree sequence for the threshold graph with the given
    creation sequence
    R   i   (   R*   R   R&   (   R    R   RY   t   rdR   R]   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyR   Ê  s    
c         C   s@   t  |  ƒ } t t |  ƒ ƒ } | | d } | t | ƒ } | S(   s   
    Return the density of the graph with this creation_sequence.
    The density is the fraction of possible edges present.
    i   (   R   t   sumR   R/   (   R    t   Nt   two_sizet   two_possiblet   den(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   densityÛ  s
    c         C   s”  |  } d } d } d } d } | j  d ƒ } g  t | ƒ D] \ } } | d k r: | ^ q: }	 t | ƒ }
 x¾ t | ƒ D]° \ } } | d k rÆ | |	 d k r¶ d | |	 f GHt ‚ n  |	 j d ƒ n  |
 | } xT |	 D]L } |
 | } | | | 7} | | d | d 7} | | | 7} | d 7} q× Wqw Wd | | | | } d | | | | } | d k r†| d k rsd St d | ƒ ‚ n  | t | ƒ S(   s>   
    Return the degree-degree correlation over all edges.
    i    R   s!   Logic error in degree_correlationi   i   i   s$   Zero Denominator but Numerator is %s(   R*   R   R   R   R   R/   (   R    R   t   s1t   s2t   s3t   mRg   R   R]   t   rdiR   t   degit   djt   degjt   denomt   numer(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   degree_correlationç  s8    1	

c         C   s¾  |  d } t  | t ƒ rK g  t t |  ƒ ƒ D] } | |  | f ^ q, } nr t  | t ƒ rd |  } nY t  | t ƒ r± t |  ƒ } g  t t | ƒ ƒ D] } | | | f ^ q’ } n t d ƒ ‚ g  | D] } | d ^ qÄ } | | k rù t d | ƒ ‚ n  | | k rt d | ƒ ‚ n  | | k r+| g S| j	 | ƒ }	 | j	 | ƒ }
 t
 |	 |
 ƒ } | | d d k rv| | g S| | } x7 | r¹| j ƒ  } | d d k rƒ| | d | g SqƒWd S(   s>  
    Find the shortest path between u and v in a
    threshold graph G with the given creation_sequence.

    For an unlabeled creation_sequence, the vertices
    u and v must be integers in (0,len(sequence)) referring
    to the position of the desired vertices in the sequence.

    For a labeled creation_sequence, u and v are labels of veritices.

    Use cs=creation_sequence(degree_sequence,with_labels=True)
    to convert a degree sequence to a creation sequence.

    Returns a list of vertices from u to v.
    Example: if they are neighbors, it returns [u,v]
    i    s"   Not a valid creation sequence types-   Vertex %s not in graph from creation_sequencei   R   iÿÿÿÿ(   R   R!   R%   R   R"   R#   R-   R$   R   t   indext   maxR   (   R    RE   R   R'   R   R   RC   R(   t   vertst   uindext   vindext   bigindt   vert(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   shortest_path	  s6    
2
2

	c         C   s•  |  d } t  | t ƒ rA t  |  t ƒ r2 |  } qÀ t |  ƒ } n t  | t ƒ r– g  |  D] } | d ^ qW } g  |  D] } | d ^ qt j | ƒ } n* t  | t ƒ r´ t |  ƒ } n t d ƒ ‚ t | ƒ } d g | } d | | <x8 t	 | d | ƒ D]# } | | d k r÷ d | | <q÷ q÷ W| | d k rRx! t	 | ƒ D] } d | | <q;Wn  x< t	 | d d d ƒ D]$ } | | d k rƒPn  d | | <qiW| S(   s  
    Return the shortest path length from indicated node to
    every other node for the threshold graph with the given
    creation sequence.
    Node is indicated by index i in creation_sequence unless
    creation_sequence is labeled in which case, i is taken to
    be the label of the node.

    Paths lengths in threshold graphs are at most 2.
    Length to unreachable nodes is set to -1.
    i    i   s"   Not a valid creation sequence typei   R   iÿÿÿÿ(
   R   R!   R   R"   Ry   R#   R-   R$   R   R%   (   R    R   R'   R   R   Ri   t   splR3   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   shortest_path_length>  s2    

)
c         C   s=  |  } g  } d } t  | j d ƒ ƒ } d } d } d } xµ t | ƒ D]§ \ }	 }
 |
 d k r™ | | d | | d | |	 | | | } | d 7} n; | d k rÄ | } | | 8} d } d } n  d } | d 7} | j t  | ƒ ƒ |
 } qF W| r9t | ƒ } d | d | d } g  | D] } | | ^ q } n  | S(   s¸   
    Return betweenness for the threshold graph with the given creation
    sequence.  The result is unscaled.  To scale the values
    to the iterval [0,1] divide by (n-1)*(n-2).
    R   i    g        i   i   g      ð?(   R/   R*   R   R&   R   (   R    t
   normalizedR   RY   t   lastcharRU   R[   R\   t   dlastR   t   ct   bRN   t   scaleR(   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   betweenness_sequencel  s2    .
	

 c         C   st  t  |  ƒ } t | ƒ } d g | } | } t | d d d … ƒ } | d } d t | ƒ g | | d <d | d <| } | | 8} t } d }	 d }
 xv |
 | k  rd t |
 |
 |	 ƒ } |	 | g |
 | g d g | |	 d | |	 <| | |	 <|	 d 7}	 |
 d 7}
 q™ Wt | ƒ d k r+| | f Sx<| d D]0} d t | |	 |	 | ƒ } |	 | | g | |	 | g d g | |	 | | |	 <| } | r´|	 | } | | 8} n | } | | |	 <|	 } |	 d 7}	 d }
 x‰ |
 | k  red t |	 | |
 |
 ƒ } d g | |	 | | g |
 | g d g | |	 d | |	 <| | |	 <|	 d 7}	 |
 d 7}
 qÝWq6W| | f S(   sÞ  
    Return a 2-tuple of Laplacian eigenvalues and eigenvectors
    for the threshold network with creation_sequence.
    The first value is a list of eigenvalues.
    The second value is a list of eigenvectors.
    The lists are in the same order so corresponding eigenvectors
    and eigenvalues are in the same position in the two lists.

    Notice that the order of the eigenvalues returned by eigenvalues(cs)
    may not correspond to the order of these eigenvectors.
    i    Ni   g      ð?i   (   R   Rh   R    R   R   (   R    R)   Ri   t   vect   valRU   t   nnt   et   type_dR   t   ddRˆ   t   st(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   eigenvectors”  sP    


0


8


?

c         C   sd   g  } | d } xM | D]E } t  g  t | |  ƒ D] \ } } | | ^ q0 ƒ } | j | ƒ q W| S(   s   
    Returns the coefficients of each eigenvector
    in a projection of the vector u onto the normalized
    eigenvectors which are contained in eigenpairs.

    eigenpairs should be a list of two objects.  The
    first is a list of eigenvalues and the second a list
    of eigenvectors.  The eigenvectors should be lists.

    There's not a lot of error checking on lengths of
    arrays, etc. so be careful.
    i   (   Rh   RO   R&   (   RE   t
   eigenpairst   coefft   evectt   evt   evvt   uvR†   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   spectral_projectionÎ  s    
2c         C   s–   t  |  ƒ } | j ƒ  g  } d } t | ƒ } | j ƒ  } xU | r‘ | | k  ri | j | ƒ | d 8} q= | d 7} | rˆ | j ƒ  } q= d } q= W| S(   s   
    Return sequence of eigenvalues of the Laplacian of the threshold
    graph for the given creation_sequence.

    Based on the Ferrer's diagram method.  The spectrum is integral
    and is the conjugate of the degree sequence.

    See::

      @Article{degree-merris-1994,
       author = 	 {Russel Merris},
       title = 	 {Degree maximal graphs are Laplacian integral},
       journal = 	 {Linear Algebra Appl.},
       year = 	 {1994},
       volume = 	 {199},
       pages = 	 {381--389},
      }

    i    i   (   R   R   R   R   R&   (   R    Rb   t   eiglistt   eigt   rowt   bigdeg(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   eigenvaluesã  s    
	

i   c         C   s   d | k o d k n s+ t  d ƒ ‚ n  d g } xF t d |  ƒ D]5 } | j ƒ  | k  rl | j d ƒ qD | j d ƒ qD W| S(   s  
    Create a random threshold sequence of size n.
    A creation sequence is built by randomly choosing d's with
    probabiliy p and i's with probability 1-p.

    s=nx.random_threshold_sequence(10,0.5)

    returns a threshold sequence of length 10 with equal
    probably of an i or a d at each position.

    A "random" threshold graph can be built with

    G=nx.threshold_graph(s)

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    i    i   s   p must be in [0,1]R   R   (   R   R%   t   randomR&   (   R   t   pt   seedR   R   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   random_threshold_sequence  s    	c         C   sº   d g d g |  d } | |  k  r2 d | | <| S| |  |  d d k rY t  d ƒ ‚ n  |  d } |  d } x. | | k  r d | | <| d 8} | | 7} qp W| | | } d | | <| S(   sç   
    Create a skewed threshold graph with a given number
    of vertices (n) and a given number of edges (m).

    The routine returns an unlabeled creation sequence
    for the threshold graph.

    FIXME: describe algorithm

    R   R   i   i   s#   Too many edges for this many nodes.(   R   (   R   Rq   R   t   indRh   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   right_d_threshold_sequence/  s    





c         C   sÉ   d g d g |  d } | |  k  r2 d | | <| S| |  |  d d k rY t  d ƒ ‚ n  d | |  d <|  d } d } x. | | k  r§ d | | <| | 7} | d 7} qz W| | k rÅ d | | | <n  | S(   sç   
    Create a skewed threshold graph with a given number
    of vertices (n) and a given number of edges (m).

    The routine returns an unlabeled creation sequence
    for the threshold graph.

    FIXME: describe algorithm

    R   R   i   i   s#   Too many edges for this many nodes.(   R   (   R   Rq   R   Rh   R¢   (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   left_d_threshold_sequenceQ  s     



i   g      ð?c         C   sl  g  t  |  d d !ƒ D] \ } } | d k r | ^ q } | j ƒ  | k  rÕ | j | ƒ } | j t | ƒ ƒ } | | }	 | |	 k rÕ |  | d k rÕ |  |	 d k rÕ d |  | <d |  | <d |  |	 <| j | ƒ qÕ n  | j ƒ  | k  rh| rh| j | ƒ }
 | j | ƒ } |
 | } | t |  ƒ k sC|  | d k sC|
 | k rG|  Sd |  |
 <d |  | <d |  | <n  |  S(   s‚  
    Perform a "swap" operation on a threshold sequence.

    The swap preserves the number of nodes and edges
    in the graph for the given sequence.
    The resulting sequence is still a threshold sequence.

    Perform one split and one combine operation on the
    'd's of a creation sequence for a threshold graph.
    This operation maintains the number of nodes and edges
    in the graph, but shifts the edges from node to node
    maintaining the threshold quality of the graph.

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    i   iÿÿÿÿR   R   (   R   Rž   t   choiceR%   t   removeR   (   R   t   p_splitt	   p_combineR    R   RD   t   dlistR¥   t   split_tot	   flip_sidet   first_choicet   second_choicet   target(    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   swap_dt  s(    8
,



.

(&   t   __doc__t   mathR    t   networkxR;   t   networkx.utilsR   t   __all__R   R   R   R    R   R-   R6   R:   R   RF   RL   R   RM   RX   R`   Rf   R   Rm   Rx   R€   R‚   R   R‰   R‘   R˜   R   R¡   R£   R¤   R¯   (    (    (    s<   lib/python2.7/site-packages/networkx/algorithms/threshold.pyt   <module>   sB   		;	,		/=3			 						"	5	.(	:		)	"	"	#	