ó
ú£Õ\c           @   s[  d  Z  d d l Z d d l Td j d d g ƒ Z d d d	 d
 d d g Z e d ƒ e d ƒ e d ƒ d „  ƒ ƒ ƒ Z	 e d ƒ e d ƒ e d ƒ d „  ƒ ƒ ƒ Z
 e d ƒ e d ƒ e d ƒ e d „ ƒ ƒ ƒ Z d „  Z e d d ƒ e d ƒ e d ƒ d „  ƒ ƒ ƒ Z e d ƒ e d ƒ e d ƒ d „  ƒ ƒ ƒ Z e d ƒ d „  ƒ Z d „  Z d S(   s*   
Communicability and centrality measures.
iÿÿÿÿN(   t   *s   
s   Aric Hagberg (hagberg@lanl.gov)s$   Franck Kalala (franckkalala@yahoo.frt   communicability_centrality_expt   communicability_centralityt&   communicability_betweenness_centralityt   communicabilityt   communicability_expt   estrada_indext   scipyt   directedt
   multigraphc         C   st   d d l  } |  j ƒ  } t j |  | ƒ } d | | d k <| j j | ƒ } t t | t t	 | j
 ƒ  ƒ ƒ ƒ } | S(   sÐ  Return the communicability centrality for each node of G

    Communicability centrality, also called subgraph centrality, of a node `n`
    is the sum of closed walks of all lengths starting and ending at node `n`.

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

    Returns
    -------
    nodes:dictionary
        Dictionary of nodes with communicability centrality as the value.

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

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

    Notes
    -----
    This version of the algorithm exponentiates the adjacency matrix.
    The communicability centrality of a node `u` in G can be found using
    the matrix exponential of the adjacency matrix of G  [1]_ [2]_,

    .. math::

        SC(u)=(e^A)_{uu} .

    References
    ----------
    .. [1] Ernesto Estrada, Juan A. Rodriguez-Velazquez,
       "Subgraph centrality in complex networks",
       Physical Review E 71, 056103 (2005).
       http://arxiv.org/abs/cond-mat/0504730

    .. [2] Ernesto Estrada, Naomichi Hatano,
       "Communicability in complex networks",
       Phys. Rev. E 77, 036111 (2008).
       http://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)])
    >>> sc = nx.communicability_centrality_exp(G)
    iÿÿÿÿNi   g        (   t   scipy.linalgt   nodest   nxt   to_numpy_matrixt   linalgt   expmt   dictt   zipt   mapt   floatt   diagonal(   t   GR   t   nodelistt   At   expAt   sc(    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyR      s    :$t   numpyc   
      C   s´   d d l  } d d l } |  j ƒ  } t j |  | ƒ } d | | d k <| j j | ƒ \ } } | j | ƒ d } | j | ƒ } | j	 | | ƒ } t
 t | t t | ƒ ƒ ƒ }	 |	 S(   s  Return communicability centrality for each node in G.

    Communicability centrality, also called subgraph centrality, of a node `n`
    is the sum of closed walks of all lengths starting and ending at node `n`.

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

    Returns
    -------
    nodes: dictionary
       Dictionary of nodes with communicability centrality as the value.

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

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

    Notes
    -----
    This version of the algorithm computes eigenvalues and eigenvectors
    of the adjacency matrix.

    Communicability centrality of a node `u` in G can be found using
    a spectral decomposition of the adjacency matrix [1]_ [2]_,

    .. math::

       SC(u)=\sum_{j=1}^{N}(v_{j}^{u})^2 e^{\lambda_{j}},

    where `v_j` is an eigenvector of the adjacency matrix `A` of G
    corresponding corresponding to the eigenvalue `\lambda_j`.

    Examples
    --------
    >>> G = nx.Graph([(0,1),(1,2),(1,5),(5,4),(2,4),(2,3),(4,3),(3,6)])
    >>> sc = nx.communicability_centrality(G)

    References
    ----------
    .. [1] Ernesto Estrada, Juan A. Rodriguez-Velazquez,
       "Subgraph centrality in complex networks",
       Physical Review E 71, 056103 (2005).
       http://arxiv.org/abs/cond-mat/0504730
    .. [2] Ernesto Estrada, Naomichi Hatano,
       "Communicability in complex networks",
       Phys. Rev. E 77, 036111 (2008).
       http://arxiv.org/abs/0707.0756
    iÿÿÿÿNi   g        i   (   R   t   numpy.linalgR   R   R   R   t   eight   arrayt   expt   dotR   R   R   R   (
   R   R   R   R   t   wt   vt   vsquaret   expwt   xgR   (    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyR   Z   s    =c         C   s¾  d d l  } d d l } |  j ƒ  } t | ƒ } t j |  | ƒ } d | | d k <| j j | ƒ } t t	 | t
 | ƒ ƒ ƒ } i  } x |  D]}	 | |	 }
 | |
 d d … f j ƒ  } | d d … |
 f j ƒ  } d | |
 d d … f <d | d d … |
 f <| | j j | ƒ | } d | |
 d d … f <d | d d … |
 f <| | j | j | ƒ ƒ 8} t | j ƒ  ƒ | |	 <| | |
 d d … f <| | d d … |
 f <qŒ Wt | d | ƒ} | S(   sÒ  Return communicability betweenness for all pairs of nodes in G.

    Communicability betweenness measure makes use of the number of walks
    connecting every pair of nodes as the basis of a betweenness centrality
    measure.

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

    Returns
    -------
    nodes:dictionary
        Dictionary of nodes with communicability betweenness as the value.

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

    See Also
    --------
    communicability:
       Communicability between all pairs of nodes in G.
    communicability_centrality:
       Communicability centrality for each node of G using matrix exponential.
    communicability_centrality_exp:
       Communicability centrality for each node in G using
       spectral decomposition.

    Notes
    -----
    Let `G=(V,E)` be a simple undirected graph with `n` nodes and `m` edges,
    and `A` denote the adjacency matrix of `G`.

    Let `G(r)=(V,E(r))` be the graph resulting from
    removing all edges connected to node `r` but not the node itself.

    The adjacency matrix for `G(r)` is `A+E(r)`,  where `E(r)` has nonzeros
    only in row and column `r`.

    The communicability betweenness of a node `r`  is [1]_

    .. math::
         \omega_{r} = \frac{1}{C}\sum_{p}\sum_{q}\frac{G_{prq}}{G_{pq}},
         p\neq q, q\neq r,

    where
    `G_{prq}=(e^{A}_{pq} - (e^{A+E(r)})_{pq}`  is the number of walks
    involving node r,
    `G_{pq}=(e^{A})_{pq}` is the number of closed walks starting
    at node `p` and ending at node `q`,
    and `C=(n-1)^{2}-(n-1)` is a normalization factor equal to the
    number of terms in the sum.

    The resulting `\omega_{r}` takes values between zero and one.
    The lower bound cannot be attained for a connected
    graph, and the upper bound is attained in the star graph.

    References
    ----------
    .. [1] Ernesto Estrada, Desmond J. Higham, Naomichi Hatano,
       "Communicability Betweenness in Complex Networks"
       Physica A 388 (2009) 764-774.
       http://arxiv.org/abs/0905.4102

    Examples
    --------
    >>> G = nx.Graph([(0,1),(1,2),(1,5),(5,4),(2,4),(2,3),(4,3),(3,6)])
    >>> cbc = nx.communicability_betweenness_centrality(G)
    iÿÿÿÿNi   g        i    t
   normalized(   R   R
   R   t   lenR   R   R   R   R   R   t   ranget   copyt   diagR   t   sumt   _rescale(   R   R%   R   R   t   nR   R   t   mappingR   R!   t   it   rowt   colt   B(    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyR   ¥   s0    K
c         C   s~   | t  k rJ t |  ƒ } | d k r- d  } qJ d | d d | d } n  | d  k	 rz x! |  D] } |  | c | 9<q] Wn  |  S(   Ni   g      ð?(   t   TrueR&   t   None(   R   R%   t   ordert   scaleR!   (    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyR+     s    	c         C   sY  d d l  } d d l } |  j ƒ  } 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_centrality_exp:
       Communicability centrality for each node of G using matrix exponential.
    communicability_centrality:
       Communicability centrality for each node in G using spectral
       decomposition.
    communicability:
       Communicability between pairs of nodes 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).
       http://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    (   R   R
   R   R   R   R   R   R   R   R   R'   R&   R   (   R   R   R   R   R   R    t   vecR#   R-   R   t   uR!   t   st   pt   qt   j(    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyR     s&    =!


