
    wgy                        d Z ddlmZ ddlZddlmZ ddgZej                  d        Z	d Z
d'd	Zd
 Zd Zd Z	 d(dZ ej                  dd      d)d       Zej                  d        Z ej                  d      d)d       Zej                  d        Zd Zd Zd Zd Zd Zd Zd Zd Zd*dZd Zd Zd  Z ed!      d)d"       Z d# Z!d$ Z" ed%      d+d&       Z#y),z?
Threshold Graphs - Creation, manipulation and identification.
    )sqrtN)py_random_stateis_threshold_graphfind_threshold_graphc                 f    t        | j                         D cg c]  \  }}|	 c}}      S c c}}w )ax  
    Returns `True` if `G` is a threshold graph.

    Parameters
    ----------
    G : NetworkX graph instance
        An instance of `Graph`, `DiGraph`, `MultiGraph` or `MultiDiGraph`

    Returns
    -------
    bool
        `True` if `G` is a threshold graph, `False` otherwise.

    Examples
    --------
    >>> from networkx.algorithms.threshold import is_threshold_graph
    >>> G = nx.path_graph(3)
    >>> is_threshold_graph(G)
    True
    >>> G = nx.barbell_graph(3, 3)
    >>> is_threshold_graph(G)
    False

    References
    ----------
    .. [1] Threshold graphs: https://en.wikipedia.org/wiki/Threshold_graph
    )is_threshold_sequencedegree)Gnds      b/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/threshold.pyr   r      s'    : !
!;1!!;<<!;s   -
c                     | dd }|j                          |rT|d   dk(  r|j                  d       |d   t        |      dz
  k7  ry|j                          |D cg c]  }|dz
  	 }}|rTyc c}w )a  
    Returns True if the sequence is a threshold degree sequence.

    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
    fails then the sequence is not a threshold sequence.
    Nr      FT)sortpoplen)degree_sequencedsr   s      r   r   r   -   sy     
	BGGI
a5A:FF1Ib6SWq[ 
 a!e     !s   A-c                    |r|rt        d      t        | t              r%| j                         D cg c]	  \  }}||g }}}nt	        |       D cg c]	  \  }}||g }}}|j                          g }|r|d   d   dk(  rL|j                  d      \  }}	t        |      dkD  r|j                  d|	df       n|j                  d|	df       Y|d   d   t        |      dz
  k7  ry|j                         \  }}	|j                  d|	df       |D cg c]  }|d   dz
  |d   g }}|r|r|S |rt        |      S |D 	cg c]  }	|	d   	 c}	S c c}}w c c}}w c c}w c c}	w )a  
    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
    #compact sequences cannot be labeledr   ir   r   r   N)

ValueError
isinstancedictitems	enumerater   r   r   insertmake_compact)
r   with_labelscompactlabelr	   r   r   r   csvs
             r   creation_sequencer%   D   sm   8 w>?? /4(5D5J5J5LM/5&vuoMM!*?!;<Aq!f<<GGI	B
a58q=VVAYFQ2w{		!aX&		!aX&b6!9B!#A
		!aX(*+1qtax1++  	BQAaD/ N< , s   EEE E c                 r   | d   }t        |t              r| dd }n@t        |t              r| D cg c]  }|d   	 }}nt        |t              r| S t	        d      g }d}t        dt        |            D ])  }||   ||dz
     k(  r|dz  }|j                  |       d}+ |j                  |       |S c c}w )aK  
    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]
    r   Nr   "Not a valid creation sequence type)r   strtupleint	TypeErrorranger   append)r%   firstr#   sccscountr   s          r   r   r      s    , a E%q!	E5	!-.qad..	E3	  <==
CE1c"g a5Bq1uIQJEJJuE JJuJ /s   B4c                 <   | d   }t        |t              r| S t        |t              r| S t        |t              r| dd }nt	        d      g }|rM|j                  |j                  d      dgz         |r$|j                  |j                  d      dgz         |rM|S )z
    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.
    r   Nr'   r   r   )r   r(   r)   r*   r+   extendr   )r%   r.   ccscopyr#   s       r   	uncompactr5      s     a E%  	E5	!  	E3	#A&<==	B

		'++a.C5()IIgkk!nu,-  I    c                 X   | d   }t        |t              r"t        | t              r| dd }nVt        |       }nJt        |t              r| D cg c]  }|d   	 }}n't        |t              rt        |       }nt        d      |j                          d}d}t        |      D ]  \  }}|dk(  r|||<   |}|dk(  s|}|dz  }! |j                          t        |      D ]  \  }}|dk(  r|||<   |}|dk(  s|}|dz  }! |dk(  r|dz  }d|z  }|D 	cg c]  }	|	|z  	 c}	S c c}w c c}	w )z
    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.
    r   Nr   r'   r   r   )	r   r(   listr)   r*   r5   r+   reverser   )
