Ë
    D^(h­  ã                   óx   — d Z ddlZddlmZ dgZ ed«       ej                  d¬«      dd„«       «       Zd	„ Zd
„ Z	y)z'Distance measures approximated metrics.é    N)Úpy_random_stateÚdiameteré   Úapproximate_diameter)Únamec                 ó¨   — | st        j                  d«      ‚| j                  «       dk(  ry| j                  «       rt	        | |«      S t        | |«      S )uu	  Returns a lower bound on the diameter of the graph G.

    The function computes a lower bound on the diameter (i.e., the maximum eccentricity)
    of a directed or undirected graph G. The procedure used varies depending on the graph
    being directed or not.

    If G is an `undirected` graph, then the function uses the `2-sweep` algorithm [1]_.
    The main idea is to pick the farthest node from a random node and return its eccentricity.

    Otherwise, if G is a `directed` graph, the function uses the `2-dSweep` algorithm [2]_,
    The procedure starts by selecting a random source node $s$ from which it performs a
    forward and a backward BFS. Let $a_1$ and $a_2$ be the farthest nodes in the forward and
    backward cases, respectively. Then, it computes the backward eccentricity of $a_1$ using
    a backward BFS and the forward eccentricity of $a_2$ using a forward BFS.
    Finally, it returns the best lower bound between the two.

    In both cases, the time complexity is linear with respect to the size of G.

    Parameters
    ----------
    G : NetworkX graph

    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    d : integer
       Lower Bound on the Diameter of G

    Examples
    --------
    >>> G = nx.path_graph(10)  # undirected graph
    >>> nx.diameter(G)
    9
    >>> G = nx.cycle_graph(3, create_using=nx.DiGraph)  # directed graph
    >>> nx.diameter(G)
    2

    Raises
    ------
    NetworkXError
        If the graph is empty or
        If the graph is undirected and not connected or
        If the graph is directed and not strongly connected.

    See Also
    --------
    networkx.algorithms.distance_measures.diameter

    References
    ----------
    .. [1] Magnien, ClÃ©mence, Matthieu Latapy, and Michel Habib.
       *Fast computation of empirically tight bounds for the diameter of massive graphs.*
       Journal of Experimental Algorithmics (JEA), 2009.
       https://arxiv.org/pdf/0904.2728.pdf
    .. [2] Crescenzi, Pierluigi, Roberto Grossi, Leonardo Lanzi, and Andrea Marino.
       *On computing the diameter of real-world directed (weighted) graphs.*
       International Symposium on Experimental Algorithms. Springer, Berlin, Heidelberg, 2012.
       https://courses.cs.ut.ee/MTAT.03.238/2014_fall/uploads/Main/diameter.pdf
    z"Expected non-empty NetworkX graph!r   r   )ÚnxÚNetworkXErrorÚnumber_of_nodesÚis_directedÚ_two_sweep_directedÚ_two_sweep_undirected)ÚGÚseeds     úq/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/approximation/distance_measures.pyr   r   	   sQ   € ñD Ü×ÑÐCÓDÐDà×ÑÓ˜aÒØà‡}�}„Ü" 1 dÓ+Ð+ä   DÓ)Ð)ó    c                 óð   — |j                  t        | «      «      }t        j                  | |«      }t	        |«      t	        | «      k7  rt        j
                  d«      ‚|�^ }}t        j                  | |«      S )aL  Helper function for finding a lower bound on the diameter
        for undirected Graphs.

        The idea is to pick the farthest node from a random node
        and return its eccentricity.

        ``G`` is a NetworkX undirected graph.

    .. note::

        ``seed`` is a random.Random or numpy.random.RandomState instance
    zGraph not connected.)ÚchoiceÚlistr	   Úshortest_path_lengthÚlenr
   Úeccentricity)r   r   ÚsourceÚ	distancesÚ_Únodes         r   r   r   W   sd   € ð �[‰[œ˜a›Ó!€Fä×'Ñ'¨¨6Ó2€Iä
ˆ9ƒ~œ˜Q›ÒÜ×ÑÐ5Ó6Ð6à�H€Qˆä�?‰?˜1˜dÓ#Ð#r   c                 ó¢  — | j                  «       }|j                  t        | «      «      }t        j                  | |«      }t        j                  ||«      }t        | «      }t        |«      |k7  st        |«      |k7  rt        j                  d«      ‚|�^ }}|�^ }}	t        t        j                  ||«      t        j                  | |	«      «      S )a   Helper function for finding a lower bound on the diameter
        for directed Graphs.

        It implements 2-dSweep, the directed version of the 2-sweep algorithm.
        The algorithm follows the following steps.
        1. Select a source node $s$ at random.
        2. Perform a forward BFS from $s$ to select a node $a_1$ at the maximum
        distance from the source, and compute $LB_1$, the backward eccentricity of $a_1$.
        3. Perform a backward BFS from $s$ to select a node $a_2$ at the maximum
        distance from the source, and compute $LB_2$, the forward eccentricity of $a_2$.
        4. Return the maximum between $LB_1$ and $LB_2$.

        ``G`` is a NetworkX directed graph.

    .. note::

        ``seed`` is a random.Random or numpy.random.RandomState instance
    zDiGraph not strongly connected.)	Úreverser   r   r	   r   r   r
   Úmaxr   )
r   r   Ú
G_reversedr   Úforward_distancesÚbackward_distancesÚnr   Úa_1Úa_2s
             r   r   r   q   s´   € ð( —‘“€Jà�[‰[œ˜a›Ó!€Fä×/Ñ/°°6Ó:Ðä×0Ñ0°¸VÓDÐô 	ˆA‹€AÜ
ÐÓ Ò"¤cÐ*<Ó&=ÀÒ&BÜ×ÑÐ@ÓAÐAà�G€Qˆà �G€QˆäŒr�‰˜z¨3Ó/´·±ÀÀCÓ1HÓIÐIr   )N)
Ú__doc__Únetworkxr	   Únetworkx.utils.decoratorsr   Ú__all__Ú_dispatchabler   r   r   © r   r   ú<module>r,      sR   ðÙ -ã Ý 5àˆ,€ñ �ÓØ€×ÑÐ-Ô.òI*ó /ó ðI*òX$ó4%Jr   