o
    ˷e                     @   s`   d Z ddlZddlmZ ddlmZ g dZeddd	 Zedd
d Z	eddd Z
dS )zDProvides functions for computing the efficiency of nodes and graphs.    N)NetworkXNoPath   )not_implemented_for)
efficiencylocal_efficiencyglobal_efficiencydirectedc                 C   s2   zdt | || }W |S  ty   d}Y |S w )a  Returns the efficiency of a pair of nodes in a graph.

    The *efficiency* of a pair of nodes is the multiplicative inverse of the
    shortest path distance between the nodes [1]_. Returns 0 if no path
    between nodes.

    Parameters
    ----------
    G : :class:`networkx.Graph`
        An undirected graph for which to compute the average local efficiency.
    u, v : node
        Nodes in the graph ``G``.

    Returns
    -------
    float
        Multiplicative inverse of the shortest path distance between the nodes.

    Notes
    -----
    Edge weights are ignored when computing the shortest path distances.

    See also
    --------
    local_efficiency
    global_efficiency

    References
    ----------
    .. [1] Latora, Vito, and Massimo Marchiori.
           "Efficient behavior of small-world networks."
           *Physical Review Letters* 87.19 (2001): 198701.
           <https://doi.org/10.1103/PhysRevLett.87.198701>

       r   )nxshortest_path_lengthr   )Guveff r   ^/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/efficiency_measures.pyr      s   %r   c           	      C   sr   t | }||d  }|dkr5t| }d}|D ]\}}| D ]\}}|dkr-|d| 7 }qq|| }|S d}|S )a  Returns the average global efficiency of the graph.

    The *efficiency* of a pair of nodes in a graph is the multiplicative
    inverse of the shortest path distance between the nodes. The *average
    global efficiency* of a graph is the average efficiency of all pairs of
    nodes [1]_.

    Parameters
    ----------
    G : :class:`networkx.Graph`
        An undirected graph for which to compute the average global efficiency.

    Returns
    -------
    float
        The average global efficiency of the graph.

    Notes
    -----
    Edge weights are ignored when computing the shortest path distances.

    See also
    --------
    local_efficiency

    References
    ----------
    .. [1] Latora, Vito, and Massimo Marchiori.
           "Efficient behavior of small-world networks."
           *Physical Review Letters* 87.19 (2001): 198701.
           <https://doi.org/10.1103/PhysRevLett.87.198701>

    r	   r   )lenr
   all_pairs_shortest_path_lengthitems)	r   ndenomlengthsg_effsourcetargetstargetdistancer   r   r   r   7   s   #
r   c                    s"    fdd D }t |t  S )a  Returns the average local efficiency of the graph.

    The *efficiency* of a pair of nodes in a graph is the multiplicative
    inverse of the shortest path distance between the nodes. The *local
    efficiency* of a node in the graph is the average global efficiency of the
    subgraph induced by the neighbors of the node. The *average local
    efficiency* is the average of the local efficiencies of each node [1]_.

    Parameters
    ----------
    G : :class:`networkx.Graph`
        An undirected graph for which to compute the average local efficiency.

    Returns
    -------
    float
        The average local efficiency of the graph.

    Notes
    -----
    Edge weights are ignored when computing the shortest path distances.

    See also
    --------
    global_efficiency

    References
    ----------
    .. [1] Latora, Vito, and Massimo Marchiori.
           "Efficient behavior of small-world networks."
           *Physical Review Letters* 87.19 (2001): 198701.
           <https://doi.org/10.1103/PhysRevLett.87.198701>

    c                 3   s"    | ]}t   | V  qd S )N)r   subgraph).0r   r   r   r   	<genexpr>   s     z#local_efficiency.<locals>.<genexpr>)sumr   )r   efficiency_listr   r   r   r   m   s   %r   )__doc__networkxr
   networkx.exceptionr   utilsr   __all__r   r   r   r   r   r   r   <module>   s    
+
5