ó žÃÒYc@sÈdZddlZddlTdjddgƒZddd d gZed ƒed ƒd „ƒƒZed ƒed ƒd„ƒƒZ ed ƒed ƒe d„ƒƒZ d„Z d„Z d„ZdS(s5 Subraph centrality and communicability betweenness. iÿÿÿÿN(t*s sAric Hagberg (hagberg@lanl.gov)s$Franck Kalala (franckkalala@yahoo.frtsubgraph_centrality_exptsubgraph_centralityt&communicability_betweenness_centralityt estrada_indextdirectedt multigraphcCswddl}t|ƒ}tj||ƒ}d||dk<|jj|jƒ}tt|t t |j ƒƒƒƒ}|S(sâReturn the subgraph centrality for each node of G. Subgraph centrality of a node `n` is the sum of weighted closed walks of all lengths starting and ending at node `n`. The weights decrease with path length. Each closed walk is associated with a connected subgraph ([1]_). Parameters ---------- G: graph Returns ------- nodes:dictionary Dictionary of nodes with subgraph centrality as the value. Raises ------ NetworkXError If the graph is not undirected and simple. See Also -------- subgraph_centrality: Alternative algorithm of the subgraph centrality for each node of G. Notes ----- This version of the algorithm exponentiates the adjacency matrix. The subgraph centrality of a node `u` in G can be found using the matrix exponential of the adjacency matrix of G [1]_, .. 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). https://arxiv.org/abs/cond-mat/0504730 Examples -------- (Example from [1]_) >>> G = nx.Graph([(1,2),(1,5),(1,8),(2,3),(2,8),(3,4),(3,6),(4,5),(4,7),(5,6),(6,7),(7,8)]) >>> sc = nx.subgraph_centrality_exp(G) >>> print(['%s %0.2f'%(node,sc[node]) for node in sorted(sc)]) ['1 3.90', '2 3.90', '3 3.64', '4 3.71', '5 3.64', '6 3.71', '7 3.64', '8 3.90'] iÿÿÿÿNig( t scipy.linalgtlisttnxtto_numpy_matrixtlinalgtexpmtAtdicttziptmaptfloattdiagonal(tGtscipytnodelistR texpAtsc((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/subgraph_alg.pyRs8  $c Cs·ddl}ddl}t|ƒ}tj||ƒ}d||dk<|jj|jƒ\}}|j|ƒd}|j |ƒ}|j ||ƒ}t t |t t|ƒƒƒ} | S(s™Return subgraph centrality for each node in G. Subgraph centrality of a node `n` is the sum of weighted closed walks of all lengths starting and ending at node `n`. The weights decrease with path length. Each closed walk is associated with a connected subgraph ([1]_). Parameters ---------- G: graph Returns ------- nodes : dictionary Dictionary of nodes with subgraph centrality as the value. Raises ------ NetworkXError If the graph is not undirected and simple. See Also -------- subgraph_centrality_exp: Alternative algorithm of the subgraph centrality for each node of G. Notes ----- This version of the algorithm computes eigenvalues and eigenvectors of the adjacency matrix. Subgraph centrality of a node `u` in G can be found using a spectral decomposition of the adjacency matrix [1]_, .. 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 -------- (Example from [1]_) >>> G = nx.Graph([(1,2),(1,5),(1,8),(2,3),(2,8),(3,4),(3,6),(4,5),(4,7),(5,6),(6,7),(7,8)]) >>> sc = nx.subgraph_centrality(G) >>> print(['%s %0.2f'%(node,sc[node]) for node in sorted(sc)]) ['1 3.90', '2 3.90', '3 3.64', '4 3.71', '5 3.64', '6 3.71', '7 3.64', '8 3.90'] References ---------- .. [1] Ernesto Estrada, Juan A. Rodriguez-Velazquez, "Subgraph centrality in complex networks", Physical Review E 71, 056103 (2005). https://arxiv.org/abs/cond-mat/0504730 iÿÿÿÿNigi(tnumpyt numpy.linalgRR R R teighR tarraytexptdotRRRR( RRRR twtvtvsquaretexpwtxgR((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/subgraph_alg.pyRYs<   cCsÄddl}ddl}t|ƒ}t|ƒ}tj||ƒ}d||dk<|jj|jƒ}t t |t |ƒƒƒ}i}x#|D]} || } || dd…fj ƒ} |dd…| fj ƒ} d|| dd…f>> 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ÿÿÿÿNigit normalized(RRRtlenR R R R R RRtrangetcopytdiagRtsumt_rescale(RR#RRtnR RtmappingtcbcRtitrowtcoltB((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/subgraph_alg.pyR¤s0A      cCs~|tkrJt|ƒ}|dkr-d}qJd|dd|d}n|dk rzx!|D]}||c|9>> 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(Rtvalues(R((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/subgraph_alg.pyRs+cCs`ddlm}yddl}Wn|dƒ‚nXyddl}Wn|dƒ‚nXdS(Niÿÿÿÿ(tSkipTestsNumPy not availablesSciPy not available(tnoseR6RR(tmoduleR6RR((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/subgraph_alg.pyt setup_moduleBs(t__doc__tnetworkxR tnetworkx.utilstjoint __author__t__all__tnot_implemented_forRRR1RR)RR9(((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/subgraph_alg.pyts$      B J  ^  0