ó
 ,µ[c           @   sx   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l m	 Z
 d g Z e j Z d
 d	 „ Z d
 S(   s1   Functions related to the Wiener index of a graph.iÿÿÿÿ(   t   division(   t   chaini   (   t   is_connected(   t   is_strongly_connected(   t   shortest_path_lengtht   wiener_indexc         C   sw   |  j  ƒ  } | r t |  ƒ s3 | r= t |  ƒ r= t d ƒ St t d „  t |  d | ƒDƒ ƒ ƒ } | ro | S| d S(   s
  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::

        >>> import networkx as nx
        >>> 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

    t   infc         s   s!   |  ] \ } } | j  ƒ  Vq d  S(   N(   t   values(   t   .0t   vt   p(    (    s9   lib/python2.7/site-packages/networkx/algorithms/wiener.pys	   <genexpr>T   s    t   weighti   (   t   is_directedR   R   t   floatt   sumt   chainit   spl(   t   GR   R   t   total(    (    s9   lib/python2.7/site-packages/networkx/algorithms/wiener.pyR      s    7
(N(   t   __doc__t
   __future__R    t	   itertoolsR   t
   componentsR   R   t   shortest_pathsR   R   t   __all__t   from_iterableR   t   NoneR   (    (    (    s9   lib/python2.7/site-packages/networkx/algorithms/wiener.pyt   <module>	   s   		