Skip to main content

Closeness centrality

Another important centrality measure is closeness centrality. Closeness centrality is a measure of centrality in a network calculated as the reciprocal of the sum of the lengths of the shortest paths between the node and all other nodes in the graph. Simply, the closer the node is to other nodes, the more central it is.

Usage in NetworkX

closeness_centrality(G, u=None, distance=None, wf_improved=True)

Not fast enough? Find 100x faster algorithms here.

Example

First save locally graph.gexf to run the below example.

import networkx as nx
import matplotlib.pyplot as plt


G = nx.read_gexf("graph.gexf")

centrality = nx.closeness_centrality(G)
colors = list(centrality.values())

nx.draw_networkx(
G,
nx.spring_layout(G),
node_size=50,
node_color=colors,
edge_color="g",
with_labels=False,
)

plt.axis("off")
plt.show()

Where to next?

There are many graph algorithms libraries out there, with their own implementations of closeness centrality algorithm. NetworkX's algorithms are written in Python, and there are many other libraries that offer faster C++ implementations, such as MAGE, a graph algorithms library developed by Memgraph team.