o
    ›Çh˜9  ã                   @   sž   d Z ddlZddlZddlmZ g d¢Zej d¡edƒej	dddd	d
ddœdd„ƒƒƒZ
edƒej	dddddd„ƒƒZedƒej	ddddd„ƒƒZdS )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                C   sÊ  ||kr	t  d¡‚t| ƒdk rt  d¡‚t| jƒdk r t  d¡‚d}d}t|  ¡ Ž \}}t j |¡}t jj}	||k rã|	d||dd }
||
 }|d7 }||kr]d	|› d
|› d}t  	|¡‚|  
|¡dkreq6| t| j| ƒ¡}||krtq6|  
|¡dkr|q6| t| j| ƒ¡}||kr‹q6|  
|¡dkr“q6| t| j| ƒ¡}||kr¢q6|| j| vrß|| j| vrß|| j| vrß|  ||¡ |  ||¡ |  ||¡ |  ||¡ |  ||¡ |  ||¡ |d7 }||k s:| S )u#  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Úfourth© r2   úl/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/networkx/algorithms/swap.pyr      sZ   =



×+r   c                 C   sh  |   ¡ r	t d¡‚||krt d¡‚t| ƒdk rt d¡‚t| jƒdk r)t d¡‚d}d}t|  ¡ Ž \}}tj |¡}tjj	}	||k r²|	d||d\}
}|
|krQq?||
 }|| }| 
t| | ƒ¡}| 
t| | ƒ¡}||krpq?|| | vr˜|| | vr˜|  ||¡ |  ||¡ |  ||¡ |  ||¡ |d	7 }||krªd
|› d|› d}t |¡‚|d	7 }||k sC| 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.

    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Úer2   r2   r3   r   †   sP   2ÿ


ÿÿ
ær   )r   c                 C   sl  t  | ¡s
t  d¡‚t| ƒdk rt  d¡‚d}d}|  ¡ }dd„ |  ¡ D ƒ}t j dd„ |  ¡ D ƒ¡}t jj}	d}
||k r´d}g }|
|k rõd	}||
k ræ||k ræ|	d
||d\}}||kr^qH|| }|| }| t	|  
|¡ƒ¡}| t	|  
|¡ƒ¡}||krqH|| | vr°|| | vr°|  ||¡ |  ||¡ |  ||¡ |  ||¡ | ||||f¡ |d7 }|d7 }t  | ||¡rÀ|d7 }n|  ||¡ |  ||¡ |  ||¡ |  ||¡ |d8 }d}||
k ræ||k sP|rðt |
d
 ¡}
nÀ|
d7 }
n»||
k rt||k rt|	d
||d\}}||krqõ|| }|| }| t	|  
|¡ƒ¡}| t	|  
|¡ƒ¡}||kr0qõ|| | vrc|| | vrc|  ||¡ |  ||¡ |  ||¡ |  ||¡ | ||||f¡ |d7 }|d7 }|d7 }||
k rt||k sÿt  | ¡r|
d7 }
n1|r©| ¡ \}}}}|  ||¡ |  ||¡ |  ||¡ |  ||¡ |d8 }|s‚t |
d
 ¡}
||k s>|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   r4   r   c                 S   s   g | ]\}}|‘qS r2   r2   ©Ú.0r7   Údr2   r2   r3   Ú
<listcomp>6  ó    z.connected_double_edge_swap.<locals>.<listcomp>c                 S   s   g | ]\}}|‘qS r2   r2   r?   r2   r2   r3   rB   7  rC   r
   Fr5   r   T)r   Úis_connectedr   r   r   r   r   r   r!   r"   Ú	neighborsr%   r$   ÚappendÚhas_pathÚmathÚceilÚpop)r&   r   Ú_window_thresholdr   r7   r(   ÚdegÚdkr+   r   ÚwindowÚwcountÚswappedÚfailr8   r9   r:   r;   r<   r=   r2   r2   r3   r   è   sž   
F



Þ$


è
ú¥\r   )r
   r   N)r
   r   N)Ú__doc__rH   Únetworkxr   Únetworkx.utilsr   Ú__all__r   Únot_implemented_forÚ_dispatchabler   r   r   r2   r2   r2   r3   Ú<module>   s    
x`
