
    h@'                         S r SSKrSSKrSSKJrJr  S/r\" S5      \" S5      \" S5      \R                  " SS	S
9SS j5       5       5       5       r	S r
S rS rg)z.Functions for computing sparsifiers of graphs.    N)not_implemented_forpy_random_statespannerdirected
multigraph   weightT)
edge_attrsreturns_graphc           	         US:  a  [        S5      eUS-   S-  n[        R                  " 5       nUR                  U R                  5        [        X5      nU R                   Vs0 s H  owU_M     nn[        R                  " U R                  5       SU-  5      n	S[        R                  " U R                  5       SSU-  -   5      -  n
SnXS-
  :  Ga  [        5       n[        UR                  5       5       H*  nUR                  5       U	:  d  M  UR                  U5        M,     [        5       n[        5       n0 nUR                   GHF  nX   U;   a  M  [        XhU5      u  nn[        UR                  5       5      U-  nU(       dU  UR                  5        H  nUR                  UU45        M     UR                  U    H  nUR                  UU45        M     M  [!        UUR"                  S9nUU   nUU   nUR                  UU45        UUU'   UR%                  5        H&  u  nnUU:  d  M  UU   nUR                  UU45        M(     UR                  U    H.  nUU   nUU   nUU:X  d  UU:  d  M  UR                  UU45        M0     GMI     ['        U5      U
:  a  GM  US-   nU H  u  nn[)        XVUXr5        M     UR+                  U5        UR%                  5        H  u  nnX;   d  M  UUU'   M     UnUR                   HA  n[-        UR                  U   5       H"  nUU   X   :X  d  M  UR/                  UU5        M$     MC     [-        UR                  5       H  nXx;  d  M
  UR1                  U5        M     XS-
  :  a  GM  UR                   H7  n[        XhU5      u  nnUR                  5        H  n[)        XVUUU5        M     M9     U$ s  snf )a  Returns 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<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).
   zstretch must be at least 1   r   )key)
ValueErrornxempty_graphadd_nodes_fromnodes_setup_residual_graphmathpownumber_of_nodessetvaluesrandomadd_lightest_edge_dictskeysadjmingetitemslen_add_edge_to_spannerremove_edges_fromlistremove_edgeremove_node)Gstretchr	   seedkHresidual_graphv
clusteringsample_prob
size_limitisampled_centerscenteredges_to_addedges_to_removenew_clusteringlightest_edge_neighborlightest_edge_weightneighboring_sampled_centersneighborclosest_centerclosest_center_weightclosest_center_neighboredge_weightnbr_cluster
nbr_weightunode_s                                 Q/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/sparsifiers.pyr   r      s   p {566	1A 	AQWW
 +15N !"(1Q$J(((1,,.Q7KTXXa//11q1u9==J	A
!e)%*++-.F{{}{*##F+ /
 u%%%A}/ <PA<8"$8 (--/0?B (
 / 6 = = ?H $$a]3 !@ !/ 2 21 5H#''H6 !6 "%/5I5M5M" )=^(L%*@*P'  !%<!=>$2q! ,@+E+E+G'FK"%::#9&#A$((!X7 ,H !/ 2 21 5H",X"6K!5k!BJ#~5%(=='++QM: !6S &h |z) E !DAq AqA ! 	((9 ',,.LD&('-t$ / $
  %%A.,,Q/0a=JM1"..q!4 1 & n**+A"**1- ,u !e)~ !!$8UV$W!.557H AxH 8 "
 HS )s   #Oc                     U R                  5       nU R                  5        HS  u  p4U(       d   [        U5      [        U5      4X#   U   S'   M,  X   U   U   [        U5      [        U5      4X#   U   S'   MU     U$ )aD  Setup 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	   )copyedgesid)r*   r	   r/   rD   r0   s        rG   r   r      sw    0 VVXN 	.0eRU^Na *./d1gfor!ube-LNa *	      c                     0 n0 nU R                   U    H(  nX   nX   U   S   nXd;  d
  XtU   :  d  M   XSU'   XtU'   M*     X44$ )a  Find 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/   r1   rE   r:   r;   r=   
nbr_centerr	   s           rG   r   r      sl    @  "&&t,)
%h/92Z8819:./5, - "77rL   c                 `    U R                  X#5        U(       a  X   U   S   S   X   U   U'   gg)a  Add 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	   r   N)add_edge)r.   r/   rD   r0   r	   s        rG   r%   r%     s9    , JJq(+A.x8;Q rL   )NN)__doc__r   networkxr   networkx.utilsr   r   __all___dispatchabler   r   r   r%    rL   rG   <module>rW      st    4   ?+ Z \"XT:l ;  # !l^!H+8\<rL   