# Katz Centrality (Centrality Measure)

In graph theory, the Katz centrality of a node is a measure of centrality in a network. It was introduced by Leo Katz in 1953 and is used to measure the relative degree of influence of an actor (or node) within a social network. Unlike typical centrality measures which consider only the shortest path (the geodesic) between a pair of actors, Katz centrality measures influence by taking into account the total number of walks between a pair of actors.

It is similar to Google’s PageRank and to the eigenvector centrality.

**Measuring Katz centrality**

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. Connections made with distant neighbors are, however, penalized by an attenuation factor . Each path or connection between a pair of nodes is assigned a weight determined by and the distance between nodes as .

For example, in the figure on the right, assume that John’s centrality is being measured and that . The weight assigned to each link that connects John with his immediate neighbors Jane and Bob will be . Since Jose connects to John indirectly through Bob, the weight assigned to this connection (composed of two links) will be . Similarly, the weight assigned to the connection between Agneta and John through Aziz and Jane will be and the weight assigned to the connection between Agneta and John through Diego, Jose and Bob will be .

**Mathematical formulation**

Let A be the adjacency matrix of a network under consideration. Elements of A are variables that take a value 1 if a node i is connected to node j and 0 otherwise. The powers of A indicate the presence (or absence) of links between two nodes through intermediaries. For instance, in matrix , if element , it indicates that node 2 and node 12 are connected through some first and second degree neighbors of node 2. If denotes Katz centrality of a node i, then mathematically:

Note that the above definition uses the fact that the element at location of the adjacency matrix raised to the power (i.e. ) reflects the total number of degree connections between nodes and . The value of the attenuation factor has to be chosen such that it is smaller than the reciprocal of the absolute value of the largest eigenvalue of the adjacency matrix A. In this case the following expression can be used to calculate Katz centrality:

Here is the identity matrix, is an identity vector of size n (n is the number of nodes) consisting of ones. denotes the transposed matrix of A and ( denotes matrix inversion of the term ( ).

Following is the code for the calculation of the Katz Centrality of the graph and its various nodes.

`def` `katz_centrality(G, alpha` `=` `0.1` `, beta` `=` `1.0` `, ` ` ` `max_iter` `=` `1000` `, tol` `=` `1.0e` `-` `6` `, ` ` ` `nstart` `=` `None` `, normalized` `=` `True` `, ` ` ` `weight ` `=` `'weight'` `): ` ` ` `"""Compute 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 the 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 :math:`\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. ` ` ` ` ` ` ` `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.0e-6) ` ` ` `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 ` ` ` `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 ` ` ` ` ` ` ` ` ` `Notes ` ` ` `----- ` ` ` ` ` `This algorithm it uses the power method to find ` ` ` `the eigenvector corresponding to the largest ` ` ` `eigenvalue of the adjacency matrix of G. ` ` ` `The constant alpha should be strictly less than ` ` ` `the inverse of largest eigenvalue of the adjacency ` ` ` `matrix for the algorithm to converge. ` ` ` `The iteration will stop after max_iter iterations ` ` ` `or an error tolerance ofnumber_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 in-edges in the graph. ` ` ` `For out-edges Katz centrality first reverse the ` ` ` `graph with G.reverse(). ` ` ` ` ` ` ` `"""` ` ` `from` `math ` `import` `sqrt ` ` ` ` ` `if` `len` `(G) ` `=` `=` `0` `: ` ` ` `return` `{} ` ` ` ` ` `nnodes ` `=` `G.number_of_nodes() ` ` ` ` ` `if` `nstart ` `is` `None` `: ` ` ` ` ` `# choose starting vector with entries of 0 ` ` ` `x ` `=` `dict` `([(n,` `0` `) ` `for` `n ` `in` `G]) ` ` ` `else` `: ` ` ` `x ` `=` `nstart ` ` ` ` ` `try` `: ` ` ` `b ` `=` `dict` `.fromkeys(G,` `float` `(beta)) ` ` ` `except` `(TypeError,ValueError,AttributeError): ` ` ` `b ` `=` `beta ` ` ` `if` `set` `(beta) !` `=` `set` `(G): ` ` ` `raise` `nx.NetworkXError(` `'beta dictionary '` ` ` `'must have a value for every node'` `) ` ` ` ` ` `# make up to max_iter iterations ` ` ` `for` `i ` `in` `range` `(max_iter): ` ` ` `xlast ` `=` `x ` ` ` `x ` `=` `dict` `.fromkeys(xlast, ` `0` `) ` ` ` ` ` `# do the multiplication y^T = Alpha * x^T A - Beta ` ` ` `for` `n ` `in` `x: ` ` ` `for` `nbr ` `in` `G[n]: ` ` ` `x[nbr] ` `+` `=` `xlast[n] ` `*` `G[n][nbr].get(weight, ` `1` `) ` ` ` `for` `n ` `in` `x: ` ` ` `x[n] ` `=` `alpha` `*` `x[n] ` `+` `b[n] ` ` ` ` ` `# check convergence ` ` ` `err ` `=` `sum` `([` `abs` `(x[n]` `-` `xlast[n]) ` `for` `n ` `in` `x]) ` ` ` `if` `err < nnodes` `*` `tol: ` ` ` `if` `normalized: ` ` ` ` ` `# normalize vector ` ` ` `try` `: ` ` ` `s ` `=` `1.0` `/` `sqrt(` `sum` `(v` `*` `*` `2` `for` `v ` `in` `x.values())) ` ` ` ` ` `# this should never be zero? ` ` ` `except` `ZeroDivisionError: ` ` ` `s ` `=` `1.0` ` ` `else` `: ` ` ` `s ` `=` `1` ` ` `for` `n ` `in` `x: ` ` ` `x[n] ` `*` `=` `s ` ` ` `return` `x ` ` ` ` ` `raise` `nx.NetworkXError(` `'Power iteration failed to converge in '` ` ` `'%d iterations.'` `%` `max_iter) ` |

*chevron_right*

*filter_none*

The above function is invoked using the networkx library and once the library is installed, you can eventually use it and the following code has to be written in python for the implementation of the katz centrality of a node.

`>>> ` `import` `networkx as nx ` `>>> ` `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` `(` `"%d %0.2f"` `%` `(n,c)) ` |

*chevron_right*

*filter_none*

The output of the above code is:

`0` `0.37` `1` `0.60` `2` `0.60` `3` `0.37` |

*chevron_right*

*filter_none*

The above result is a dictionary depicting the value of katz centrality of each node. The above is an extension of my article series on the centrality measures. Keep networking!!!

**References**

http://networkx.readthedocs.io/en/networkx-1.10/index.html

https://en.wikipedia.org/wiki/Katz_centrality

## Recommended Posts:

- Betweenness Centrality (Centrality Measure)
- Degree Centrality (Centrality Measure)
- Network Centrality Measures in a Graph using Networkx | Python
- ML | V-Measure for Evaluating Clustering Performance
- Statistical Functions in Python | Set 2 ( Measure of Spread)
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Python | Measure similarity between two sentences using cosine similarity
- Createview - Class Based Views Django
- Sorting Algorithm Visualization : Merge Sort
- Django CRUD (Create, Retrieve, Update, Delete) Function Based Views
- Types of inheritance Python
- Using Google Cloud Function to generate data for Machine Learning model
- Difference between List and Dictionary in Python
- List View - Function based Views Django

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.