
    h9                     F   S r SSKrSSKrSSKJr  / SQr\R                  R                  S5      \" S5      \R                  " SSS9S	S
SS.S j5       5       5       r
\" S5      \R                  " SSS9SS j5       5       r\" S5      \R                  " SS9SS j5       5       rg)zSwap edges in a graph.    N)py_random_state)double_edge_swapconnected_double_edge_swapdirected_edge_swap
undirected   T)mutates_inputreturns_graph   d   )nswap	max_triesseedc                   X:  a  [         R                  " S5      e[        U 5      S:  a  [         R                  " S5      e[        U R                  5      S:  a  [         R                  " S5      eSnSn[	        U R                  5       6 u  pg[         R                  R                  U5      n[         R                  R                  n	XQ:  Ga  U	" SXS9S   n
Xj   nUS-  nXB:  a  S	U S
U S3n[         R                  " U5      eU R                  U5      S:X  a  MT  UR                  [        U R                  U   5      5      nX:X  a  M  U R                  U5      S:X  a  M  UR                  [        U R                  U   5      5      nX:X  a  M  U R                  U5      S:X  a  M  UR                  [        U R                  U   5      5      nX:X  a  GM  XR                  U   ;  a  XR                  U   ;  a}  XR                  U   ;  ak  U R                  X5        U R                  X5        U R                  X5        U R                  X5        U R                  X5        U R                  X5        US-  nXQ:  a  GM  U $ )us  Swap three edges in a directed graph while keeping the node degrees fixed.

A directed edge swap swaps three edges such that a -> b -> c -> d becomes
a -> c -> b -> d. This pattern of swapping allows all possible states with the
same in- and out-degree distribution in a directed graph to be reached.

If the swap would create parallel edges (e.g. if a -> c already existed in the
previous example), another attempt is made to find a suitable trio of edges.

Parameters
----------
G : DiGraph
   A directed graph

nswap : integer (optional, default=1)
   Number of three-edge (directed) swaps to perform

max_tries : integer (optional, default=100)
   Maximum number of attempts to swap edges

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
G : DiGraph
   The graph after the edges are swapped.

Raises
------
NetworkXError
    If `G` is not directed, or
    If nswap > max_tries, or
    If there are fewer than 4 nodes or 3 edges in `G`.
NetworkXAlgorithmError
    If the number of swap attempts exceeds `max_tries` before `nswap` swaps are made

Notes
-----
Does not enforce any connectivity constraints.

The graph G is modified in place.

A later swap is allowed to undo a previous swap.

References
----------
.. [1] Erdős, Péter L., et al. “A Simple Havel-Hakimi Type Algorithm to Realize
       Graphical Degree Sequences of Directed Graphs.” ArXiv:0905.4913 [Math],
       Jan. 2010. https://doi.org/10.48550/arXiv.0905.4913.
       Published  2010 in Elec. J. Combinatorics (17(1)). R66.
       http://www.combinatorics.org/Volume_17/PDF/v17i1r66.pdf
.. [2] “Combinatorics - Reaching All Possible Simple Directed Graphs with a given
       Degree Sequence with 2-Edge Swaps.” Mathematics Stack Exchange,
       https://math.stackexchange.com/questions/22272/. Accessed 30 May 2022.
*Number of swaps > number of tries allowed.   z"DiGraph has fewer than four nodes.r   zDiGraph has fewer than 3 edgesr   r   cdistributionr   !Maximum number of swap attempts (*) exceeded before desired swaps achieved ().)nxNetworkXErrorlenedgeszipdegreeutilscumulative_distributiondiscrete_sequenceNetworkXAlgorithmError
out_degreechoicelistsuccadd_edgeremove_edge)Gr   r   r   tries	swapcountkeysdegreescdfr    start_indexstartmsgsecondthirdfourths                   J/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/swap.pyr   r      s,   z KLL
1vzCDD
177|a?@@
 EI$MD
((
*
*7
3C22

'HK!
5eW<fglfmmopC++C00 <<!#T!&&-01?<<1$D01?<<!#T!&&-01? &ffVn,ffUm+ JJu$JJu%JJv&MM%(MM&(MM%(NIS 
V H    c                    U R                  5       (       a  [        R                  " S5      eX:  a  [        R                  " S5      e[        U 5      S:  a  [        R                  " S5      e[        U R                  5      S:  a  [        R                  " S5      eSnSn[        U R                  5       6 u  pg[        R                  R                  U5      n[        R                  R                  n	XQ:  a  U	" SXS9u  pX:X  a  M  Xj   nXk   nUR                  [        X   5      5      nUR                  [        X   5      5      nX:X  a  M\  XU   ;  aQ  XU   ;  aI  U R                  X5        U R                  X5        U R                  X5        U R                  X5        US	-  nXB:  a  S
U SU S3n[        R                  " U5      eUS	-  nXQ:  a  M  U $ )ae  Swap two edges in the graph while keeping the node degrees fixed.

A double-edge swap removes two randomly chosen edges u-v and x-y
and creates the new edges u-x and v-y::

 u--v            u  v
        becomes  |  |
 x--y            x  y

If either the edge u-x or v-y already exist no swap is performed
and another attempt is made to find a suitable edge pair.

Parameters
----------
G : graph
   An undirected graph

nswap : integer (optional, default=1)
   Number of double-edge swaps to perform

max_tries : integer (optional)
   Maximum number of attempts to swap edges

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
G : graph
   The graph after double edge swaps.

Raises
------
NetworkXError
    If `G` is directed, or
    If `nswap` > `max_tries`, or
    If there are fewer than 4 nodes or 2 edges in `G`.
NetworkXAlgorithmError
    If the number of swap attempts exceeds `max_tries` before `nswap` swaps are made

Notes
-----
Does not enforce any connectivity constraints.

The graph G is modified in place.
zSdouble_edge_swap() not defined for directed graphs. Use directed_edge_swap instead.r   r    Graph has fewer than four nodes.   zGraph has fewer than 2 edgesr   r   r   r   r   r   )is_directedr   r   r   r   r   r   r   r   r    r#   r$   r&   r'   r!   )r(   r   r   r   nr*   r+   r,   r-   r    uixiuxvyes                    r4   r   r      s   d 	}}a
 	
 KLL
1vzABB
177|a=>> 	
AI$MD
((
*
*7
3C22

 %QcE8HHKKQT
#KKQT
#6qTM1JJqJJqMM!MM!NI>3A3 7227<  ++A..	Q5 
6 Hr5   )r	   c                    [         R                  " U 5      (       d  [         R                  " S5      e[        U 5      S:  a  [         R                  " S5      eSnSnU R	                  5       nU R	                  5        VVs/ s H  u  pGUPM	     nnn[         R
                  R                  U R	                  5        VVs/ s H  u  pGUPM	     snn5      n	[         R
                  R                  n
SnWU:  Gan  Sn/ nX:  Ga  SnX:  Gas  XA:  Gam  U
" SXS9u  nnUU:X  a  M  X   nUU   nUR                  [        U R                  U5      5      5      nUR                  [        U R                  U5      5      5      nUU:X  a  M  UU U   ;  ak  UU U   ;  ab  U R                  UU5        U R                  UU5        U R                  UU5        U R                  UU5        UR                  UUUU45        US-  nUS-  n[         R                  " U UU5      (       a  US-  nOOU R                  UU5        U R                  UU5        U R                  UU5        U R                  UU5        US-  nS	nX:  a  XA:  a  GMm  U(       a  [        R                   " US-  5      nGOUS-  nGOX:  Ga  XA:  Ga   U
" SXS9u  nnUU:X  a  M  X   nUU   nUR                  [        U R                  U5      5      5      nUR                  [        U R                  U5      5      5      nUU:X  a  M  UU U   ;  ak  UU U   ;  ab  U R                  UU5        U R                  UU5        U R                  UU5        U R                  UU5        UR                  UUUU45        US-  nUS-  nUS-  nX:  a  XA:  a  GM   [         R                  " U 5      (       a  US-  nOU(       ak  UR#                  5       u  nnnnU R                  UU5        U R                  UU5        U R                  UU5        U R                  UU5        US-  nU(       a  Mk  [        R                   " US-  5      nXA:  a  GMn  U$ s  snnf s  snnf )
a  Attempts the specified number of double-edge swaps in the graph `G`.

A double-edge swap removes two randomly chosen edges `(u, v)` and `(x,
y)` and creates the new edges `(u, x)` and `(v, y)`::

 u--v            u  v
        becomes  |  |
 x--y            x  y

If either `(u, x)` or `(v, y)` already exist, then no swap is performed
so the actual number of swapped edges is always *at most* `nswap`.

Parameters
----------
G : graph
   An undirected graph

nswap : integer (optional, default=1)
   Number of double-edge swaps to perform

_window_threshold : integer

   The window size below which connectedness of the graph will be checked
   after each swap.

   The "window" in this function is a dynamically updated integer that
   represents the number of swap attempts to make before checking if the
   graph remains connected. It is an optimization used to decrease the
   running time of the algorithm in exchange for increased complexity of
   implementation.

   If the window size is below this threshold, then the algorithm checks
   after each swap if the graph remains connected by checking if there is a
   path joining the two nodes whose edge was just removed. If the window
   size is above this threshold, then the algorithm performs do all the
   swaps in the window and only then check if the graph is still connected.

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.

Returns
-------
int
   The number of successful swaps

Raises
------

NetworkXError

   If the input graph is not connected, or if the graph has fewer than four
   nodes.

Notes
-----

The initial graph `G` must be connected, and the resulting graph is
connected. The graph `G` is modified in place.

References
----------
.. [1] C. Gkantsidis and M. Mihail and E. Zegura,
       The Markov chain simulation method for generating connected
       power law random graphs, 2003.
       http://citeseer.ist.psu.edu/gkantsidis03markov.html
zGraph not connectedr   r7   r   r   Fr8   r   T)r   is_connectedr   r   r   r   r   r    r#   r$   	neighborsr'   r&   appendhas_pathmathceilpop)r(   r   _window_thresholdr   r:   r*   degddkr-   r    windowwcountswappedfailr;   r<   r=   r>   r?   r@   s                        r4   r   r      s   L ??1455
1vzABB	AI
((*C
	#
!
B	#
((
*
*!((*+E*$!A*+E
FC22F
e) %D/ai -QcMR8FrFKKQ[[^ 45KKQ[[^ 456AaD=Qad]MM!Q'MM!Q'JJq!$JJq!$NNAq!Q<0NIQ;;q!Q''aKF JJq!$JJq!$MM!Q'MM!Q'NIDE /aiH 6A:.!
 /ai -QcMR8FrFKKQ[[^ 45KKQ[[^ 456AaD=Qad]MM!Q'MM!Q'JJq!$JJq!$NNAq!Q<0NIQ!1 /ai4 q!!! #*;;=LQ1aJJq!$JJq!$MM!Q'MM!Q'NI g 6A:.w )x A 
$+Es   >Q$;Q*
)r   r   N)r   r   N)__doc__rG   networkxr   networkx.utilsr   __all__r   not_implemented_for_dispatchabler   r   r    r5   r4   <module>rY      s       *
R l+D9#$$ u :  ,up D9] : ]@ %l & lr5   