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
Not fast enough? Find 100x faster algorithms here.
First save locally
graph.gexf to run the below example.
- Python code
import networkx as nx
import matplotlib.pyplot as plt
G = nx.read_gexf("graph.gexf")
centrality = nx.degree_centrality(G)
colors = list(centrality.values())
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.