
    wgu                     h   d Z ddlZddlZddlmZmZmZ ddlmZ ddl	Z
ddlmZmZ g dZej                  Zd Z	 ddZ ed	       e
j&                  dd
      dd              Z ed       e
j&                  dd
      	 dd              Z ed       e
j&                  dd
      dd              Z e
j&                  dd
      dd       Z e
j&                  dd
      dd       Z e
j&                  dd
      dd       Z ed       e
j&                  dd
      dd              Z G d d      Zy)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    t        t        d t        |       D                    S )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   .   K   | ]  \  }}|g|z    y wN ).0nds      c/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/generators/degree_seq.py	<genexpr>z_to_stublist.<locals>.<genexpr>5   s     F41asQwFs   )listchaini	enumerate)degree_sequences    r   _to_stublistr      s    : F9_+EFFGG    c                    t        |       }t        j                  ||      }|dk(  r|S |rRt        | |d      }t	        | \  }}	t        |      }
t        |	      }|j                  |
       |j                  |       n6t        |       }t        |      }|dz  }|j                  |       |d| ||d }}
|j                  t	        |
|             |S )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_modelr5   8   s    D 	LA
q,'AAv L/QGu+#G,"6*\"[!- MAvX$,UdOXde_kS{34Hr   r!   T)graphsreturns_graphc                    t        |       dz  dk7  rd}t        j                  |      t        j                  d|t        j                        }|j                         rt        j                  d      t        | ||      }|S )a|  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NetworkXNotImplementedr5   )r(   r)   r,   msgr-   s        r   r	   r	   }   sq    t <1!Ms##
q,>A}}''(MNN\148AHr      c                     t        |       t        |      k7  rd}t        j                  |      |t        j                  }t	        ||d| |      }d}|S )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=   MultiDiGraphr5   )in_degree_sequenceout_degree_sequencer)   r,   rA   r-   names          r   r
   r
      sc    h #&9"::Hs##*	A <DHr      c           	         t        |       }t        j                  |      }|dk(  st        |       dk(  r|S dt	        |       z  }t        t        |       t        d      d      }t        |      D 	ci c]  \  }\  }}	|| }
}}}	|D 	cg c]  \  }}	|		 }}}	|}|s|dz  }t        |      D ]  }|}	|s|	dz  }	||   |z  }t        ||	   |z  d      }|	|k  s,|dkD  s2|dk7  r?|j                         }|	t        j                  t        j                  |d|z
              z  }	|	|k  rGt        ||	   |z  d      }|j                         ||z  k  r|j                  |
|   |
|	          |	dz  }	|}|	|k  s|dkD  r |S c c}	}}w c c}	}w )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   rH   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6	6Aqq!t6G6
A1
C
D	4[ FAQ#A#!eAAvKKMTZZAE 2331uA+;;=1q5(JJwqz71:6Q !eA  H+ 7
s   3FFc                    t        j                  |       st        j                  d      t        |       }t        j                  ||      }|j                         rt        j                  d      t        |      D cg c]  }g  }}d\  }}}| D ]2  }	|	dkD  s	||	   j                  |       t        ||	      ||	z   |dz   }}}4 |dk(  r|S dg|dz   z  }
|dkD  rt        ||         dk(  r|dz  }t        ||         dk(  r||dz
  kD  rt        j                  d      ||   j                         }|dz  }d}|}t        |      D ]i  }t        ||         dk(  r|dz  }t        ||         dk(  r||   j                         }|j                  ||       |dz  }|dkD  s[|dz
  |f|
|<   |dz  }k t        |      D ]#  }|
|   \  }}||   j                  |       |dz  }% |dkD  r|S c c}w )a  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   rH   r   r   zNon-graphical integer sequence)r#   is_graphicalr=   r"   r$   r?   rN   appendrL   poprT   )r(   r)   r`   r-   inum_degsdmaxdsumr   r   modstubssourcemslenktargetstubval
stubtargets                    r   r   r     s2   P ??<(899LA
q,'A}}BCC!!H%q%H%MD$ :q5QKq!aL$(AE$D	: 	Avx4!8$H
a%(4.!Q&AID (4.!Q& !a%<""#CDD $##%	Qt 	Ahqk"a'Q hqk"a'a[__&FJJvv&FA1u#$q5&/
	 u 	A$,QK!WjW$$Z0FA	3 a%< HW &s   >	G.c                    t         j                  j                  |       } t         j                  j                  |      }d\  }}t        |       t        |      }}t	        ||      }t        j
                  ||t         j                        }|dk(  r|S d}	g g }}
