K i@'dZddlZddlZddlmZmZdgZedededejdd dd Z d Z d Z dZ y)z.Functions for computing sparsifiers of graphs.N)not_implemented_forpy_random_statespannerdirected multigraphweightT) edge_attrs returns_graphc R|dkr td|dzdz}tj}|j|jt ||}|jDcic]}||}}t j|jd|z } dt j|jdd|z zz} d} | |dz krt} t|jD]'} |j| ks| j| )t}t}i}|jD]4}||| vr t|||\}}t|j| z}|sP|jD]}|j||f|j|D]}|j||ft!||j"}||}||}|j||f|||<|j%D]#\} }||ks || }|j||f%|j|D]*}||}||}||k(s||ks|j||f,7t'|| kDr| dz} |D]\}}t)||||||j+||j%D]\}} | | vs | ||<|}|jD]=}t-|j|D] }||||k(s|j/||"?t-|jD]}||vs|j1|| |dz kr|jD]6}t|||\}}|jD]}t)|||||8|Scc}w)abReturns a spanner of the given graph with the given stretch. A spanner of a graph G = (V, E) with stretch t is a subgraph H = (V, E_S) such that E_S is a subset of E and the distance between any pair of nodes in H is at most t times the distance between the nodes in G. Parameters ---------- G : NetworkX graph An undirected simple graph. stretch : float The stretch of the spanner. weight : object The edge attribute to use as distance. seed : integer, random_state, or None (default) Indicator of random number generation state. See :ref:`Randomness`. Returns ------- NetworkX graph A spanner of the given graph with the given stretch. Raises ------ ValueError If a stretch less than 1 is given. Notes ----- This function implements the spanner algorithm by Baswana and Sen, see [1]. This algorithm is a randomized las vegas algorithm: The expected running time is O(km) where k = (stretch + 1) // 2 and m is the number of edges in G. The returned graph is always a spanner of the given graph with the specified stretch. For weighted graphs the number of edges in the spanner is O(k * n^(1 + 1 / k)) where k is defined as above and n is the number of nodes in G. For unweighted graphs the number of edges is O(n^(1 + 1 / k) + kn). References ---------- [1] S. Baswana, S. Sen. A Simple and Linear Time Randomized Algorithm for Computing Sparse Spanners in Weighted Graphs. Random Struct. Algorithms 30(4): 532-563 (2007). zstretch must be at least 1r)key) ValueErrornx empty_graphadd_nodes_fromnodes_setup_residual_graphmathpownumber_of_nodessetvaluesrandomadd_lightest_edge_dictskeysadjmingetitemslen_add_edge_to_spannerremove_edges_fromlist remove_edge remove_node)Gstretchr seedkHresidual_graphv clustering sample_prob size_limitisampled_centerscenter edges_to_addedges_to_removenew_clusteringlightest_edge_neighborlightest_edge_weightneighboring_sampled_centersneighborclosest_centerclosest_center_weightclosest_center_neighbor edge_weight nbr_cluster nbr_weightunode_s e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/sparsifiers.pyrr sBp{566 1A AQWW +1f5N!"(1!Q$(J(((1,,.Q7KTXXa//11q1u9==J A a!e)%*++-. ,F{{}{*##F+ , u %%%0 ;A!}/$2q!,@+E+E+G8'FK"%::#9&#A$((!X78!/ 2 21 5;H",X"6K!5k!BJ#~5%(=='++QM:;S0 ;h | z )  E! BDAq NAq& A B ((9',,. .LD&('-t$ .$  %% 5A.,,Q/0 5a=JqM1"..q!4 5 5 n**+ .A "**1- .u a!e)~ ! !I$8UV$W!.557 IH NAx H II HS)s" N$c|j}|jD]P\}}|s t|t|f|||d<(||||t|t|f|||d<R|S)aSetup residual graph as a copy of G with unique edges weights. The node set of the residual graph corresponds to the set V' from the Baswana-Sen paper and the edge set corresponds to the set E' from the paper. This function associates distinct weights to the edges of the residual graph (even for unweighted input graphs), as required by the algorithm. Parameters ---------- G : NetworkX graph An undirected simple graph. weight : object The edge attribute to use as distance. Returns ------- NetworkX graph The residual graph used for the Baswana-Sen algorithm. r )copyedgesid)r*r r/rDr0s rGrrs0VVXN M1.0eRU^N1 a  *./d1gfor!ube-LN1 a  * M ci}i}|j|D])}||}|||d}||vs |||ks |||<|||<+||fS)aFind the lightest edge to each cluster. Searches for the minimum-weight edge to each cluster adjacent to the given node. Parameters ---------- residual_graph : NetworkX graph The residual graph used by the Baswana-Sen algorithm. clustering : dictionary The current clustering of the nodes. node : node The node from which the search originates. Returns ------- lightest_edge_neighbor, lightest_edge_weight : dictionary, dictionary lightest_edge_neighbor is a dictionary that maps a center C to a node v in the corresponding cluster such that the edge from the given node to v is the lightest edge from the given node to any node in cluster. lightest_edge_weight maps a center C to the weight of the aforementioned edge. Notes ----- If a cluster has no node that is adjacent to the given node in the residual graph then the center of the cluster is not a key in the returned dictionaries. r )r )r/r1rEr:r;r= nbr_centerr s rGrrs@ "&&t,6) %h/9 2 2,Z8819 ": ./5  ,6 "#7 77rLc\|j|||r|||dd||||<yy)aAdd the edge {u, v} to the spanner H and take weight from the residual graph. Parameters ---------- H : NetworkX graph The spanner under construction. residual_graph : NetworkX graph The residual graph used by the Baswana-Sen algorithm. The weight for the edge is taken from this graph. u : node One endpoint of the edge. v : node The other endpoint of the edge. weight : object The edge attribute to use as distance. r rN)add_edge)r.r/rDr0r s rGr%r%s?,JJq! (+A.x8;!QrL)NN) __doc__rnetworkxrnetworkx.utilsrr__all__ _dispatchablerrrr%rLrGrWsv4 ? +Z \"XT:l ;#!l ^!H+8\