Degree centrality
Historically, the first centrality was the degree centrality. Conceptually, it is the simplest algorithm to measure centrality. Degree centrality is defined as the number of connections a node has. Here, we have to differentiate two types of edges - edges that go “in” the node and edges that go “out” of the node. Therefore, there are two separate measures of degree centrality - indegree and outdegree. Indegree centrality is often interpreted as a form of popularity and outdegree as a form of companionship.
Usage in NetworkX
degree_centrality(G)
Not fast enough? Find 100x faster algorithms here.
Example
First save locally graph.gexf
to run the below example.
- Python code
- Output
import networkx as nx
import matplotlib.pyplot as plt
G = nx.read_gexf("graph.gexf")
centrality = nx.degree_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 degree 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.