r%   r.   wseqr$   wprevjr/   wscalewws
             r   creation_sequence_to_weightsr@      sT    a E%'.$Q'D)*D	E5	!/0!00	E3	*+<==LLN	AD$ 18DGDS[DFA 	LLN$ 18DGDS[DFA s{	QUF"&'BBK''9 18 (s   D"D'c                    |r|rt        d      t        | t              r%| j                         D cg c]	  \  }}||g }}}nt	        |       D cg c]	  \  }}||g }}}|j                          g }||d   d   z
  }	|r|d   d   |	k  r(|j                  d      \  }}|j                  |df       n1|j                         \  }}|j                  |df       ||d   d   z
  }	t        |      dk(  r&|j                         \  }}|j                  |df       |r|j                          |r|S |rt        |      S |D 
cg c]  }
|
d   	 c}
S c c}}w c c}}w c c}
w )a  
    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.
    r   r   r   r   r   r   )r   r   r   r   r   r   r   r-   r   r9   r   )weights	thresholdr    r!   r"   r;   r:   r   r#   cutoffr$   s              r   weights_to_creation_sequencerE      sY   < w>?? '4 -4]]_=zqE
==#,W#5641aA66IIK	Bb!$F
71:!JQIIucl#JQIIucl#b!,Ft9>JQIIucl#  JJL	BQAaD1 >6, s   EEET)graphsreturns_graphc                 $   | d   }t        |t              rt        t        |             }nRt        |t              r| dd }n<t        |t
              r t        |       }t        t        |            }nt        d       yt        j                  d|      }|j                         rt        j                  d      d|_        |rO|j                  d      \  }}|dk(  r"t        |      D ]  }|j                  ||        |j                  |       |rO|S )a  
    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
    r   Nz"not a valid creation sequence typezDirected Graph not supportedzThreshold Graphr   )r   r(   r8   r   r)   r*   r5   printnxempty_graphis_directedNetworkXErrornamer   add_edgeadd_node)	r%   create_usingr.   cir#   r
   r$   	node_typeus	            r   threshold_graphrU   1  s    $ a E%)-./	E5	!q!	E3	())B- 23
q,'A}}=>>AF
 I
 !W !

1a !	

1  Hr6   c                    | j                         D ]m  \  }}| j                         D ]U  }| j                  ||      r||k7  s| j                  |      D ]&  }| j                  ||      r||k7  s||||gc c c S  W o y)z
    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.
    F)edgesnodeshas_edge	neighbors)r
   rT   r$   r;   xs        r   find_alternating_4_cycler\   e  s     	 ,1 	,A::a#QQ ,A::a+Q !1a|+,	,, r6   )rG   c                 ,    t        t        |       |      S )as  
    Returns a threshold subgraph that is close to largest in `G`.

    The threshold graph will contain the largest degree node in G.

    Parameters
    ----------
    G : NetworkX graph instance
        An instance of `Graph`, or `MultiDiGraph`
    create_using : NetworkX graph class or `None` (default), optional
        Type of graph to use when constructing the threshold graph.
        If `None`, infer the appropriate graph type from the input.

    Returns
    -------
    graph :
        A graph instance representing the threshold graph

    Examples
    --------
    >>> from networkx.algorithms.threshold import find_threshold_graph
    >>> G = nx.barbell_graph(3, 3)
    >>> T = find_threshold_graph(G)
    >>> T.nodes  # may vary
    NodeView((7, 8, 5, 6))

    References
    ----------
    .. [1] Threshold graphs: https://en.wikipedia.org/wiki/Threshold_graph
    )rU   find_creation_sequence)r
   rQ   s     r   r   r   u  s    @ 1!4lCCr6   c           	         g }| }|j                         dkD  r!t        |j                               }|j                         D cg c]	  \  }}||f }}}|j	                          |d   d   dk(  r0|j                  t        |dgt        |      dz
  z  dgz                n|d   d   dk(  r3|j                  d      \  }}|j                  |df       |d   d   dk(  r3|j                         \  }}|j                  |df       |j                  |j                  |            }|j                         dkD  r!|j                          |S c c}}w )z
    Find a threshold subgraph that is close to largest in G.
    Returns the labeled creation sequence of that threshold graph.
    r   r   r   r   r   )orderr   r	   r   r   r3   zipr   r   r-   subgraphrZ   r9   )	r
   r#   Hdsdictr$   r   r   isobigvs	            r   r^   r^     s-    
