ó
 ,ľ[c           @   s   d  Z  d d l Z d d l Td j d d g  Z d d g Z e d	  e d
  d     Z e d	  e d
  d     Z	 d   Z
 d S(   s   
Communicability.
i˙˙˙˙N(   t   *s   
s   Aric Hagberg (hagberg@lanl.gov)s$   Franck Kalala (franckkalala@yahoo.frt   communicabilityt   communicability_expt   directedt
   multigraphc         C   sY  d d l  } d d l } t |   } t j |  |  } d | | d k <| j j |  \ } } | j |  } t t	 | t
 t |     } i  }	 xž |  D]ś }
 i  |	 |
 <xŁ |  D] } d } | |
 } | | } xd t
 t |   D]P } | | d d  | f | d f | d d  | f | d f | | 7} qĺ Wt |  |	 |
 | <q˛ Wq W|	 S(   s  Return communicability between all pairs of nodes in G.

    The communicability between pairs of nodes in G is the sum of
    closed walks of different lengths starting at node u and ending at node v.

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

    Returns
    -------
    comm: dictionary of dictionaries
        Dictionary of dictionaries keyed by nodes with communicability
        as the value.

    Raises
    ------
    NetworkXError
       If the graph is not undirected and simple.

    See Also
    --------
    communicability_exp:
       Communicability between all pairs of nodes in G  using spectral
       decomposition.
    communicability_betweenness_centrality:
       Communicability betweeness centrality for each node in G.

    Notes
    -----
    This algorithm uses a spectral decomposition of the adjacency matrix.
    Let G=(V,E) be a simple undirected graph.  Using the connection between
    the powers  of the adjacency matrix and the number of walks in the graph,
    the communicability  between nodes `u` and `v` based on the graph spectrum
    is [1]_

    .. math::
        C(u,v)=\sum_{j=1}^{n}\phi_{j}(u)\phi_{j}(v)e^{\lambda_{j}},

    where `\phi_{j}(u)` is the `u\rm{th}` element of the `j\rm{th}` orthonormal
    eigenvector of the adjacency matrix associated with the eigenvalue
    `\lambda_{j}`.

    References
    ----------
    .. [1] Ernesto Estrada, Naomichi Hatano,
       "Communicability in complex networks",
       Phys. Rev. E 77, 036111 (2008).
       https://arxiv.org/abs/0707.0756

    Examples
    --------
    >>> G = nx.Graph([(0,1),(1,2),(1,5),(5,4),(2,4),(2,3),(4,3),(3,6)])
    >>> c = nx.communicability(G)
    i˙˙˙˙Ni   g        i    (   t   numpyt   scipy.linalgt   listt   nxt   to_numpy_matrixt   linalgt   eight   expt   dictt   zipt   ranget   lent   float(   t   GR   t   scipyt   nodelistt   At   wt   vect   expwt   mappingt   ct   ut   vt   st   pt   qt   j(    (    sF   lib/python2.7/site-packages/networkx/algorithms/communicability_alg.pyR      s&    :!


Nc   	      C   sĚ   d d l  } t |   } t j |  |  } d | | d k <| j j | j  } t t | t	 t
 |     } i  } xO |  D]G } i  | | <x4 |  D], } t | | | | | f  | | | <q Wq} W| S(   s˘  Return communicability between all pairs of nodes in G.

    Communicability between pair of node (u,v) of node in G is the sum of
    closed walks of different lengths starting at node u and ending at node v.

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

    Returns
    -------
    comm: dictionary of dictionaries
        Dictionary of dictionaries keyed by nodes with communicability
        as the value.

    Raises
    ------
    NetworkXError
        If the graph is not undirected and simple.

    See Also
    --------
    communicability:
       Communicability between pairs of nodes in G.
    communicability_betweenness_centrality:
       Communicability betweeness centrality for each node in G.

    Notes
    -----
    This algorithm uses matrix exponentiation of the adjacency matrix.

    Let G=(V,E) be a simple undirected graph.  Using the connection between
    the powers  of the adjacency matrix and the number of walks in the graph,
    the communicability between nodes u and v is [1]_,

    .. math::
        C(u,v) = (e^A)_{uv},

    where `A` is the adjacency matrix of G.

    References
    ----------
    .. [1] Ernesto Estrada, Naomichi Hatano,
       "Communicability in complex networks",
       Phys. Rev. E 77, 036111 (2008).
       https://arxiv.org/abs/0707.0756

    Examples
    --------
    >>> G = nx.Graph([(0,1),(1,2),(1,5),(5,4),(2,4),(2,3),(4,3),(3,6)])
    >>> c = nx.communicability_exp(G)
    i˙˙˙˙Ni   g        (   R   R   R   R	   R
   t   expmR   R   R   R   R   R   (	   R   R   R   R   t   expAR   R   R   R   (    (    sF   lib/python2.7/site-packages/networkx/algorithms/communicability_alg.pyR   f   s    7!
.c         C   s`   d d l  m } y d d  l } Wn | d   n Xy d d  l } Wn | d   n Xd  S(   Ni˙˙˙˙(   t   SkipTests   NumPy not availables   SciPy not available(   t   noseR#   R   R   (   t   moduleR#   R   R   (    (    sF   lib/python2.7/site-packages/networkx/algorithms/communicability_alg.pyt   setup_moduleŻ   s    (   t   __doc__t   networkxR   t   networkx.utilst   joint
   __author__t   __all__t   not_implemented_forR   R   R&   (    (    (    sF   lib/python2.7/site-packages/networkx/algorithms/communicability_alg.pyt   <module>   s   
			P	H