K i9RdZddlZddlZddlmZgdZejjdedejddd d dd d Z edejdddd Z edejdddZ y)zSwap edges in a graph.N)py_random_state)double_edge_swapconnected_double_edge_swapdirected_edge_swap undirectedT) mutates_input returns_graphd)nswap max_triesseedc||kDrtjdt|dkrtjdt|jdkrtjdd}d}t |j \}}tj j|}tj j} ||kr| d||d} || } |dz }||kDrd |d |d } tj| |j| dk(rV|jt|j| } | | k(r|j| dk(r|jt|j| }| |k(r|j|dk(r|jt|j|}||k(r||j| vr||j| vr| |j|vrq|j| ||j|| |j| ||j| | |j| ||j|||dz }||kr|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`. 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.rzDiGraph has fewer than 3 edgesrr  cdistributionr!Maximum number of swap attempts (*) exceeded before desired swaps achieved ().)nx NetworkXErrorlenedgeszipdegreeutilscumulative_distributiondiscrete_sequenceNetworkXAlgorithmError out_degreechoicelistsuccadd_edge remove_edge)Gr rrtries swapcountkeysdegreescdfr start_indexstartmsgsecondthirdfourths ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/swap.pyrr sRz yKLL 1vzCDD 177|a?@@ EI$MD' (( * *7 3C22 e '4HK [!   9 5eW`. 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.rr Graph has fewer than four nodes.zGraph has fewer than 2 edgesrrr rrr) is_directedrrrrrrrrr r#r$r&r'r!)r(r rrnr*r+r,r-r uixiuxvyes r4rrsd }} a   yKLL 1vzABB 177|a=>> AI$MD' (( * *7 3C22 e %QcER 8  H H KKQqT # KKQqT # 6  QqTM1 JJq!  JJq!  MM!Q  MM!Q  NI >3A37227< ++A. . Q5 e 6 Hr5)r crtj|stjdt|dkrtjdd}d}|j }|j Dcgc]\}}| }}}tj j |j Dcgc]\}}| c}}} tj j} d} |krEd} g} | |krd}| | kri||krc| d| |\}}||k(r ||}||}|jt|j|}|jt|j|}||k(r|||vri|||vrb|j|||j|||j|||j||| j||||f|dz }|dz }tj|||r| dz } nO|j|||j|||j|||j|||dz}d }| | kr||krc|rtj | dz } n| dz } n| | kr||kr| d| |\}}||k(r||}||}|jt|j|}|jt|j|}||k(r|||vri|||vrb|j|||j|||j|||j||| j||||f|dz }|dz }| dz } | | kr||krtj|r| dz } n| re| j#\}}}}|j|||j|||j|||j|||dz}| retj | dz } ||krE|Scc}}wcc}}w) aAttempts 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`. 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 zGraph not connectedrr7rr Fr8rT)r is_connectedrrrrrr r#r$ neighborsr'r&appendhas_pathmathceilpop)r(r _window_thresholdrr:r*degddkr-r windowwcountswappedfailr;r<r=r>r?r@s r4rrsL ??1 455 1vzABB AI ((*C #1! #B # (( * *!((*+E$!QA+E FC22 F e) % %D6/a%i-QcMR8rFrFKKQ[[^ 45KKQ[[^ 456AaD=Qad]MM!Q'MM!Q'JJq!$JJq!$NNAq!Q<0NIQ;;q!Q'aKFJJq!$JJq!$MM!Q'MM!Q'NIDE6/a%iH6A:.!  6/a%i-QcMR8rFrFKKQ[[^ 45KKQ[[^ 456AaD=Qad]MM!Q'MM!Q'JJq!$JJq!$NNAq!Q<0NIQ! 16/a%i4q!! #*;;=LQ1aJJq!$JJq!$MM!Q'MM!Q'NI 6A:.w e)x A $+Es 5 P-0 P3 )r r N)r rN) __doc__rGnetworkxrnetworkx.utilsr__all__rnot_implemented_for _dispatchablerrrr5r4rYs * Rl+D9#$$u :,u pD9] :] @%l&lr5