B	A
'')a-ahhj!!'0Aq!f00
	b6!9>IIc&3%3r7Q;"73%"?@AeAh!mvvayHQIIsCj! eAh!m FFH	D
		4+JJq{{4()% '')a-& JJLI# 1s   Ec                     | }|j                  d      }||dz
  z  |dz
  z  dz  }t        |      D ]  \  }}|dk(  r|||dz
  z  dz  z  }|dz  }  |S )zb
    Compute number of triangles in the threshold graph with the
    given creation sequence.
    r   r         r   )r1   r   )r%   r#   drntrir   typs         r   	trianglesrm     sx     
B	#Ba=BF#a'D B- 3#:B"q&MA%%D!GB	
 Kr6   c                    | }g }|j                  d      }|dz
  |dz
  z  dz  }d}d}t        |      D ]W  \  }}|dk(  r|dz  }||dz
  |z  z   }	n)
dk(  r||dz
  |z  z  }d}||z  }d}|dz  }||dz
  z  dz  }	|j                  |	       |}
Y |S )zT
    Return triangle sequence for the given threshold graph creation sequence.

    r   r   rh   r   r1   r   r-   )r%   r#   seqrj   dcurirundrunr   symtriprevsyms              r   triangle_sequencerw     s    
 
B
C	#BFrAv!#DDDB- 3#:AID"q&D(C#~a4'd
AIDQ-1$C

3 Jr6   c                     t        |       }t        |       }g }t        |      D ]@  \  }}||   }|dk  r|j                  d       "||dz
  z  dz  }|j                  ||z         B |S )zR
    Return cluster sequence for the given threshold graph creation sequence.
    r   r   rh   )rw   r   r   r-   )r%   triseqdegseqcseqr   degru   max_sizes           r   cluster_sequencer~     s~     01F./FDF# $3Qi!8KKN37O)C(N#$ Kr6   c                     | }g }|j                  d      }t        |      D ]5  \  }}|dk(  r|dz  }|j                  ||z          %|j                  |       7 |S )z]
    Return degree sequence for the threshold graph with the given
    creation sequence
    r   r   ro   )r%   r#   rp   rdr   rt   s         r   r   r     sd    
 
B
C	#BB- 3#:!GBJJrAvJJrN Jr6   c                 ^    t        |       }t        t        |             }||dz
  z  }||z  }|S )z
    Return the density of the graph with this creation_sequence.
    The density is the fraction of possible edges present.
    r   )r   sumr   )r%   Ntwo_sizetwo_possibledens        r   densityr     s;    
 	A?#456HA;L
\
!CJr6   c                    | }d}d}d}d}|j                  d      }t        |      D cg c]  \  }}|dk(  s| }	}}t        |      }
t        |      D ]j  \  }}|dk(  r,||	d   k7  rt        d||	       t        |	j                  d       |
|   }|	D ]*  }|
|   }|||z  z  }||dz  |dz  z   z  }|||z   z  }|dz  }, l d|z  |z  ||z  z
  }d|z  |z  ||z  z
  }|dk(  r|dk(  ryt	        d|       ||z  S c c}}w )z>
    Return the degree-degree correlation over all edges.
    r   r   z!Logic error in degree_correlationrh   r      z"Zero Denominator but Numerator is )r1   r   r   rI   r   r   )r%   r#   s1s2s3mr   r   rt   rdir   degidjdegjdenomnumers                   r   degree_correlationr     sZ    
