ó
|£*^c           @   sd   d  Z  d d l Z d j d d d g ƒ Z d d g Z d	 d
 d d d „ Z d d „ Z d „  Z	 d S(   s   
Eigenvector centrality.
iÿÿÿÿNs   
s%   Aric Hagberg (aric.hagberg@gmail.com)s   Pieter Swart (swart@lanl.gov)s#   Sasha Gutfraind (ag362@cornell.edu)t   eigenvector_centralityt   eigenvector_centrality_numpyid   g�íµ ÷Æ°>t   weightc      
   C   s5  d d l  m } t |  ƒ t j k s: t |  ƒ t j k rL t j d ƒ ‚ n  t |  ƒ d k rp t j d ƒ ‚ n  | d
 k r® t	 g  |  D] } | d t |  ƒ f ^ q† ƒ } n | } d t
 | j ƒ  ƒ } x | D] }	 | |	 c | 9<qÑ W|  j ƒ  }
 x(t | ƒ D]} | } t	 j | d ƒ } xO | D]G } x> |  | D]2 } | | c | | |  | | j | d ƒ 7<q:Wq)Wy* d | t
 d „  | j ƒ  Dƒ ƒ ƒ } Wn t k
 r·d } n Xx | D] } | | c | 9<q¿Wt
 g  | D] } t | | | | ƒ ^ qãƒ } | |
 | k  r| SqWt j d	 ƒ ‚ d
 S(   sG	  Compute the eigenvector centrality for the graph G.

    Eigenvector centrality computes the centrality for a node based on the
    centrality of its neighbors. The eigenvector centrality for node `i` is

    .. math::

        \mathbf{Ax} = \lambda \mathbf{x}

    where `A` is the adjacency matrix of the graph G with eigenvalue `\lambda`.
    By virtue of the Perronâ€“Frobenius theorem, there is a unique and positive
    solution if `\lambda` is the largest eigenvalue associated with the
    eigenvector of the adjacency matrix `A` ([2]_).

    Parameters
    ----------
    G : graph
      A networkx graph

    max_iter : integer, optional
      Maximum number of iterations in power method.

    tol : float, optional
      Error tolerance used to check convergence in power method iteration.

    nstart : dictionary, optional
      Starting value of eigenvector iteration for each node.

    weight : None or string, optional
      If None, all edge weights are considered equal.
      Otherwise holds the name of the edge attribute used as weight.

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

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> centrality = nx.eigenvector_centrality(G)
    >>> print(['%s %0.2f'%(node,centrality[node]) for node in centrality])
    ['0 0.37', '1 0.60', '2 0.60', '3 0.37']

    See Also
    --------
    eigenvector_centrality_numpy
    pagerank
    hits

    Notes
    -----
    The measure was introduced by [1]_.

    The eigenvector calculation is done by the power iteration method and has
    no guarantee of convergence. The iteration will stop after ``max_iter``
    iterations or an error tolerance of ``number_of_nodes(G)*tol`` has been
    reached.

    For directed graphs this is "left" eigenvector centrality which corresponds
    to the in-edges in the graph. For out-edges eigenvector centrality
    first reverse the graph with ``G.reverse()``.

    References
    ----------
    .. [1] Phillip Bonacich:
       Power and Centrality: A Family of Measures.
       American Journal of Sociology 92(5):1170â€“1182, 1986
       http://www.leonidzhukov.net/hse/2014/socialnetworks/papers/Bonacich-Centrality.pdf
    .. [2] Mark E. J. Newman:
       Networks: An Introduction.
       Oxford University Press, USA, 2010, pp. 169.

    iÿÿÿÿ(   t   sqrts   Not defined for multigraphs.i    s   Empty graph.g      ð?i   c         s   s   |  ] } | d  Vq d S(   i   N(    (   t   .0t   v(    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/eigenvector.pys	   <genexpr>y   s    sV   eigenvector_centrality():
power iteration failed to converge in %d iterations."%(i+1))N(   t   mathR   t   typet   nxt
   MultiGrapht   MultiDiGrapht   NetworkXExceptiont   lent   Nonet   dictt   sumt   valuest   number_of_nodest   ranget   fromkeyst   gett   ZeroDivisionErrort   abst   NetworkXError(   t   Gt   max_itert   tolt   nstartR   R   t   nt   xt   st   kt   nnodest   it   xlastt   nbrt   err(    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/eigenvector.pyR       s:    L*24*
1c   
      C   så   d d l  } d d l m } t |  ƒ d k r@ t j d ƒ ‚ n  t j |  d |  j ƒ  d | d t ƒ} | j	 | j
 d	 d
 d d ƒ\ } } | j ƒ  j } | j | j ƒ  ƒ | j j | ƒ } t t |  t t | | ƒ ƒ ƒ }	 |	 S(   sœ  Compute the eigenvector centrality for the graph G.

    Eigenvector centrality computes the centrality for a node based on the
    centrality of its neighbors. The eigenvector centrality for node `i` is

    .. math::

        \mathbf{Ax} = \lambda \mathbf{x}

    where `A` is the adjacency matrix of the graph G with eigenvalue `\lambda`.
    By virtue of the Perronâ€“Frobenius theorem, there is a unique and positive
    solution if `\lambda` is the largest eigenvalue associated with the
    eigenvector of the adjacency matrix `A` ([2]_).

    Parameters
    ----------
    G : graph
      A networkx graph

    weight : None or string, optional
      The name of the edge attribute used as weight.
      If None, all edge weights are considered equal.

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

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> centrality = nx.eigenvector_centrality_numpy(G)
    >>> print(['%s %0.2f'%(node,centrality[node]) for node in centrality])
    ['0 0.37', '1 0.60', '2 0.60', '3 0.37']

    See Also
    --------
    eigenvector_centrality
    pagerank
    hits

    Notes
    -----
    The measure was introduced by [1]_.

    This algorithm uses the SciPy sparse eigenvalue solver (ARPACK) to
    find the largest eigenvalue/eigenvector pair.

    For directed graphs this is "left" eigenvector centrality which corresponds
    to the in-edges in the graph. For out-edges eigenvector centrality
    first reverse the graph with G.reverse().

    References
    ----------
    .. [1] Phillip Bonacich:
       Power and Centrality: A Family of Measures.
       American Journal of Sociology 92(5):1170â€“1182, 1986
       http://www.leonidzhukov.net/hse/2014/socialnetworks/papers/Bonacich-Centrality.pdf
    .. [2] Mark E. J. Newman:
       Networks: An Introduction.
       Oxford University Press, USA, 2010, pp. 169.
    iÿÿÿÿN(   t   linalgi    s   Empty graph.t   nodelistR   t   dtypeR   i   t   whicht   LR(   t   scipyt   scipy.sparseR%   R   R   R   t   to_scipy_sparse_matrixt   nodest   floatt   eigst   Tt   flattent   realt   signR   t   normR   t   zipt   map(
   R   R   t   spR%   t   Mt
   eigenvaluet   eigenvectort   largestR4   t
   centrality(    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/eigenvector.pyR   ˆ   s    ?	$%"c         C   s:   d d l  m } y d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   SciPy not available(   t   noseR=   R*   (   t   moduleR=   R*   (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/eigenvector.pyt   setup_moduleÕ   s
    (
   t   __doc__t   networkxR   t   joint
   __author__t   __all__R   R    R   R@   (    (    (    s~   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/centrality/eigenvector.pyt   <module>   s   			uM