
    hu                     N   S r SSKrSSKrSSKJrJrJr  SSKJr  SSK	r
SSKJrJr  / SQr\R                  rS r SS jr\" S	5      \
R&                  " SS
S9SS j5       5       r\" S5      \
R&                  " SS
S9 SS j5       5       r\" S5      \
R&                  " SS
S9SS j5       5       r\
R&                  " SS
S9SS j5       r\
R&                  " SS
S9SS j5       r\
R&                  " SS
S9SS j5       r\" S5      \
R&                  " SS
S9SS j5       5       r " S S5      rg)zIGenerate graphs with a given degree sequence or expected degree sequence.    N)chaincombinationszip_longest)
itemgetter)py_random_staterandom_weighted_sample)configuration_modeldirected_configuration_modelexpected_degree_graphhavel_hakimi_graphdirected_havel_hakimi_graphdegree_sequence_treerandom_degree_sequence_graphc                 J    [        [        S [        U 5       5       5      5      $ )a:  Returns a list of degree-repeated node numbers.

``degree_sequence`` is a list of nonnegative integers representing
the degrees of nodes in a graph.

This function returns a list of node numbers with multiplicities
according to the given degree sequence. For example, if the first
element of ``degree_sequence`` is ``3``, then the first node number,
``0``, will appear at the head of the returned list three times. The
node numbers are assumed to be the numbers zero through
``len(degree_sequence) - 1``.

Examples
--------

>>> degree_sequence = [1, 2, 3]
>>> _to_stublist(degree_sequence)
[0, 1, 1, 2, 2, 2]

If a zero appears in the sequence, that means the node exists but
has degree zero, so that number will be skipped in the returned
list::

>>> degree_sequence = [2, 0, 1]
>>> _to_stublist(degree_sequence)
[0, 0, 2]

c              3   2   #    U  H  u  pU/U-  v   M     g 7fN ).0nds      P/var/www/html/env/lib/python3.13/site-packages/networkx/generators/degree_seq.py	<genexpr>_to_stublist.<locals>.<genexpr>5   s     F+E41sQw+Es   )listchaini	enumerate)degree_sequences    r   _to_stublistr      s    : F9_+EFFGG    c                    [        U 5      n[        R                  " XQ5      nUS:X  a  U$ U(       aM  [        XSS9n[	        U6 u  p[        U5      n
[        U	5      nUR                  U
5        UR                  U5        O4[        U 5      n[        U5      nUS-  nUR                  U5        USU XS pUR                  [	        X5      5        U$ )a  Helper function for generating either undirected or directed
configuration model graphs.

``deg_sequence`` is a list of nonnegative integers representing the
degree of the node whose label is the index of the list element.

``create_using`` see :func:`~networkx.empty_graph`.

``directed`` and ``in_deg_sequence`` are required if you want the
returned graph to be generated using the directed configuration
model algorithm. If ``directed`` is ``False``, then ``deg_sequence``
is interpreted as the degree sequence of an undirected graph and
``in_deg_sequence`` is ignored. Otherwise, if ``directed`` is
``True``, then ``deg_sequence`` is interpreted as the out-degree
sequence and ``in_deg_sequence`` as the in-degree sequence of a
directed graph.

.. note::

   ``deg_sequence`` and ``in_deg_sequence`` need not be the same
   length.

``seed`` is a random.Random or numpy.random.RandomState instance

This function returns a graph, directed if and only if ``directed``
is ``True``, generated according to the configuration model
algorithm. For more information on the algorithm, see the
:func:`configuration_model` or :func:`directed_configuration_model`
functions.

r   )	fillvalue   N)lennxempty_graphr   zipr   shuffleadd_edges_from)deg_sequencecreate_usingdirectedin_deg_sequenceseedr   Gpairsout_degin_degout_stublistin_stubliststublisthalfs                 r   _configuration_modelr6   8   s    D 	LA
q'AAv LQGu+#G,"6*\"[!- MAvX$,UdOXe_kS34Hr   r"   T)graphsreturns_graphc                    [        U 5      S-  S:w  a  Sn[        R                  " U5      e[        R                  " SU[        R                  S9nUR                  5       (       a  [        R                  " S5      e[        XUS9nU$ )ah  Returns a random graph with the given degree sequence.

The configuration model generates a random pseudograph (graph with
parallel edges and self loops) by randomly assigning edges to
match the given degree sequence.

Parameters
----------
deg_sequence :  list of nonnegative integers
    Each list entry corresponds to the degree of a node.
create_using : NetworkX graph constructor, optional (default MultiGraph)
    Graph type to create. If graph instance, then cleared before populated.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
G : MultiGraph
    A graph with the specified degree sequence.
    Nodes are labeled starting at 0 with an index
    corresponding to the position in deg_sequence.

Raises
------
NetworkXError
    If the degree sequence does not have an even sum.

See Also
--------
is_graphical

Notes
-----
As described by Newman [1]_.

A non-graphical degree sequence (not realizable by some simple
graph) is allowed since this function returns graphs with self
loops and parallel edges.  An exception is raised if the degree
sequence does not have an even sum.

This configuration model construction process can lead to
duplicate edges and loops.  You can remove the self-loops and
parallel edges (see below) which will likely result in a graph
that doesn't have the exact degree sequence specified.

The density of self-loops and parallel edges tends to decrease as
the number of nodes increases. However, typically the number of
self-loops will approach a Poisson distribution with a nonzero mean,
and similarly for the number of parallel edges.  Consider a node
with *k* stubs. The probability of being joined to another stub of
the same node is basically (*k* - *1*) / *N*, where *k* is the
degree and *N* is the number of nodes. So the probability of a
self-loop scales like *c* / *N* for some constant *c*. As *N* grows,
this means we expect *c* self-loops. Similarly for parallel edges.

References
----------
.. [1] M.E.J. Newman, "The structure and function of complex networks",
   SIAM REVIEW 45-2, pp 167-256, 2003.

Examples
--------
You can create a degree sequence following a particular distribution
by using the one of the distribution functions in
:mod:`~networkx.utils.random_sequence` (or one of your own). For
example, to create an undirected multigraph on one hundred nodes
with degree sequence chosen from the power law distribution:

>>> sequence = nx.random_powerlaw_tree_sequence(100, tries=5000)
>>> G = nx.configuration_model(sequence)
>>> len(G)
100
>>> actual_degrees = [d for v, d in G.degree()]
>>> actual_degrees == sequence
True

The returned graph is a multigraph, which may have parallel
edges. To remove any parallel edges from the returned graph:

>>> G = nx.Graph(G)

Similarly, to remove self-loops:

>>> G.remove_edges_from(nx.selfloop_edges(G))

r"   r   =Invalid degree sequence: sum of degrees must be even, not odddefaultz#not implemented for directed graphs)r-   )sumr$   NetworkXErrorr%   
MultiGraphis_directedNetworkXNotImplementedr6   )r)   r*   r-   msgr.   s        r   r	   r	   }   sq    t <1!Ms##
q,>A}}''(MNN\48AHr      c                     [        U 5      [        U5      :w  a  Sn[        R                  " U5      eUc  [        R                  n[	        UUSU US9nSnU$ )a
  Returns a directed_random graph with the given degree sequences.

The configuration model generates a random directed pseudograph
(graph with parallel edges and self loops) by randomly assigning
edges to match the given degree sequences.

Parameters
----------
in_degree_sequence :  list of nonnegative integers
   Each list entry corresponds to the in-degree of a node.
out_degree_sequence :  list of nonnegative integers
   Each list entry corresponds to the out-degree of a node.
create_using : NetworkX graph constructor, optional (default MultiDiGraph)
    Graph type to create. If graph instance, then cleared before populated.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
G : MultiDiGraph
    A graph with the specified degree sequences.
    Nodes are labeled starting at 0 with an index
    corresponding to the position in deg_sequence.

Raises
------
NetworkXError
    If the degree sequences do not have the same sum.

See Also
--------
configuration_model

Notes
-----
Algorithm as described by Newman [1]_.

A non-graphical degree sequence (not realizable by some simple
graph) is allowed since this function returns graphs with self
loops and parallel edges.  An exception is raised if the degree
sequences does not have the same sum.

This configuration model construction process can lead to
duplicate edges and loops.  You can remove the self-loops and
parallel edges (see below) which will likely result in a graph
that doesn't have the exact degree sequence specified.  This
"finite-size effect" decreases as the size of the graph increases.

References
----------
.. [1] Newman, M. E. J. and Strogatz, S. H. and Watts, D. J.
   Random graphs with arbitrary degree distributions and their applications
   Phys. Rev. E, 64, 026118 (2001)

Examples
--------
One can modify the in- and out-degree sequences from an existing
directed graph in order to create a new directed graph. For example,
here we modify the directed path graph:

>>> D = nx.DiGraph([(0, 1), (1, 2), (2, 3)])
>>> din = list(d for n, d in D.in_degree())
>>> dout = list(d for n, d in D.out_degree())
>>> din.append(1)
>>> dout[0] = 2
>>> # We now expect an edge from node 0 to a new node, node 3.
... D = nx.directed_configuration_model(din, dout)

The returned graph is a directed multigraph, which may have parallel
edges. To remove any parallel edges from the returned graph:

>>> D = nx.DiGraph(D)

Similarly, to remove self-loops:

>>> D.remove_edges_from(nx.selfloop_edges(D))

z8Invalid degree sequences: sequences must have equal sumsT)r+   r,   r-   z.directed configuration_model {} nodes {} edges)r=   r$   r>   MultiDiGraphr6   )in_degree_sequenceout_degree_sequencer*   r-   rB   r.   names          r   r
   r
      sc    h #&9"::Hs##*	A <DHr      c           	      D   [        U 5      n[        R                  " U5      nUS:X  d  [        U 5      S:X  a  U$ S[	        U 5      -  n[        [        U 5      [        S5      SS9n[        U5       VVV	s0 s H
  u  nu  pXx_M     n
nnn	U VV	s/ s H  u  pU	PM	     nnn	UnU(       d  US-  n[        U5       H  nUn	U(       d  U	S-  n	X   U-  n[        X   U-  S5      nX:  d  M0  US:  d  M8  US:w  aA  UR                  5       nU	[        R                  " [        R                  " USU-
  5      5      -  n	X:  aE  [        X   U-  S5      nUR                  5       UU-  :  a  UR                  X   X   5        U	S-  n	UnX:  d  M  US:  a  M  M     U$ s  sn	nnf s  sn	nf )u  Returns a random graph with given expected degrees.

Given a sequence of expected degrees $W=(w_0,w_1,\ldots,w_{n-1})$
of length $n$ this algorithm assigns an edge between node $u$ and
node $v$ with probability

.. math::

   p_{uv} = \frac{w_u w_v}{\sum_k w_k} .

Parameters
----------
w : list
    The list of expected degrees.
selfloops: bool (default=True)
    Set to False to remove the possibility of self-loop edges.
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
Graph

Examples
--------
>>> z = [10 for i in range(100)]
>>> G = nx.expected_degree_graph(z)

Notes
-----
The nodes have integer labels corresponding to index of expected degrees
input sequence.

The complexity of this algorithm is $\mathcal{O}(n+m)$ where $n$ is the
number of nodes and $m$ is the expected number of edges.

The model in [1]_ includes the possibility of self-loop edges.
Set selfloops=False to produce a graph without self loops.

For finite graphs this model doesn't produce exactly the given
expected degree sequence.  Instead the expected degrees are as
follows.

For the case without self loops (selfloops=False),

.. math::

   E[deg(u)] = \sum_{v \ne u} p_{uv}
            = w_u \left( 1 - \frac{w_u}{\sum_k w_k} \right) .


NetworkX uses the standard convention that a self-loop edge counts 2
in the degree of a node, so with self loops (selfloops=True),

.. math::

   E[deg(u)] =  \sum_{v \ne u} p_{uv}  + 2 p_{uu}
            = w_u \left( 1 + \frac{w_u}{\sum_k w_k} \right) .

References
----------
.. [1] Fan Chung and L. Lu, Connected components in random graphs with
   given expected degree sequences, Ann. Combinatorics, 6,
   pp. 125-145, 2002.
.. [2] Joel Miller and Aric Hagberg,
   Efficient generation of networks with given expected degrees,
   in Algorithms and Models for the Web-Graph (WAW 2011),
   Alan Frieze, Paul Horn, and Paweł Prałat (Eds), LNCS 6732,
   pp. 115-126, 2011.
r   rI   T)keyreverse)r#   r$   r%   maxr=   sortedr   r   rangeminrandommathfloorlogadd_edge)wr-   	selfloopsr   r.   rhoordercuvmappingseqlastfactorprqs                    r   r   r   K  s   T 	AA
qA 	AvQ1
c!f*C 9Q<Z]DAE%.u%56%5	6Aqt%5G6
1C
D	4[FA##eAAvKKMTZZAE 233u+;;=1q5(JJwz7:6Q eA   H+ 7
s   3FFc                    [         R                  " U 5      (       d  [         R                  " S5      e[        U 5      n[         R                  " X!5      nUR                  5       (       a  [         R                  " S5      e[        U5       Vs/ s H  n/ PM     nnSu  pgnU  H1  n	U	S:  d  M  XY   R                  U5        [        Xi5      Xy-   US-   pnM3     US:X  a  U$ S/US-   -  n
US:  Ga  [        XV   5      S:X  a  US-  n[        XV   5      S:X  a  M  XhS-
  :  a  [         R                  " S5      eXV   R                  5       nUS-  nSnUn[        U5       Hj  n[        X]   5      S:X  a  US-  n[        X]   5      S:X  a  M  X]   R                  5       nUR                  X5        US-  nUS:  d  M\  US-
  U4X'   US-  nMl     [        U5       H"  nX   u  nnX_   R                  U5        US-  nM$     US:  a  GM  U$ s  snf )a2  Returns a simple graph with given degree sequence constructed
using the Havel-Hakimi algorithm.

Parameters
----------
deg_sequence: list of integers
    Each integer corresponds to the degree of a node (need not be sorted).
create_using : NetworkX graph constructor, optional (default=nx.Graph)
    Graph type to create. If graph instance, then cleared before populated.
    Directed graphs are not allowed.

Raises
------
NetworkXException
    For a non-graphical degree sequence (i.e. one
    not realizable by some simple graph).

Notes
-----
The Havel-Hakimi algorithm constructs a simple graph by
successively connecting the node of highest degree to other nodes
of highest degree, resorting remaining nodes by degree, and
repeating the process. The resulting graph has a high
degree-associativity.  Nodes are labeled 1,.., len(deg_sequence),
corresponding to their position in deg_sequence.

The basic algorithm is from Hakimi [1]_ and was generalized by
Kleitman and Wang [2]_.

References
----------
.. [1] Hakimi S., On Realizability of a Set of Integers as
   Degrees of the Vertices of a Linear Graph. I,
   Journal of SIAM, 10(3), pp. 496-506 (1962)
.. [2] Kleitman D.J. and Wang D.L.
   Algorithms for Constructing Graphs and Digraphs with Given Valences
   and Factors  Discrete Mathematics, 6(1), pp. 79-88 (1973)
zInvalid degree sequencez!Directed graphs are not supportedr   r   r   r   rI   r   r   zNon-graphical integer sequence)r$   is_graphicalr>   r#   r%   r@   rO   appendrM   poprU   )r)   r*   ra   r.   inum_degsdmaxdsumr   r   modstubssourcemslenktargetstubval
stubtargets                    r   r   r     s	   P ??<((899LA
q'A}}BCC!!H%HqHH%MDq5Kq!L$(AED	  	Avx4!8$H
a%(.!Q&AID (.!Q& a%<""#CDD ##%	QtAhk"a'Q hk"a'[__&FJJv&FA1u#$q5&/
  uA$,K!Wj$$Z0FA 3 a%< HW &s   Hc                 &   [         R                  R                  U 5      n [         R                  R                  U5      nSu  p4[        U 5      [        U5      pe[	        XV5      n[         R
                  " Xr[         R                  S9nUS:X  a  U$ Sn	/ / p[        U5       H  nSu  pX:  a  X   nX:  a  X   nUS:  d  US:  a  [         R                  " S5      eX=-   XN-   [	        X5      pnUS:  a  U
R                  SU-  SU-  U45        Mo  US:  d  Mw  UR                  SU-  U45        M     X4:w  a  [         R                  " S5      e[        R                  " U
5        [        R                  " U5        S/U	S-   -  nU
(       Ga  [        R                  " U
5      u  nnnUS-  nU[        U
5      [        U5      -   :  a  [         R                  " S	5      eSn[        U5       H  nU(       a5  U
(       a  U
S   S   US   S   :  a  [        R                  " U5      u  nnSnO[        R                  " U
5      u  nnnUS:X  a  [         R                  " S	5      eUR                  UU5        US-   S:  d  US:  d  M  US-   UU4UU'   US-  nM     [        U5       HI  nUU   nUS   S:  a  [        R                  " U
U5        M*  [        R                  " UUS   US
   45        MK     US:  a  [        R                  " UUU45        U
(       a  GM  U$ )a  Returns a directed graph with the given degree sequences.

Parameters
----------
in_deg_sequence :  list of integers
    Each list entry corresponds to the in-degree of a node.
out_deg_sequence : list of integers
    Each list entry corresponds to the out-degree of a node.
create_using : NetworkX graph constructor, optional (default DiGraph)
    Graph type to create. If graph instance, then cleared before populated.

Returns
-------
G : DiGraph
    A graph with the specified degree sequences.
    Nodes are labeled starting at 0 with an index
    corresponding to the position in deg_sequence

Raises
------
NetworkXError
    If the degree sequences are not digraphical.

See Also
--------
configuration_model

Notes
-----
Algorithm as described by Kleitman and Wang [1]_.

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)
rf   r;   r   z;Invalid degree sequences. Sequence values must be positive.z9Invalid degree sequences. Sequences must have equal sums.re   rI   z Non-digraphical integer sequencer"   )r$   utilsmake_list_of_intsr#   rM   r%   DiGraphrO   r>   rh   heapqheapifyheappoprU   heappush)r,   out_deg_sequencer*   suminsumoutninnoutmaxnr.   maxinstubheapzeroheapr   r1   r0   rn   freeoutfreeinrr   rp   rj   stubout
stubsourcestubinstubs                            r   r   r     s   N hh00AOxx112BC MEO$c*:&;s>D
t2::>AqyERh4[8&)G7$'FA:1""M   %~v/?UASuA:OOR'\2;:;q[OOR'\1-.  G
 	
 
MM(	MM({eai(H
$)MM($;!&&"CMCM11""#EFF vAXa[^hqk!n-L(-h(?%*05h0G-&*!|&&'IJJJJz6*{Q&1*#*Q;
"C
  uAA;DAw{x.x$q'47);<  Q;NN8gv%67? (B Hr   c                    [        U 5      nUS-  S:w  a  Sn[        R                  " U5      e[        U 5      US-  -
  S:w  a  Sn[        R                  " U5      e[        R                  " SU5      nUR                  5       (       a  [        R                  " S5      e[        S U  5       SS	9n[        U5      S-   n[        R                  " U[        U5      5        Un[        SUS-
  5       H@  nUR                  5       S-
  n	[        XwU	-   5       H  n
UR                  X5        M     Xy-  nMB     [        U5      [        U 5      :  a  UR                  S5        U$ )
zMake a tree for the given degree sequence.

A tree has #nodes-#edges=1 so
the degree sequence must have
len(deg_sequence)-sum(deg_sequence)/2=1
r"   r   r:   rI   zbInvalid degree sequence: tree must have number of nodes equal to one less than the number of edgeszDirected Graph not supportedc              3   4   #    U  H  oS :  d  M
  Uv   M     g7f)rI   Nr   )r   ss     r   r   'degree_sequence_tree.<locals>.<genexpr>  s     3\U!!\s   		T)rL   )r=   r$   r>   r#   r%   r@   rN   add_pathrO   ri   rU   remove_node)r)   r*   
degree_sumrB   r.   degr   r_   ro   nedgesrr   s              r   r   r     s9    \"JA~Ms##
<:?*a/4 	 s##
q,'A}}=>>
 3\3T
BC 	C1AKK58D 1q5/QD-0FJJv& 1	 " 1vL!!	aHr   c                     [        X5      n[        U5       H  n UR                  5       s  $    [        R
                  " SU S35      e! [        R                   a     MI  f = f)aA  Returns a simple random graph with the given degree sequence.

If the maximum degree $d_m$ in the sequence is $O(m^{1/4})$ then the
algorithm produces almost uniform random graphs in $O(m d_m)$ time
where $m$ is the number of edges.

Parameters
----------
sequence :  list of integers
    Sequence of degrees
seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
tries : int, optional
    Maximum number of tries to create a graph

Returns
-------
G : Graph
    A graph with the specified degree sequence.
    Nodes are labeled starting at 0 with an index
    corresponding to the position in the sequence.

Raises
------
NetworkXUnfeasible
    If the degree sequence is not graphical.
NetworkXError
    If a graph is not produced in specified number of tries

See Also
--------
is_graphical, configuration_model

Notes
-----
The generator algorithm [1]_ is not guaranteed to produce a graph.

References
----------
.. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi,
   A sequential algorithm for generating random graphs.
   Algorithmica, Volume 58, Number 4, 860-910,
   DOI: 10.1007/s00453-009-9340-1

Examples
--------
>>> sequence = [1, 2, 2, 3]
>>> G = nx.random_degree_sequence_graph(sequence, seed=42)
>>> sorted(d for n, d in G.degree())
[1, 2, 2, 3]
zfailed to generate graph in z tries)DegreeSequenceRandomGraphrO   generater$   NetworkXUnfeasibler>   )sequencer-   triesDSRGtry_ns        r   r   r     se    n %X4Du	==?" 
 

9%G
HH $$ 		s   A		A! A!c                   N    \ rS rSrS rS rSS jrS rS rS r	S	 r
S
 rS rSrg)r   i  c                 0   [         R                  " U5      (       d  [         R                  " S5      eX l        [	        U5      U l        [        U R
                  5      S-  U l         [        U R
                  5      U l	        g ! [         a
    SU l	         g f = f)Nz degree sequence is not graphicalg       @r   )r$   rg   r   rngr   degreer=   mrM   rl   
ValueError)selfr   r   s      r   __init__"DegreeSequenceRandomGraph.__init__  ss    v&&''(JKK6lT[[!C'	DKK(DI 	DI	s   &B BBc                    [        [        U R                  5      5      U l        [        R
                  " 5       U l        U R                  R                  U R                  5        [        U R                  R                  5       5       H  u  pUS:X  d  M  U R                  U	 M     [        U R                  5      S:  a0  U R                  5         U R                  5         U R                  5         U R                  $ )Nr   )dictr   r   remaining_degreer$   Graphgraphadd_nodes_fromr   itemsr#   phase1phase2phase3)r   r   r   s      r   r   "DegreeSequenceRandomGraph.generate  s     $Yt{{%; <XXZ


!!$"7"78..4467DAAv))!, 8 t$$%)KKMKKMKKMzzr   Nc                 ^   Ub  UR                  X5        U R                  U   S:X  a"  U R                  U	 Ub  UR                  U5        OU R                  U==   S-  ss'   U R                  U   S:X  a#  U R                  U	 Ub  UR                  U5        g g U R                  U==   S-  ss'   g )NrI   )remove_edger   r   )r   r[   r\   	aux_graphs       r   update_remaining*DegreeSequenceRandomGraph.update_remaining  s     !!!'  #q(%%a($%%a(!!!$)$  #q(%%a($%%a( % !!!$)$r   c                 f    SU R                   U   U R                   U   -  SU R                  -  -  -
  $ )NrI   g      @)r   r   )r   r[   r\   s      r   ra   DegreeSequenceRandomGraph.p&  s/    4;;q>DKKN2cDFFlCCCr   c                     [        U R                  R                  5       5      S-  nU R                  U   U R                  U   -  U-  $ Nr"   )rM   r   values)r   r[   r\   norms       r   rc   DegreeSequenceRandomGraph.q*  sF    4((//12a7$$Q'$*?*?*BBTIIr   c                 r   ^ ^ [        T R                  5      n[        U5      m[        U U4S jU 5       5      $ )zsReturns True if and only if an arbitrary remaining node can
potentially be joined with some other remaining node.

c              3   F   >#    U  H  oTR                   T   ;  v   M     g 7fr   )r   )r   r\   r   r[   s     r   r   :DegreeSequenceRandomGraph.suitable_edge.<locals>.<genexpr>6  s     95aDJJqM)5s   !)iterr   nextany)r   nodesr[   s   ` @r   suitable_edge'DegreeSequenceRandomGraph.suitable_edge/  s/    
 T**+K95999r   c                    U R                   n[        UR                  5       5      SU R                  S-  -  :  a  [	        [        USU R                  5      5      u  p#U R                  R                  X#5      (       a  Mq  U R                  R                  5       U R                  X#5      :  a,  U R                  R                  X#5        U R                  X#5        [        UR                  5       5      SU R                  S-  -  :  a  M  g g r   )r   r=   r   rl   rN   r   r   r   has_edgerQ   ra   rU   r   )r   rem_degr[   r\   s       r   r    DegreeSequenceRandomGraph.phase18  s    '''.."#q499a<'770!TXXFGDAzz""1((xx 466!</

##A)%%a+ '.."#q499a<'77r   c                 B   U R                   nU R                  n[        U5      SU R                  -  :  a   [	        UR                  [        UR                  5       5      S5      5      u  p4U R                  R                  X45      (       a  MW  UR                  5       U R                  X45      :  a  OM|  UR                  5       U R                  X45      :  a,  U R                  R                  X45        U R                  X45        [        U5      SU R                  -  :  a  M  g g r   )r   r   r#   rl   rN   sampler   keysr   r   rQ   rc   ra   rU   r   )r   remaining_degr   r[   r\   s        r   r    DegreeSequenceRandomGraph.phase2C  s    --hh- A		M1cjjm.@.@.B)CQGH::&&q,,::<$&&,.  zz|dffQl*

##A)%%a+ - A		M1r   c           
         [        U R                  S5      n[        R                  " U VVs/ s H*  u  p#U R                  R                  X#5      (       a  M'  X#4PM,     snn5      nU R                  nU R                  (       a  U R                  5       (       d  [        R                  " S5      e [        UR                  [        UR                  5       5      5      5      u  p#UR                  5       U R                  X#5      :  a  OMY  UR                  5       U R                  X#5      :  a+  U R                  R!                  X#5        U R#                  X#US9  U R                  (       a  M  g g s  snnf )Nr"   zno suitable edges left)r   )r   r   r$   r   r   r   r   r   r   rN   choicer   edgesrQ   rc   ra   rU   r   )r   potential_edgesr[   r\   Hr   s         r   r    DegreeSequenceRandomGraph.phase3R  s   &t'<'<a@HH"1S/9L9LQ9RVaV/S
 hh##%%''++,DEEcjjaggi9:::<$&&,.  zz|dffQl*

##A)%%aa%8 ### Ts   &E*
E*
)r   rl   r   r   r   r   r   )__name__
__module____qualname____firstlineno__r   r   r   ra   rc   r   r   r   r   __static_attributes__r   r   r   r   r     s1    
"*$DJ
:	,,9r   r   )FNN)NN)NTr   )N
   )__doc__rz   rR   	itertoolsr   r   r   operatorr   networkxr$   networkx.utilsr   r   __all__from_iterabler   r   r6   _dispatchabler	   r
   r   r   r   r   r   r   r   r   r   <module>r      s   O   6 6   B 
		HB LPBJ T2b 3 bJ T2EIb 3 bJ T2i 3 iX T2Y 3Yx T2l 3l^ T2* 3*Z T2;I 3 ;I|o9 o9r   