B	
B	
B	
B	A	#B$R=
7CC3J1
7C
7		BB- 3#:CF{91cB  GGAJ!u 	Bb6D$+B$'D!G##B$+BFA	 EBJb EEBJb EzA:=eWEFF5=+ 8s
   C<C<c                    | d   }t        |t              r't        t        |             D cg c]	  }|| |   f }}nct        |t              r| dd }nMt        |t
              r2t        |       }t        t        |            D cg c]	  }|||   f }}nt        d      |D cg c]  }|d   	 }}||vrt        d| d      ||vrt        d| d      ||k(  r|gS |j                  |      }	|j                  |      }
t        |	|
      }||   d   dk(  r||gS ||d }|r#|j                         }|d   dk(  r||d   |gS |r#yc c}w c c}w c c}w )	a=  
    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 vertices.

    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]
    r   Nr'   zVertex z$ not in graph from creation_sequencer   r   r   )r   r(   r,   r   r)   r*   r5   r+   r   indexmaxr   )r%   rT   r$   r.   r   r#   rR   r/   vertsuindexvindexbigindverts                r   shortest_pathr   9  s   $ a E%16s;L7M1NOAq#A&'OO	E5	!q!	E3	()"'B.1Qq"Q%j11<==aQqTE~71#%IJKK~71#%IJKKAvs
[[^F[[^F F	&z!}1v	FGB
vvx7c>tAw?" 
 = P
 2 s   EE$Ec                 T   | d   }t        |t              r"t        | t              r| dd }nxt        |       }nlt        |t              r5| D cg c]  }|d   	 }}| D cg c]  }|d   	 c}j	                  |      }n't        |t
              rt        |       }nt        d      t        |      }dg|z  }d||<   t        |dz   |      D ]  }||   dk(  sd||<    ||   dk(  rt        |      D ]  }d||<   	 t        |dz
  dd      D ]  }||   dk(  r |S d||<    |S c c}w c c}w )a  
    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.
    r   Nr   r'   rh   r   r   )
r   r(   r8   r)   r   r*   r5   r+   r   r,   )r%   r   r.   r#   r$   r   splr=   s           r   shortest_path_lengthr   n  sY    a E%'."1%B'(B	E5	!-.qad..,-aQqT-33A6	E3	()<== 	BA#'CCF1q5!_ a5C<CF 
!u|q 	ACF	 1q5!R  a5C<J A J/ /-s   D D%c                    | }g }d}t        |j                  d            }d}d}d}t        |      D ]c  \  }	}
|
dk(  r&||dz
  |z  |z  z   d|z  |	|z
  |z
  z  |z  z   }|dz  }n|dk(  r}||z  }d}d}d}|dz  }|j                  t        |             |
}e |r+t	        |      }d|dz
  |dz
  z  z  }|D cg c]  }||z  	 }}|S c c}w )z
    Return betweenness for the threshold graph with the given creation
    sequence.  The result is unscaled.  To scale the values
    to the interval [0,1] divide by (n-1)*(n-2).
    r   r   g        r   rh         ?)floatr1   r   r-   r   )r%   
normalizedr#   rp   lastcharrj   rr   rs   dlastr   cbr`   scaler/   s                  r   betweenness_sequencer     s    
B
CH	rxx}	BDDE" 18 T)B..TQX_1MPR1RRAAID3d
AAID

58!& B	eai01"%&Qq5y&&J 's   7Cc                     t        |       }t        |      }dg|z  }|dd }t        |ddd         }|d   }dt        |      z  g|z  |d<   d|d<   |}||z  }d}d}	d}
|
|k  rGdt        |
|
z  |	z         z  }|	| gz  |
|z  gz   dg||	z
  dz
  z  z   ||	<   |||	<   |	dz  }	|
dz  }
|
|k  rGt        |      dk(  r||fS |dd D ]  }dt        ||	z  |	|z   z        z  }|	| |z  gz  ||	|z  gz  z   dg||	z
  |z
  z  z   ||	<   | }|r|	|z   }||z  }n|}|||	<   |	}|	dz  }	d}
|
|k  sddt        |	|z
  |
|
z  z         z  }dg|z  |	|z
  | gz  z   |
|z  gz   dg||	z
  dz
  z  z   ||	<   |||	<   |	dz  }	|