t        |      D ]  }d\  }}||k  r||   }||k  r| |   }|dk  s|dk  rt        j                  d      ||z   ||z   t	        |	|      }	}}|dkD  r|
j                  d|z  d|z  |f       q|dkD  sw|j                  d|z  |f        ||k7  rt        j                  d      t        j                  |
       t        j                  |       dg|	dz   z  }|
ret        j                  |
      \  }}}|dz  }|t        |
      t        |      z   kD  rt        j                  d	      d}t        |      D ]  }|r.|
r|
d   d   |d   d   kD  rt        j                  |      \  }}d}nt        j                  |
      \  }}}|dk(  rt        j                  d	      |j                  ||       |dz   dk  s|dk  s|dz   ||f||<   |dz  } t        |      D ]D  }||   }|d   dk  rt        j                  |
|       't        j                  ||d   |d
   f       F |dk  rt        j                  |||f       |
re|S )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)
    re   r:   r   z;Invalid degree sequences. Sequence values must be positive.z9Invalid degree sequences. Sequences must have equal sums.rd   rH   z Non-digraphical integer sequencer!   )r#   utilsmake_list_of_intsr"   rL   r$   DiGraphrN   r=   rg   heapqheapifyheappoprT   heappush)r+   out_deg_sequencer)   suminsumoutninnoutmaxnr-   maxinstubheapzeroheapr   r0   r/   rm   freeoutfreeinrq   ro   ri   stubout
stubsourcestubinstubs                            r   r   r     s   N hh00AOxx112BC ME6O$c*:&;CsD>D
t\2::>AqyERhH4[ /t8&q)Gs7$Q'FA:1""M   %v~v/?UFASuv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                    t        |       }|dz  dk7  rd}t        j                  |      t        |       |dz  z
  dk7  rd}t        j                  |      t        j                  d|      }|j                         rt        j                  d      t        d | D        d	      }t        |      dz   }t        j                  |t        |             |}t        d|dz
        D ]@  }|j                         dz
  }	t        |||	z         D ]  }
|j                  ||
        ||	z  }B t        |      t        |       kD  r|j                  d       |S )
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   r9   rH   zbInvalid degree sequence: tree must have number of nodes equal to one less than the number of edgeszDirected Graph not supportedc              3   ,   K   | ]  }|d kD  s	|  yw)rH   Nr   )r   ss     r   r   z'degree_sequence_tree.<locals>.<genexpr>  s     3QU!3s   
T)rK   )r<   r#   r=   r"   r$   r?   rM   add_pathrN   rh   rT   remove_node)r(   r)   
degree_sumrA   r-   degr   r^   rn   nedgesrq   s              r   r   r     sG    \"JA~Ms##
<:?*a/4 	 s##
q,'A}}=>>
 3\3T
BC 	C1AKK58D 1q5/ QD$-0 	'FJJvv&	'	 1vL!!	aHr   c                     t        | |      }t        |      D ]  }	 |j                         c S  t        j
                  d| d      # t        j                  $ r Y Ew xY w)a  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)DegreeSequenceRandomGraphrN   generater#   NetworkXUnfeasibler=   )sequencer,   triesDSRGtry_ns        r   r   r     sl    n %Xt4Du 	==?"
 

9%G
HH $$ 		s   AAAc                   D    e Zd Zd Zd ZddZd Zd Zd Zd Z	d	 Z
d
 Zy)r   c                 "   t        j                  |      st        j                  d      || _        t	        |      | _        t        | j
                        dz  | _        	 t        | j
                        | _	        y # t        $ r
 d| _	        Y y w xY w)Nz degree sequence is not graphicalg       @r   )r#   rf   r   rngr   degreer<   mrL   rk   
ValueError)selfr   r   s      r   __init__z"DegreeSequenceRandomGraph.__init__  sr    v&''(JKK6lT[[!C'	DKK(DI 	DI	s    A; ;BBc                    t        t        | j                              | _        t	        j
                         | _        | j                  j                  | j                         t        | j                  j                               D ]  \  }}|dk(  s| j                  |=  t        | j                        dkD  r0| j                          | j                          | j                          | j                  S )Nr   )dictr   r   remaining_degreer#   Graphgraphadd_nodes_fromr   itemsr"   phase1phase2phase3)r   r   r   s      r   r   z"DegreeSequenceRandomGraph.generate  s     $Yt{{%; <XXZ


