ó žÃÒYc@ s†dZddlmZddlZddlZddlZdjddddd gƒZd d gZ d d d„Z d dd„Z dS(sSwap edges in a graph. iÿÿÿÿ(tdivisionNs sAric Hagberg (hagberg@lanl.gov)sPieter Swart (swart@lanl.gov)s Dan Schult (dschult@colgate.edu)s.Joel Miller (joel.c.miller.research@gmail.com)s Ben Edwardstdouble_edge_swaptconnected_double_edge_swapiidcC sÜ|jƒrtjdƒ‚n||kr<tjdƒ‚nt|ƒdkr`tjdƒ‚nd}d}t|jƒŒ\}}tjj|ƒ}x?||kr×tjjdd|ƒ\}} || krÕq™n||} || } t j t || ƒƒ} t j t || ƒƒ} | | kr-q™n| || krš| || krš|j | | ƒ|j | | ƒ|j | | ƒ|j | | ƒ|d7}n||krÊd |d |}tj|ƒ‚n|d7}q™W|S( s@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 Returns ------- G : graph The graph after double edge swaps. Notes ----- Does not enforce any connectivity constraints. The graph G is modified in place. s3double_edge_swap() not defined for directed graphs.s*Number of swaps > number of tries allowed.isGraph has less than four nodes.iit cdistributionis.Maximum number of swap attempts (%s) exceeded s#before desired swaps achieved (%s).(t is_directedtnxt NetworkXErrortlentziptdegreetutilstcumulative_distributiontdiscrete_sequencetrandomtchoicetlisttadd_edget remove_edgetNetworkXAlgorithmError(tGtnswapt max_triestnt swapcounttkeystdegreestcdftuitxitutxtvtyte((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/swap.pyRsB#           icC s­tj|ƒs!tjdƒ‚nt|ƒdkrEtjdƒ‚nd}d}|jƒ}td„|jƒDƒƒ}tjjtd„|jƒDƒƒƒ}d}xÿ||kr¨d} g} ||kr¶t} x°| |kr†||kr†tjj dd |ƒ\} } | | krq×n|| }|| }t j t|j |ƒƒƒ}t j t|j |ƒƒƒ}||krq×n|||kr|||kr|j ||ƒ|j ||ƒ|j||ƒ|j||ƒ| j||||fƒ|d7}n|d7}tj|||ƒr3| d7} q×|j||ƒ|j||ƒ|j ||ƒ|j ||ƒ|d8}t} q×W| r©ttj|dƒƒ}q¥|d7}qªxH| |kr||krtjj dd |ƒ\} } | | krq¹n|| }|| }t j t|j |ƒƒƒ}t j t|j |ƒƒƒ}||krcq¹n|||kré|||kré|j ||ƒ|j ||ƒ|j||ƒ|j||ƒ| j||||fƒ|d7}n|d7}| d7} q¹Wtj|ƒr|d7}qªxl| r‹| jƒ\}}}}|j||ƒ|j||ƒ|j ||ƒ|j ||ƒ|d8}q Wttj|dƒƒ}qªW|S( sAttempts 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. 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 sGraph not connectedisGraph has less than four nodes.ics s|]\}}|VqdS(N((t.0Rtd((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/swap.pys °scs s|]\}}|VqdS(N((R"RR#((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/swap.pys ±siiR(Rt is_connectedRRR RR R tFalseR R Rt neighborsRRtappendthas_pathtTruetinttmathtceiltpop(RRt_window_thresholdRRtdegtdkRtwindowtwcounttswappedtfailRRRRRR ((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/swap.pyRhs”@ (                     ( t__doc__t __future__RR+R tnetworkxRtjoint __author__t__all__RR(((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/swap.pyts      M