dz  }
|
|k  rT ||fS )a  
    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.
    r   Nrh   r   Tr   )r   r   r   r   )r%   r0   r   vecvalrj   nnetype_dr   ddr   sts                r   eigenvectorsr     sJ    (
)CCA#'C
a&C	S1XB	QBDGm_q CFCF
A"HBF	A	
B
r'd27Q;''ufXe,sa!eai/@@AA	Q
a r' 3x1}Sz!"g d26QV,--rcEk]"R1u9+%55q1urz8JJABA"HBAA	Q2g$q2vR/00ES2XRUF8 33rEzlBaSAPQETUIEVVCFCFFA!GB 2g( :r6   c                 |    g }|d   }|D ]/  }t        d t        ||       D              }|j                  |       1 |S )a  
    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.
    r   c              3   ,   K   | ]  \  }}||z    y wN ).0evvuvs      r   	<genexpr>z&spectral_projection.<locals>.<genexpr>  s     5Yc2b5s   )r   ra   r-   )rT   
eigenpairscoeffevectevr   s         r   spectral_projectionr     sI     EqME 5#b!*55Q Lr6   c                     t        |       }|j                          g }d}t        |      }|j                         }|r9||k  r|j	                  |       |dz  }n|dz  }|r|j                         }nd}|r9|S )a  
    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},
      }

    r   r   )r   r   r   r   r-   )r%   rz   eiglisteigrowbigdegs         r   eigenvaluesr     s}    ( ./F
KKMG
C
f+CZZ\F
C<NN31HC1HC  Nr6   rh   c                     d|cxk  rdk  st        d       t        d      dg}t        d|       D ]8  }|j                         |k  r|j                  d       (|j                  d       : |S )a  
    Create a random threshold sequence of size n.
    A creation sequence is built by randomly choosing d's with
    probability 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>`.
    r   r   zp must be in [0,1]r   r   )r   r,   randomr-   )r   pseedr#   r   s        r   random_threshold_sequencer   =  ss    ( KaK-.. -..
B1a[ ;;=1IIcNIIcN	
 Ir6   c                     dgdg| dz
  z  z   }|| k  rd||<   |S || | dz
  z  dz  kD  rt        d      | dz
  }| dz
  }||k  rd||<   |dz  }||z  }||k  r|||z
  z
  }d||<   |S z
    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   r   rh   z#Too many edges for this many nodes.r   )r   r   r#   indr   s        r   right_d_threshold_sequencer   `  s     #!a%	 B 	1u1	 	1A;?>?? a%C
a%C
'3qs
 ' sSy/CBsGIr6   c                     dgdg| dz
  z  z   }|| k  rd||<   |S || | dz
  z  dz  kD  rt        d      d|| dz
  <   | dz
  }d}||k  rd||<   ||z  }|dz  }||k  r||kD  rd|||z
  <   |S r   r   )r   r   r#   r   r   s        r   left_d_threshold_sequencer     s     #!a%	 B 	1u1	 	1A;?>?? Bq1uI
a%C
C
'3s
q ' Qw37Ir6      c                    t        | dd       D cg c]  \  }}|dk(  s| }}}|j                         |k  re|j                  |      }|j                  t        |            }||z
  }	||	k7  r0| |   dk(  r(| |	   dk(  r d| |<   d| |<   d| |	<   |j	                  |       |j                         |k  rU|rS|j                  |      }
|j                  |      }|
|z   }|t        |       k\  s| |   dk(  s|
|k(  r| S d| |
<   d| |<   d| |<   | S c c}}w )a  
    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>`.
    r   r   r   r   )r   r   choicer,   remover   )r#   p_split	p_combiner   r   rS   dlistr   split_to	flip_sidefirst_choicesecond_choicetargets                r   swap_dr     s/   ( &/r!Bx%8M>AyI<LQMEM{{}wU#;;uV}-X%	y R\S%8R	]c=QBvJBxLByMLL 
 {{}y U{{5)E*-SW6
c 1\]5RI<=6
 I7 Ns
   D	D	)FF)r   FFr   )T)r   r   N)$__doc__mathr   networkxrJ   networkx.utilsr   __all___dispatchabler   r   r%   r   r5   r@   rE   rU   r\   r   r^   rm   rw   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r   <module>r      s_     *!7
8 = =>.8v)X0+(` 6;;~ T20 30f   %D &DD  @(8""	D2j+\%P7t*$T  DD F . .r6   