!!$"7"78..4467 	-DAqAv))!,	- t$$%)KKMKKMKKMzzr   Nc                 V   ||j                  ||       | j                  |   dk(  r!| j                  |= |)|j                  |       n| j                  |xx   dz  cc<   | j                  |   dk(  r"| j                  |= ||j                  |       y y | j                  |xx   dz  cc<   y )NrH   )remove_edger   r   )r   rZ   r[   	aux_graphs       r   update_remainingz*DegreeSequenceRandomGraph.update_remaining  s     !!!Q'  #q(%%a($%%a(!!!$)$  #q(%%a($%%a( % !!!$)$r   c                 f    d| j                   |   | j                   |   z  d| j                  z  z  z
  S )NrH   g      @)r   r   )r   rZ   r[   s      r   r`   zDegreeSequenceRandomGraph.p&  s/    4;;q>DKKN2cDFFlCCCr   c                     t        | j                  j                               dz  }| j                  |   | j                  |   z  |z  S Nr!   )rL   r   values)r   rZ   r[   norms       r   rb   zDegreeSequenceRandomGraph.q*  sF    4((//12a7$$Q'$*?*?*BBTIIr   c                 p     t         j                        }t        |      t         fd|D              S )zReturns True if and only if an arbitrary remaining node can
        potentially be joined with some other remaining node.

        c              3   @   K   | ]  }|j                      v  y wr   )r   )r   r[   r   rZ   s     r   r   z:DegreeSequenceRandomGraph.suitable_edge.<locals>.<genexpr>6  s     9a1DJJqM)9s   )iterr   nextany)r   nodesrZ   s   ` @r   suitable_edgez'DegreeSequenceRandomGraph.suitable_edge/  s/    
 T**+K95999r   c                    | j                   }t        |j                               d| j                  dz  z  k\  rt	        t        |d| j                              \  }}| j                  j                  ||      rl| j                  j                         | j                  ||      k  r.| j                  j                  ||       | j                  ||       t        |j                               d| j                  dz  z  k\  ry y r   )r   r<   r   rk   rM   r   r   r   has_edgerP   r`   rT   r   )r   rem_degrZ   r[   s       r   r   z DegreeSequenceRandomGraph.phase18  s    '''.."#q499a<'770!TXXFGDAqzz""1a(xx 466!Q</

##Aq)%%a+ '.."#q499a<'77r   c                 6   | j                   }| j                  }t        |      d| j                  z  k\  r	 t	        |j                  t        |j                               d            \  }}| j                  j                  ||      rS|j                         | j                  ||      k  rnw|j                         | j                  ||      k  r.| j                  j                  ||       | j                  ||       t        |      d| j                  z  k\  ry y r   )r   r   r"   rk   rM   sampler   keysr   r   rP   rb   r`   rT   r   )r   remaining_degr   rZ   r[   s        r   r   z DegreeSequenceRandomGraph.phase2C  s    --hh- A		M1cjjm.@.@.B)CQGH1::&&q!,::<$&&A,.  zz|dffQl*

##Aq)%%a+ - A		M1r   c           
         t        | j                  d      }t        j                  |D cg c]&  \  }}| j                  j                  ||      r#||f( c}}      }| j                  }| j                  r| j                         st        j                  d      	 t        |j                  t        |j                                           \  }}|j                         | j                  ||      k  rnY|j                         | j                  ||      k  r0| j                  j!                  ||       | j#                  |||       | j                  ry y c c}}w )Nr!   zno suitable edges left)r   )r   r   r#   r   r   r   r   r   r   rM   choicer   edgesrP   rb   r`   rT   r   )r   potential_edgesrZ   r[   Hr   s         r   r   z DegreeSequenceRandomGraph.phase3R  s   &t'<'<a@HH"1SA9L9LQPQ9RaVS
 hh##%%'++,DEEcjjaggi9:1::<$&&A,.  zz|dffQl*

##Aq)%%aa%8 ## Ts   $E
E
r   )__name__
__module____qualname__r   r   r   r`   rb   r   r   r   r   r   r   r   r   r     s1    
"*$DJ
:	,,9r   r   )FNN)NN)NTr   )N
   )__doc__ry   rQ   	itertoolsr   r   r   operatorr   networkxr#   networkx.utilsr   r   __all__from_iterabler   r   r5   _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   