a
HlX`v)@sTdZddlmZddlZddlmZddgZeddd
dZeddddZ dS)zKatz centrality.)sqrtN)not_implemented_forkatz_centralitykatz_centrality_numpyZ
multigraph皙??ư>TcstdkriS}dur0ddDnztt} WnJtttfy}
z,} ttkr~t
d
WYd}
~
n
d}
~
00tD]}tdD]:}D],}


d7<qqD]} <qt
fddD}krrz"d tt
d
dD}Wntyxd }Yn0nd}D]}9<qSqt dS)uCompute the Katz centrality for the nodes of the graph G.
Katz centrality computes the centrality for a node based on the centrality
of its neighbors. It is a generalization of the eigenvector centrality. The
Katz centrality for node $i$ is
.. math::
x_i = \alpha \sum_{j} A_{ij} x_j + \beta,
where $A$ is the adjacency matrix of graph G with eigenvalues $\lambda$.
The parameter $\beta$ controls the initial centrality and
.. math::
\alpha < \frac{1}{\lambda_{\max}}.
Katz centrality computes the relative influence of a node within a
network by measuring the number of the immediate neighbors (first
degree nodes) and also all other nodes in the network that connect
to the node under consideration through these immediate neighbors.
Extra weight can be provided to immediate neighbors through the
parameter $\beta$. Connections made with distant neighbors
are, however, penalized by an attenuation factor $\alpha$ which
should be strictly less than the inverse largest eigenvalue of the
adjacency matrix in order for the Katz centrality to be computed
correctly. More information is provided in [1]_.
Parameters

G : graph
A NetworkX graph.
alpha : float
Attenuation factor
beta : scalar or dictionary, optional (default=1.0)
Weight attributed to the immediate neighborhood. If not a scalar, the
dictionary must have an value for every node.
max_iter : integer, optional (default=1000)
Maximum number of iterations in power method.
tol : float, optional (default=1.0e6)
Error tolerance used to check convergence in power method iteration.
nstart : dictionary, optional
Starting value of Katz iteration for each node.
normalized : bool, optional (default=True)
If True normalize the resulting values.
weight : None or string, optional (default=None)
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 Katz centrality as the value.
Raises

NetworkXError
If the parameter `beta` is not a scalar but lacks a value for at least
one node
PowerIterationFailedConvergence
If the algorithm fails to converge to the specified tolerance
within the specified number of iterations of the power iteration
method.
Examples

>>> import math
>>> G = nx.path_graph(4)
>>> phi = (1 + math.sqrt(5)) / 2.0 # largest eigenvalue of adj matrix
>>> centrality = nx.katz_centrality(G, 1 / phi  0.01)
>>> for n, c in sorted(centrality.items()):
... print(f"{n} {c:.2f}")
0 0.37
1 0.60
2 0.60
3 0.37
See Also

katz_centrality_numpy
eigenvector_centrality
eigenvector_centrality_numpy
pagerank
hits
Notes

Katz centrality was introduced by [2]_.
This algorithm it uses the power method to find the eigenvector
corresponding to the largest eigenvalue of the adjacency matrix of ``G``.
The parameter ``alpha`` should be strictly less than the inverse of largest
eigenvalue of the adjacency matrix for the algorithm to converge.
You can use ``max(nx.adjacency_spectrum(G))`` to get $\lambda_{\max}$ the largest
eigenvalue of the adjacency matrix.
The iteration will stop after ``max_iter`` iterations or an error tolerance of
``number_of_nodes(G) * tol`` has been reached.
When $\alpha = 1/\lambda_{\max}$ and $\beta=0$, Katz centrality is the same
as eigenvector centrality.
For directed graphs this finds "left" eigenvectors which corresponds
to the inedges in the graph. For outedges Katz centrality
first reverse the graph with ``G.reverse()``.
References

.. [1] Mark E. J. Newman:
Networks: An Introduction.
Oxford University Press, USA, 2010, p. 720.
.. [2] Leo Katz:
A New Status Index Derived from Sociometric Index.
Psychometrika 18(1):39–43, 1953
http://phya.snu.ac.kr/~dkim/PRL87278701.pdf
rNcSsi]
}dqS)r.0nr
r
/Users/cmdms/OneDriveUOB/Development/Projects/2021/samconsensusv3/env/lib/python3.9/sitepackages/networkx/algorithms/centrality/katz.py
z#katz_centrality..0beta dictionary must have a value for every nodecs g]}tqSr
)absrxZxlastr
r
rz#katz_centrality..rcss]}dVqdS)Nr
)rvr
r
r rz"katz_centrality..)lenZnumber_of_nodesdictfromkeysfloat TypeError
ValueErrorAttributeErrorsetnx
NetworkXErrorrangegetsumrvaluesZeroDivisionErrorZPowerIterationFailedConvergence)GalphabetaZmax_iterZtolZnstart
normalizedweightZnnodesbeir
Znbrerrsr
rrr
sJ ,"
c
Cszzddl}Wn.ty:}ztdWYd}~n
d}~00tdkrLiSz<}ttkrptdjt t
d}Wnptyt}ztdft
}Wn6t
ttfy}ztdWYd}~n
d}~00Yn0tjdj}  jd}
j

 }rZtj}nd }tttt
}S)
u
Compute the Katz centrality for the graph G.
Katz centrality computes the centrality for a node based on the centrality
of its neighbors. It is a generalization of the eigenvector centrality. The
Katz centrality for node $i$ is
.. math::
x_i = \alpha \sum_{j} A_{ij} x_j + \beta,
where $A$ is the adjacency matrix of graph G with eigenvalues $\lambda$.
The parameter $\beta$ controls the initial centrality and
.. math::
\alpha < \frac{1}{\lambda_{\max}}.
Katz centrality computes the relative influence of a node within a
network by measuring the number of the immediate neighbors (first
degree nodes) and also all other nodes in the network that connect
to the node under consideration through these immediate neighbors.
Extra weight can be provided to immediate neighbors through the
parameter $\beta$. Connections made with distant neighbors
are, however, penalized by an attenuation factor $\alpha$ which
should be strictly less than the inverse largest eigenvalue of the
adjacency matrix in order for the Katz centrality to be computed
correctly. More information is provided in [1]_.
Parameters

G : graph
A NetworkX graph
alpha : float
Attenuation factor
beta : scalar or dictionary, optional (default=1.0)
Weight attributed to the immediate neighborhood. If not a scalar the
dictionary must have an value for every node.
normalized : bool
If True normalize the resulting values.
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 Katz centrality as the value.
Raises

NetworkXError
If the parameter `beta` is not a scalar but lacks a value for at least
one node
Examples

>>> import math
>>> G = nx.path_graph(4)
>>> phi = (1 + math.sqrt(5)) / 2.0 # largest eigenvalue of adj matrix
>>> centrality = nx.katz_centrality_numpy(G, 1 / phi)
>>> for n, c in sorted(centrality.items()):
... print(f"{n} {c:.2f}")
0 0.37
1 0.60
2 0.60
3 0.37
See Also

katz_centrality
eigenvector_centrality_numpy
eigenvector_centrality
pagerank
hits
Notes

Katz centrality was introduced by [2]_.
This algorithm uses a direct linear solver to solve the above equation.
The parameter ``alpha`` should be strictly less than the inverse of largest
eigenvalue of the adjacency matrix for there to be a solution.
You can use ``max(nx.adjacency_spectrum(G))`` to get $\lambda_{\max}$ the largest
eigenvalue of the adjacency matrix.
When $\alpha = 1/\lambda_{\max}$ and $\beta=0$, Katz centrality is the same
as eigenvector centrality.
For directed graphs this finds "left" eigenvectors which corresponds
to the inedges in the graph. For outedges Katz centrality
first reverse the graph with ``G.reverse()``.
References

.. [1] Mark E. J. Newman:
Networks: An Introduction.
Oxford University Press, USA, 2010, p. 720.
.. [2] Leo Katz:
A New Status Index Derived from Sociometric Index.
Psychometrika 18(1):39–43, 1953
http://phya.snu.ac.kr/~dkim/PRL87278701.pdf
rNz!Requires NumPy: http://numpy.org/r)Zdtyperzbeta must be a number)nodelistrr)numpyImportErrorrkeysr!r"r#arraylistr'rr ZonesrrZ
adj_matrixZtodenseTshapeZlinalgZsolveZeyesignr&normrzipmap)
r)r*r+r,rnpr/r3r.Ar
Z
centralityr<r
r
rrs6n (
)rrrr NTN)rrTN)
__doc__mathrZnetworkxr"Znetworkx.utilsr__all__rrr
r
r
rs 9