
    wg@'                         d Z ddlZddlZddlmZmZ dgZ ed       ed       ed       ej                  dd	
      dd                            Z	d Z
d Zd Zy)z.Functions for computing sparsifiers of graphs.    N)not_implemented_forpy_random_statespannerdirected
multigraph   weightT)
edge_attrsreturns_graphc           	      R   |dk  rt        d      |dz   dz  }t        j                         }|j                  | j                         t        | |      }| j                  D ci c]  }|| }}t        j                  | j                         d|z        }	dt        j                  | j                         dd|z  z         z  }
d}||dz
  k  rt               }t        |j                               D ]'  }|j                         |	k  s|j                  |       ) t               }t               }i }|j                  D ]4  }||   |v rt        |||      \  }}t        |j                               |z  }|sP|j                         D ]  }|j                  ||f        |j                  |   D ]  }|j                  ||f        t!        ||j"                        }||   }||   }|j                  ||f       |||<   |j%                         D ]#  \  }}||k  s||   }|j                  ||f       % |j                  |   D ]*  }||   }||   }||k(  s||k  s|j                  ||f       , 7 t'        |      |
kD  r|dz   }|D ]  \  }}t)        |||||        |j+                  |       |j%                         D ]  \  }}||v s|||<    |}|j                  D ]=  }t-        |j                  |         D ]   }||   ||   k(  s|j/                  ||       " ? t-        |j                        D ]  }||vs|j1                  |        ||dz
  k  r|j                  D ]6  }t        |||      \  }}|j                         D ]  }t)        |||||        8 |S c c}w )ab  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                                 d/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/sparsifiers.pyr   r      sB   p {566	1A 	AQWW
 +1f5N !"(1!Q$(J(((1,,.Q7KTXXa//11q1u9==J	A
a!e)%*++-. 	,F{{}{*##F+	,
 u%%% 0	;A!}/ <P
A<8"$8 (--/0?B (
 / 6 = = ? 4H $$a]34 !/ 2 21 5 7H#''H67 "%/5I5M5M" )=^(L%*@*P'  !%<!=>$2q! ,@+E+E+G 8'FK"%::#9&#A$((!X78 !/ 2 21 5 ;H",X"6K!5k!BJ#~5%(=='++QM:;S0	;h |z) E ! 	BDAq NAq&A	B 	((9 ',,. 	.LD&('-t$	. $
  %% 	5A.,,Q/0 5a=JqM1"..q!45	5 n**+ 	.A
"**1-	.u a!e)~ !! I$8UV$W!.557 	IH NAxH	II
 HS )s   "
N$c                     | j                         }| j                         D ]P  \  }}|s t        |      t        |      f||   |   d<   (| |   |   |   t        |      t        |      f||   |   d<   R |S )a  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      s    0 VVXN 	 M1.0eRU^N1a *./d1gfor!ube-LN1a *	M     c                     i }i }| j                   |   D ])  }||   }| |   |   d   }||vs	|||   k  s |||<   |||<   + ||fS )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      s    @  "&&t, 6)
%h/922,Z8819":./5 ,6 "#777rL   c                 \    | j                  ||       |r||   |   d   d   | |   |   |<   yy)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%     s?    , JJq!(+A.x8;!Q rL   )NN)__doc__r   networkxr   networkx.utilsr   r   __all___dispatchabler   r   r   r%    rL   rG   <module>rW      sv    4   ?+ Z \"XT:l ;  # !l^!H+8\<rL   