
    hy                        S r SSKJr  SSKrSSKJr  SS/r\R                  S 5       r	S r
S'S	 jrS
 rS rS r S(S jr\R                  " SSS9S)S j5       r\R                  S 5       r\R                  " SS9S)S j5       r\R                  S 5       rS rS rS rS rS rS rS rS rS*S jrS rS rS  r\" S!5      S)S" j5       r S# r!S$ r"\" S%5      S+S& j5       r#g),z?
Threshold Graphs - Creation, manipulation and identification.
    )sqrtN)py_random_stateis_threshold_graphfind_threshold_graphc                 j    [        U R                  5        VVs/ s H  u  pUPM	     snn5      $ s  snnf )a   
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      O/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/threshold.pyr   r      s)    : !
!;
!
!;<<!;s   /
c                    U SS nUR                  5         U(       a_  US   S:X  a  UR                  S5        M#  US   [        U5      S-
  :w  a  gUR                  5         U Vs/ s H  o"S-
  PM	     nnU(       a  M_  gs  snf )at  
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   -   sz     
	BGGI
a5A:FF1Ib6SWq[ 
 R!eR  "  !s   #A=c                 "   U(       a  U(       a  [        S5      e[        U [        5      (       a&  U R                  5        VVs/ s H  u  p4XC/PM
     nnnO [	        U 5       VVs/ s H  u  pgXv/PM
     nnnUR                  5         / nU(       a  US   S   S:X  aM  UR                  S5      u  py[        U5      S:  a  UR                  SU	S45        OUR                  SU	S45        M`  US   S   [        U5      S-
  :w  a  gUR                  5       u  pyUR                  SU	S45        U Vs/ s H  owS   S-
  US   /PM     nnU(       a  M  U(       a  U$ U(       a  [        U5      $ U V	s/ s H  oS   PM	     sn	$ s  snnf s  snnf s  snf s  sn	f )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   so   8 w>?? /4((5D5J5J5LM5L/5vo5LM!*?!;<!;qf!;<GGI	B
a58q=VVAYFQ2w{		!aX&		!aX&b6!9B!#
		!aX(*+1tax1+ " 	B"QaD"/ N< , s   E;#F*F*Fc                    U S   n[        U[        5      (       a  U SS nOL[        U[        5      (       a  U  Vs/ s H  o3S   PM	     nnO"[        U[        5      (       a  U $ [	        S5      e/ nSn[        S[        U5      5       H*  nX&   X&S-
     :X  a  US-  nM  UR                  U5        SnM,     UR                  U5        U$ s  snf )a  
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	!	!-.-qd-.	E3		  <==
CE1c"g5B1uIQJEJJuE  JJuJ /s   Cc                 z   U S   n[        U[        5      (       a  U $ [        U[        5      (       a  U $ [        U[        5      (       a  U SS nO[	        S5      e/ nU(       aX  UR                  UR                  S5      S/-  5        U(       a$  UR                  UR                  S5      S/-  5        U(       a  MX  U$ )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                    U S   n[        U[        5      (       a'  [        U [        5      (       a  U SS nOb[        U 5      nOV[        U[        5      (       a  U  Vs/ s H  o3S   PM	     nnO,[        U[        5      (       a  [        U 5      nO[        S5      eUR                  5         SnSn[        U5       H"  u  pgUS:X  a  XBU'   UnM  US:X  d  M  UnUS-  nM$     UR                  5         [        U5       H"  u  pgUS:X  a  XBU'   UnM  US:X  d  M  UnUS-  nM$     US:X  a  US-  nSU-  nU V	s/ s H  oU-  PM	     sn	$ s  snf s  sn	f )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@      sS    a E%'..$Q'D)*D	E5	!	!/0/!/0	E3		*+<==LLN	AD$8GDS[DFA   	LLN$8GDS[DFA   s{	QUF"&'$BK$''9 18 (s   E2Ec                    U(       a  U(       a  [        S5      e[        U [        5      (       a&  U R                  5        VVs/ s H  u  pEXT/PM
     nnnO [	        U 5       VVs/ s H  u  puXW/PM
     nnnUR                  5         / nXS   S   -
  n	U(       a  US   S   U	:  a'  UR                  S5      u  pTUR                  US45        O/UR                  5       u  pTUR                  US45        XS   S   -
  n	[        U5      S:X  a%  UR                  5       u  pTUR                  US45        U(       a  M  UR                  5         U(       a  U$ U(       a  [        U5      $ U V
s/ s H  oS   PM	     sn
$ s  snnf s  snnf s  sn
f )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      sT   < w>?? '4  -4]]_=_z
_=#,W#56#541#56IIK	Bb!$F
71:!JQIIucl#JQIIucl#b!,Ft9>JQIIucl# $ JJL	B"QaD"1 >6, s   E7#E=&FT)graphsreturns_graphc                 h   U S   n[        U[        5      (       a  [        [        U 5      5      nO\[        U[        5      (       a  U SS nOA[        U[
        5      (       a   [        U 5      n[        [        U5      5      nO[        S5        g[        R                  " SU5      nUR                  5       (       a  [        R                  " S5      eSUl        U(       aV  UR                  S5      u  pgUS:X  a#  [        U5       H  nUR                  Xh5        M     UR                  U5        U(       a  MV  U$ )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
 
 !W

1  	

1 " Hr6   c                     U R                  5        Hz  u  pU R                  5        Ha  nU R                  X5      (       a  M  X:w  d  M"  U R                  U5       H*  nU R                  X$5      (       a  M  X$:w  d  M"  XX4/s  s  s  $    Mc     M|     g)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  sm     	A::a##QA::a++ !a|+ (   r6   )rG   c                 ,    [        [        U 5      U5      $ )a  
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           	         / nU nUR                  5       S:  Ga&  [        UR                  5       5      nUR                  5        VVs/ s H  u  pEXT4PM
     nnnUR	                  5         US   S   S:X  a0  UR                  [        US/[        U5      S-
  -  S/-   5      5        OUS   S   S:X  a4  UR                  S5      u  pWUR                  US45        US   S   S:X  a  M4  UR                  5       u  pXUR                  US45        UR                  UR                  U5      5      nUR                  5       S:  a  GM&  UR                  5         U$ s  snnf )zy
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qf0
	b6!9>IIc&3%3r7Q;"73%"?@AeAh!mvvayHQIIsCj! eAh!m FFH	
		4+JJq{{4()% '')a-& JJLI# 1s   Ec                     U nUR                  S5      nX"S-
  -  US-
  -  S-  n[        U5       H  u  pEUS:X  a  X2US-
  -  S-  -  nM  US-  nM!     U$ )zV
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     sm     
B	#Ba=BF#a'D B-#:"q&MA%%D!GB	  
 Kr6   c                    U n/ nUR                  S5      nUS-
  US-
  -  S-  nSnSn[        U5       HU  u  pxUS:X  a  US-  nXCS-
  U-  -   n	O'W
S:X  a  XCS-
  U-  -  nSnX6-  nSnUS-  nX3S-
  -  S-  n	UR                  U	5        Un
MW     U$ )zL
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-#:AIDq&D(C#~a4'
AIDQ-1$C

3   Jr6   c                     [        U 5      n[        U 5      n/ n[        U5       H?  u  pEX   nUS::  a  UR                  S5        M"  XUS-
  -  S-  nUR                  Xg-  5        MA     U$ )zJ
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~     sq     01F./FDF#i!8KKN7O)CN# $ Kr6   c                     U n/ nUR                  S5      n[        U5       H6  u  pEUS:X  a  US-  nUR                  X4-   5        M%  UR                  U5        M8     U$ )zQ
Return degree sequence for the threshold graph with the given
creation sequence
r   r   ro   )r%   r#   rp   rdr   rt   s         r   r   r     s[    
 
B
C	#BB-#:!GBJJrvJJrN   Jr6   c                 Z    [        U 5      n[        [        U 5      5      nXS-
  -  nX#-  nU$ )zu
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     s7    
 	A?#456HA;L

!CJr6   c                    U nSnSnSnSnUR                  S5      n[        U5       VVs/ s H  u  pxUS:X  d  M  UPM     n	nn[        U5      n
[        U5       Hg  u  pxUS:X  a+  XyS   :w  a  [        SXy5        [        eU	R                  S5        X   nU	 H'  nX   nX-U-  -  nX;S-  US-  -   -  nXKU-   -  nUS-  nM)     Mi     SU-  U-  XD-  -
  nSU-  U-  XD-  -
  nUS:X  a  US:X  a  g[	        SU 35      eX-  $ s  snnf )z6
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     s9    
B	
B	
B	
B	A	#B$R=
7=C3J1=C
7		BB-#:F{91B  GGAJuB6D+B'D!G##B+BFA    EBJ EEBJ EzA:=eWEFF=+ 8s
   C=C=c                    U S   n[        U[        5      (       a(  [        [        U 5      5       Vs/ s H  oDX   4PM
     nnOn[        U[        5      (       a  U SS nOS[        U[
        5      (       a3  [        U 5      n[        [        U5      5       Vs/ s H  oDXd   4PM
     nnO[        S5      eU Vs/ s H  owS   PM	     nnX(;  a  [        SU S35      eX;  a  [        SU S35      eX:X  a  U/$ UR                  U5      n	UR                  U5      n
[        X5      nX[   S   S:X  a  X/$ X[S nU(       a)  UR                  5       nUS   S:X  a  XS   U/$ U(       a  M)  gs  snf s  snf s  snf )	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O1NA#&'1NO	E5	!	!q!	E3		()"'B.1.Q"%j.1<==2aqT2E~71#%IJKK~71#%IJKKvs
[[^F[[^F F	z!}v	GB
vvx7c>Aw?" "
 = P
 2 s   E%E*5E/c                    U S   n[        U[        5      (       a'  [        U [        5      (       a  U SS nO[        U 5      nOz[        U[        5      (       a9  U  Vs/ s H  oDS   PM	     nnU  Vs/ s H  oDS   PM	     snR	                  U5      nO,[        U[
        5      (       a  [        U 5      nO[        S5      e[        U5      nS/U-  nSXa'   [        US-   U5       H  nX7   S:X  d  M  SXg'   M     X1   S:X  a  [        U5       H  nSXg'   M	     [        US-
  SS5       H  nX7   S:X  a    U$ SXg'   M     U$ s  snf s  snf )al  
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  sM    a E%'.."1%B'(B	E5	!	!-.-qd-.,-,aqT,-33A6	E3		()<== 	BA#'CCF1q5!_5C<CF  
u|qACF  1q5!R 5C<J  ! J/ /-s   D;/E c                    U n/ nSn[        UR                  S5      5      nSnSnSn[        U5       Hb  u  pU
S:X  a$  XS-
  U-  U-  -   SU-  X-
  U-
  -  U-  -   nUS-  nOUS:X  a
  WnXW-  nSnSnSnUS-  nUR                  [        U5      5        U
nMd     U(       a-  [	        U5      nSUS-
  US-
  -  -  nU Vs/ s H  oU-  PM	     nnU$ s  snf )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"8 T)B..TQX_1MPR1RRAAID3
AAID

58! & B	eai01"%&#Q5y#&J 's   <Cc                    [        U 5      n[        U5      nS/U-  nUSS n[        USSS2   5      nUS   nS[        U5      -  /U-  US'   SUS'   UnXV-  nSnSn	Sn
X:  aB  S[        X-  U	-   5      -  nX* /-  X-  /-   S/X)-
  S-
  -  -   X9'   XtU	'   U	S-  n	U
S-  n
X:  a  MB  [        U5      S:X  a  XC4$ USS  H  nS[        Xi-  X-   -  5      -  nX* U-  /-  XiU-  /-  -   S/X)-
  U-
  -  -   X9'   U(       + nU(       a	  X-   nXV-  nOUnXtU	'   U	nU	S-  n	Sn
X:  d  Me  S[        X-
  X-  -   5      -  nS/U-  X-
  U* /-  -   X-  /-   S/X)-
  S-
  -  -   X9'   XtU	'   U	S-  n	U
S-  n
X:  a  MN  M     XC4$ )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     s
    (
)CCA#'C
a&C	S1XB	QBDGm_q CFCF
AHBF	A	
B
'd27Q;''fX,saeai/@@A	Q
a ' 3x1}z!"gd26QV,--cEk]"Ru9+%55qurz8JJAHBAA	Qg$qv/00ES2XUF8 33rzlBaSAETUIEVVCFFFA!GB g ( :r6   c                 ~    / nUS   nU H/  n[        S [        X@5       5       5      nUR                  U5        M1     U$ )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   .   #    U  H  u  pX-  v   M     g 7fN ).0evvuvs      r   	<genexpr>&spectral_projection.<locals>.<genexpr>  s     5*Yc*s   )r   ra   r-   )rT   
eigenpairscoeffevectevr   s         r   spectral_projectionr     sB     EqME5#b*55Q  Lr6   c                    [        U 5      nUR                  5         / nSn[        U5      nUR                  5       nU(       aD  XT:  a  UR	                  U5        US-  nOUS-  nU(       a  UR                  5       nOSnU(       a  MD  U$ )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
<NN31HC1HC # Nr6   rh   c                     SUs=::  a  S::  d  O  [        S5      eS/n[        SU 5       H;  nUR                  5       U:  a  UR                  S5        M*  UR                  S5        M=     U$ )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   =  s]    ( KaK-..
B1a[;;=1IIcNIIcN	 
 Ir6   c                     S/S/U S-
  -  -   nX:  a  SX!'   U$ XU S-
  -  S-  :  a  [        S5      eU S-
  nU S-
  nXA:  a  SX#'   US-  nXC-  nXA:  a  M  XU-
  -
  nSX#'   U$ 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 	u	 	A;?>?? a%C
a%C
'q
 ' Sy/CBGIr6   c                     S/S/U S-
  -  -   nX:  a  SX!'   U$ XU S-
  -  S-  :  a  [        S5      eSX S-
  '   U S-
  nSnX1:  a  SX$'   X4-  nUS-  nX1:  a  M  X1:  a  SX#U-
  '   U$ r   r   )r   r   r#   r   r   s        r   left_d_threshold_sequencer     s     #!a%	 B 	u	 	A;?>?? B1uI
a%C
C
'
q ' w7Ir6      c                 &   [        U SS 5       VVs/ s H  u  pEUS:X  d  M  UPM     nnnUR                  5       U:  aa  UR                  U5      nUR                  [        U5      5      nXx-
  n	X:w  a-  X   S:X  a%  X	   S:X  a  SX'   SX'   SX	'   UR	                  U5        UR                  5       U:  aW  U(       aP  UR                  U5      n
UR                  U5      nX-   nU[        U 5      :  d  X   S:X  d  X:X  a  U $ SX
'   SX'   SX'   U $ s  snnf )aN  
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%8>AI<LQ%8EM{{}wU#;;uV}-%	 R\S%8R]c=QBJBLBMLL 
 {{}y U{{5)E*-SW
c 1\5RI
 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   