Nc   	      C   sÉ   d d l  } |  j ƒ  } t j |  | ƒ } d | | d k <| j j | ƒ } t t | t t	 | ƒ ƒ ƒ ƒ } i  } xO |  D]G } i  | | <x4 |  D], } t
 | | | | | f ƒ | | | <q‘ Wqz W| S(   sN  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_centrality_exp:
       Communicability centrality for each node of G using matrix exponential.
    communicability_centrality:
       Communicability centrality for each node in G using spectral
       decomposition.
    communicability_exp:
       Communicability between all pairs of nodes in G  using spectral
       decomposition.

    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).
       http://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   R   R   R   R'   R&   R   (	   R   R   R   R   R   R-   R   R7   R!   (    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyR   o  s    <!
.c         C   s   t  t |  ƒ j ƒ  ƒ S(   s^  Return the Estrada index of a the graph G.

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

    Returns
    -------
    estrada index: float

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

    See also
    --------
    estrada_index_exp

    Notes
    -----
    Let `G=(V,E)` be a simple undirected graph with `n` nodes  and let
    `\lambda_{1}\leq\lambda_{2}\leq\cdots\lambda_{n}`
    be a non-increasing ordering of the eigenvalues of its adjacency
    matrix `A`.  The Estrada index is

    .. math::
        EE(G)=\sum_{j=1}^n e^{\lambda _j}.

    References
    ----------
    .. [1] E. Estrada,  Characterization of 3D molecular structure,
       Chem. Phys. Lett. 319, 713 (2000).

    Examples
    --------
    >>> G=nx.Graph([(0,1),(1,2),(1,5),(5,4),(2,4),(2,3),(4,3),(3,6)])
    >>> ei=nx.estrada_index(G)
    (   R*   R   t   values(   R   (    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyR   º  s    )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   (    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyt   setup_moduleæ  s    (   t   __doc__t   networkxR   t   networkx.utilst   joint
   __author__t   __all__t   requiret   not_implemented_forR   R   R2   R   R+   R   R   R   R@   (    (    (    sj   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/centrality/communicability_alg.pyt   <module>   s:   
				B		I			f		Q		I,