o
    ßË·e[&  ã                   @   sP   d Z ddlZddlZddlmZ ddgZedƒdd	d„ƒZedƒdd
d„ƒZdS )zSwap edges in a graph.
é    N)Úpy_random_stateÚdouble_edge_swapÚconnected_double_edge_swapé   é   éd   c                 C   sP  |   ¡ r	t d¡‚||krt d¡‚t| ƒdk rt d¡‚d}d}t|  ¡ Ž \}}tj |¡}tjj}	||k r¦|	d||d\}
}|
|krEq3||
 }|| }| 	t
| | ƒ¡}| 	t
| | ƒ¡}||krdq3|| | vrŒ|| | vrŒ|  ||¡ |  ||¡ |  ||¡ |  ||¡ |d7 }||kržd	|› d
|› d}t |¡‚|d7 }||k s7| S )aÕ  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.

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

    The graph G is modified in place.
    z3double_edge_swap() not defined for directed graphs.z*Number of swaps > number of tries allowed.é   úGraph has less than four nodes.r   é   ©ÚcdistributionÚseedr   z!Maximum number of swap attempts (z*) exceeded before desired swaps achieved (z).)Úis_directedÚnxÚNetworkXErrorÚlenÚzipÚdegreeÚutilsÚcumulative_distributionÚdiscrete_sequenceÚchoiceÚlistÚadd_edgeÚremove_edgeÚNetworkXAlgorithmError)ÚGÚnswapÚ	max_triesr   ÚnÚ	swapcountÚkeysÚdegreesÚcdfr   ÚuiÚxiÚuÚxÚvÚyÚe© r+   úO/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/swap.pyr      sH   (


ÿÿ
æc                 C   sx  t  | ¡s
t  d¡‚t| ƒdk rt  d¡‚d}d}|  ¡ }tdd„ |  ¡ D ƒƒ}t j tdd„ |  ¡ D ƒƒ¡}t jj}	d}
||k rºd}g }|
|k rùd	}||
k rê||k rê|	d
||d\}}||krbqL|| }|| }| 	t|  
|¡ƒ¡}| 	t|  
|¡ƒ¡}||krƒqL|| | vr´|| | vr´|  ||¡ |  ||¡ |  ||¡ |  ||¡ | ||||f¡ |d7 }|d7 }t  | ||¡rÄ|d7 }n|  ||¡ |  ||¡ |  ||¡ |  ||¡ |d8 }d}||
k rê||k sT|rôt |
d
 ¡}
nÂ|
d7 }
n½||
k rz||k rzt jjd
|d\}}||krqù|| }|| }| 	t|  
|¡ƒ¡}| 	t|  
|¡ƒ¡}||kr5qù|| | vrh|| | vrh|  ||¡ |  ||¡ |  ||¡ |  ||¡ | ||||f¡ |d7 }|d7 }|d7 }||
k rz||k st  | ¡r…|
d7 }
n1|r¯| ¡ \}}}}|  ||¡ |  ||¡ |  ||¡ |  ||¡ |d8 }|sˆt |
d
 ¡}
||k sB|S )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   r	   r   c                 s   s    | ]\}}|V  qd S ©Nr+   ©Ú.0r   Údr+   r+   r,   Ú	<genexpr>­   ó   € z-connected_double_edge_swap.<locals>.<genexpr>c                 s   s    | ]\}}|V  qd S r-   r+   r.   r+   r+   r,   r1   ®   r2   r   Fr
   r   T)r   )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Údkr#   r   ÚwindowÚwcountÚswappedÚfailr$   r%   r&   r'   r(   r)   r+   r+   r,   r   `   sž   
E



Þ$


è
ú¥\)r   r   N)r   r   N)	Ú__doc__r7   Únetworkxr   Únetworkx.utilsr   Ú__all__r   r   r+   r+   r+   r,   Ú<module>   s    S