GRAPH-CENTRALITY
betweenness_centrality
Compute the shortest-path betweenness centrality for nodes.
Example of usage
Compare route hubs using betweenness_centrality on a connected transport network.
Usage / limitations
+Use when: Analyse structural relationships, hub importance or likely missing connections in a transport network.
−Limitations: Depends on network structure, directedness, graph connectivity and the assumptions of the selected measure.
Mathematical formula
Network measure computed over the hub graph; consult the method definition for its mathematical expression.
Sample dataShow
{
"variable": "transport_network",
"domain": "Regional transport network connectivity",
"nodes": [
[
"Hub-A",
{
"community": 0,
"route_group": 0,
"passengers": 1
}
],
[
"Hub-B",
{
"community": 0,
"route_group": 0,
"passengers": 2
}
],
[
"Hub-C",
{
"community": 0,
"route_group": 0,
"passengers": 3
}
],
[
"Hub-D",
{
"community": 1,
"route_group": 1,
"passengers": 4
}
],
[
"Hub-E",
{
"community": 1,
"route_group": 1,
"passengers": 5
}
],
[
"Hub-F",
{
"community": 1,
"route_group": 1,
"passengers": 6
}
]
],
"edges": [
[
"Hub-A",
"Hub-B"
],
[
"Hub-A",
"Hub-C"
],
[
"Hub-B",
"Hub-C"
],
[
"Hub-C",
"Hub-D"
],
[
"Hub-D",
"Hub-E"
],
[
"Hub-D",
"Hub-F"
],
[
"Hub-E",
"Hub-F"
]
],
"directed_edges": [
[
"Hub-A",
"Hub-B"
],
[
"Hub-B",
"Hub-C"
],
[
"Hub-C",
"Hub-D"
],
[
"Hub-D",
"Hub-E"
],
[
"Hub-E",
"Hub-F"
],
[
"Hub-F",
"Hub-A"
],
[
"Hub-A",
"Hub-C"
],
[
"Hub-C",
"Hub-E"
]
]
}
Code example (Python)
transport_network = {'nodes': [('Hub-A', {'community': 0, 'route_group': 0, 'passengers': 1}), ('Hub-B', {'community': 0, 'route_group': 0, 'passengers': 2}), ('Hub-C', {'community': 0, 'route_group': 0, 'passengers': 3}), ('Hub-D', {'community': 1, 'route_group': 1, 'passengers': 4}), ('Hub-E', {'community': 1, 'route_group': 1, 'passengers': 5}), ('Hub-F', {'community': 1, 'route_group': 1, 'passengers': 6})], 'edges': [('Hub-A', 'Hub-B'), ('Hub-A', 'Hub-C'), ('Hub-B', 'Hub-C'), ('Hub-C', 'Hub-D'), ('Hub-D', 'Hub-E'), ('Hub-D', 'Hub-F'), ('Hub-E', 'Hub-F')], 'directed_edges': [('Hub-A', 'Hub-B'), ('Hub-B', 'Hub-C'), ('Hub-C', 'Hub-D'), ('Hub-D', 'Hub-E'), ('Hub-E', 'Hub-F'), ('Hub-F', 'Hub-A'), ('Hub-A', 'Hub-C'), ('Hub-C', 'Hub-E')]}
# DataLens.ai example — transport network analytics.
# The named transport_network sample is included when copying input + code + test.
import math
import networkx as nx
# Construct an undirected network of connected transport hubs.
G = nx.Graph()
G.add_nodes_from((name, attrs) for name, attrs in transport_network["nodes"])
G.add_edges_from(transport_network["edges"])
# Construct a directed route network when direction is relevant.
D = nx.DiGraph()
D.add_nodes_from((name, attrs) for name, attrs in transport_network["nodes"])
D.add_edges_from(transport_network["directed_edges"])
# Evaluate betweenness_centrality using the network's named hubs.
result = nx.betweenness_centrality(G)
# Verify the result contains meaningful values for this network.
assert result is not None
assert isinstance(result, (dict, list, float, int))
assert len(result) > 0 if isinstance(result, (dict, list)) else math.isfinite(float(result))
# Successful execution and assertions confirm the example.
print("Expected output verified")
# DataLens.ai example — transport network analytics.
# The named transport_network sample is included when copying input + code + test.
import math
import networkx as nx
# Construct an undirected network of connected transport hubs.
G = nx.Graph()
G.add_nodes_from((name, attrs) for name, attrs in transport_network["nodes"])
G.add_edges_from(transport_network["edges"])
# Construct a directed route network when direction is relevant.
D = nx.DiGraph()
D.add_nodes_from((name, attrs) for name, attrs in transport_network["nodes"])
D.add_edges_from(transport_network["directed_edges"])
# Evaluate betweenness_centrality using the network's named hubs.
result = nx.betweenness_centrality(G)
# Verify the result contains meaningful values for this network.
assert result is not None
assert isinstance(result, (dict, list, float, int))
assert len(result) > 0 if isinstance(result, (dict, list)) else math.isfinite(float(result))
# Successful execution and assertions confirm the example.
print("Expected output verified")