Identifying Network Bottlenecks with NetworkX Betweenness Centrality
Learn how to identify critical bottlenecks in your network using NetworkX's betweenness centrality and Brandes' algorithm, including a practical Python example and performance trade-offs.
14 Jul 2026, 11:45 UTC

The Problem: Finding the 'Critical' Node
In a complex network—whether it is a microservices architecture, a logistics chain, or a social graph—the most connected node isn't always the most important. A node with a high degree (many connections) might just be a local hub. The real risk usually lies in the nodes that act as bridges. If a bridge node fails, the network fragments, and communication between distant clusters stops entirely.
The technical solution for this is Betweenness Centrality. Unlike degree centrality, which simply counts connections, betweenness centrality measures how often a node appears on the shortest path between all other pairs of nodes in the graph. The higher the score, the more that node controls the flow of information.
How NetworkX Handles the Math
NetworkX implements betweenness centrality using Brandes' algorithm. In a naive approach, calculating all-pairs shortest paths would be computationally expensive. Brandes' algorithm optimizes this by calculating dependencies from the leaf nodes upward, reducing the time complexity to O(NM) for unweighted graphs, where N is the number of nodes and M is the number of edges.
Under the hood, NetworkX uses a "dictionary-of-dictionaries-of-dictionaries" structure. This means looking up if an edge exists between two nodes happens in O(1) average time, which is critical when the algorithm is traversing thousands of potential paths to determine centrality.
Practical Implementation: Analyzing a Network
To use this in a real scenario, you need the networkx library installed in your Python environment. The following example demonstrates how to identify a bottleneck in a small network of five nodes where one node (Node 2) acts as the sole bridge between two clusters.
import networkx as nx
# Initialize an undirected graph
G = nx.Graph()
# Create two clusters connected by a single bridge node (Node 2)
# Cluster A: 0, 1, 2
# Cluster B: 2, 3, 4
edges = [(0, 1), (1, 2), (0, 2), (2, 3), (3, 4), (2, 4)]
G.add_edges_from(edges)
# Calculate betweenness centrality
# normalized=True scales the values between 0 and 1
centrality = nx.betweenness_centrality(G, normalized=True)
# Sort nodes by centrality score descending
sorted_centrality = sorted(centrality.items(), key=lambda item: item[1], reverse=True)
print("Node Centrality Scores:")
for node, score in sorted_centrality:
print(f"Node {node}: {score:.4f}")
Execution Details
- Environment: Run this in any Python 3.x environment with
pip install networkx. - Permissions: No special system permissions are required as this is a local memory operation.
- Expected Result: Node 2 will have the highest score because any path from {0, 1} to {3, 4} must pass through it.
- Risk: Running this on a graph with tens of thousands of nodes without a workstation-class CPU may lead to long execution times or memory exhaustion.
Trade-offs: Python Flexibility vs. C++ Speed
NetworkX is written in pure Python, which makes it incredibly flexible and easy to debug. You can attach any Python object as a node attribute. However, this flexibility comes with a memory overhead. Because every node and edge is stored in a Python dictionary, the memory footprint is significantly larger than a compressed sparse row (CSR) matrix used by C++ libraries like igraph or Graph-tool.
When to stick with NetworkX:
- Your graph size is under 100,000 nodes.
- You need to rapidly prototype a custom centrality metric.
- Your primary goal is algorithmic analysis rather than high-performance rendering.
When to migrate: If you find your betweenness_centrality calls taking hours or your RAM hitting 90% utilization, you have likely outgrown pure Python and should move to a library with a C++ backend.
Verifying the Results
To verify that the centrality scores are accurate, you can manually trace the shortest paths for a small subset of your graph. For any pair of nodes (s, t), identify all shortest paths; the fraction of those paths that pass through node v is the contribution of that pair to v's centrality. If the manual trace matches the nx.betweenness_centrality output, your topology is being interpreted correctly.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.