ó žÃÒYc@ sŽdZddlmZddlmZddlZddlmZddgZ edƒd d ddd „ƒZ dd d d„Z d„Z dS(s/Functions for computing eigenvector centrality.iÿÿÿÿ(tdivision(tsqrtN(tnot_implemented_forteigenvector_centralityteigenvector_centrality_numpyt multigraphidgíµ ÷ư>c  s˜t|ƒdkr$tjdƒ‚nˆd krCd„|Dƒ‰ntd„ˆjƒDƒƒrqtjdƒ‚n‡fd†ˆjƒDƒ‰|jƒ}xét |ƒD]Û}ˆ‰ˆj ƒ‰xOˆD]G}x>||D]2}ˆ|cˆ||||j |dƒ7>> G = nx.path_graph(4) >>> centrality = nx.eigenvector_centrality(G) >>> sorted((v, '{:0.2f}'.format(c)) for v, c in centrality.items()) [(0, '0.37'), (1, '0.60'), (2, '0.60'), (3, '0.37')] Raises ------ NetworkXPointlessConcept If the graph `G` is the null graph. NetworkXError If each value in `nstart` is zero. PowerIterationFailedConvergence If the algorithm fails to converge to the specified tolerance within the specified number of iterations of the power iteration method. See Also -------- eigenvector_centrality_numpy pagerank hits Notes ----- The measure was introduced by [1]_ and is discussed in [2]_. The power iteration method is used to compute the eigenvector and convergence is **not** guaranteed. Our method stops after ``max_iter`` iterations or when the change in the computed vector between two iterations is smaller than an error tolerance of ``G.number_of_nodes() * tol``. This implementation uses ($A + I$) rather than the adjacency matrix $A$ because it shifts the spectrum to enable discerning the correct eigenvector even for networks with multiple dominant eigenvalues. 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 .. [2] Mark E. J. Newman. *Networks: An Introduction.* Oxford University Press, USA, 2010, pp. 169. is,cannot compute centrality for the null graphcS si|]}d|“qS(i((t.0tv((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pys |s cs s|]}|dkVqdS(iN((RR((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pys }ss*initial vector cannot have all zero valuesc s/i|]%\}}|tˆjƒƒ|“qS((tsumtvalues(RtkR(tnstart(s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pys s ics s|]}|dVqdS(iN((Rtz((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pys sc s#i|]\}}|ˆ|“qS(((RR R(tnorm(s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pys s c3 s'|]}tˆ|ˆ|ƒVqdS(N(tabs(Rtn(txtxlast(s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pys ’sN(tlentnxtNetworkXPointlessConcepttNonetallR t NetworkXErrortitemstnumber_of_nodestrangetcopytgetRRtPowerIterationFailedConvergence( tGtmax_iterttolR tweighttnnodestiRtnbr((R R RRs€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pyRs&_    4()i2ic C sâddl}ddlm}t|ƒdkr@tjdƒ‚ntj|dt|ƒd|dtƒ}|j |j d d d d d |d|ƒ\}}|j ƒj } |j | jƒƒ|jj| ƒ} tt|| | ƒƒ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:: Ax = \lambda 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 (default=None) The name of the edge attribute used as weight. If None, all edge weights are considered equal. max_iter : integer, optional (default=100) Maximum number of iterations in power method. tol : float, optional (default=1.0e-6) Relative accuracy for eigenvalues (stopping criterion). The default value of 0 implies machine precision. 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(['{} {:0.2f}'.format(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()``. Raises ------ NetworkXPointlessConcept If the graph ``G`` is the null graph. 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(tlinalgis,cannot compute centrality for the null graphtnodelistR!tdtypeR itwhichtLRtmaxiterR (tscipyt scipy.sparseR%RRRtto_scipy_sparse_matrixtlisttfloatteigstTtflattentrealtsignRR tdicttzip( RR!RR tspR%tMt eigenvaluet eigenvectortlargestR ((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pyR—sK  %cC s:ddlm}yddl}Wn|dƒ‚nXdS(Niÿÿÿÿ(tSkipTestsSciPy not available(tnoseR<R+(tmoduleR<R+((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pyt setup_moduleñs (t__doc__t __future__RtmathRtnetworkxRtnetworkx.utilsRt__all__RRRR?(((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/eigenvector.pyt s    }Z