U
    ¹mœdÞ  ã                   @   sF   d Z ddlmZ ddlmZmZ ddlmZ dgZ	ej
Zd	dd„ZdS )
z1Functions related to the Wiener index of a graph.é    )Úchainé   )Úis_connectedÚis_strongly_connected)Úshortest_path_lengthÚwiener_indexNc                 C   sV   |   ¡ }|rt| ƒr |s(t| ƒs(tdƒS ttdd„ t| |d�D ƒƒƒ}|rN|S |d S )aè  Returns the Wiener index of the given graph.

    The *Wiener index* of a graph is the sum of the shortest-path
    distances between each pair of reachable nodes. For pairs of nodes
    in undirected graphs, only one orientation of the pair is counted.

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

    weight : object
        The edge attribute to use as distance when computing
        shortest-path distances. This is passed directly to the
        :func:`networkx.shortest_path_length` function.

    Returns
    -------
    float
        The Wiener index of the graph `G`.

    Raises
    ------
    NetworkXError
        If the graph `G` is not connected.

    Notes
    -----
    If a pair of nodes is not reachable, the distance is assumed to be
    infinity. This means that for graphs that are not
    strongly-connected, this function returns ``inf``.

    The Wiener index is not usually defined for directed graphs, however
    this function uses the natural generalization of the Wiener index to
    directed graphs.

    Examples
    --------
    The Wiener index of the (unweighted) complete graph on *n* nodes
    equals the number of pairs of the *n* nodes, since each pair of
    nodes is at distance one::

        >>> n = 10
        >>> G = nx.complete_graph(n)
        >>> nx.wiener_index(G) == n * (n - 1) / 2
        True

    Graphs that are not strongly-connected have infinite Wiener index::

        >>> G = nx.empty_graph(2)
        >>> nx.wiener_index(G)
        inf

    Úinfc                 s   s   | ]\}}|  ¡ V  qd S )N)Úvalues)Ú.0ÚvÚp© r   úS/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/networkx/algorithms/wiener.pyÚ	<genexpr>J   s     zwiener_index.<locals>.<genexpr>)Úweighté   )Úis_directedr   r   ÚfloatÚsumÚchainiÚspl)ÚGr   r   Útotalr   r   r   r      s    6ÿÿ)N)Ú__doc__Ú	itertoolsr   Ú
componentsr   r   Zshortest_pathsr   r   Ú__all__Úfrom_iterabler   r   r   r   r   r   Ú<module>   s   