BETWEENNESS_CENT
Betweenness centrality measures the importance of a node based on the number of shortest paths between all pairs of nodes that pass through it. Nodes with high betweenness serve as bridges or brokers between different parts of a network.
This is useful for identifying bottlenecks in communication networks or key influencers who connect diverse groups in social networks.
The function returns a ranked list of nodes and their centrality scores, sorted from most to least central.
Excel Usage
=BETWEENNESS_CENT(edges, weighted, directed)
edges(list[list], required): 2D array of edges [source, target, weight?].weighted(bool, optional, default: false): If True, use edge weights for shortest path calculations.directed(bool, optional, default: false): If True, treat the graph as directed.
Returns (list[list]): 2D array of [node, score] pairs, sorted by score descending.
Example 1: Diamond graph betweenness
Inputs:
| edges | |
|---|---|
| A | B |
| A | C |
| B | D |
| C | D |
Excel formula:
=BETWEENNESS_CENT({"A","B";"A","C";"B","D";"C","D"})
Expected output:
| Result | |
|---|---|
| A | 0.166667 |
| B | 0.166667 |
| C | 0.166667 |
| D | 0.166667 |
Example 2: Bridge node betweenness
Inputs:
| edges | |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
| 3 | 6 |
| 6 | 7 |
Excel formula:
=BETWEENNESS_CENT({1,2;2,3;3,4;4,5;3,6;6,7})
Expected output:
| Result | |
|---|---|
| 3 | 0.8 |
| 2 | 0.333333 |
| 4 | 0.333333 |
| 6 | 0.333333 |
| 1 | 0 |
| 5 | 0 |
| 7 | 0 |
Example 3: Weighted betweenness
Inputs:
| edges | weighted | ||
|---|---|---|---|
| A | B | 10 | true |
| A | C | 1 | |
| C | B | 1 |
Excel formula:
=BETWEENNESS_CENT({"A","B",10;"A","C",1;"C","B",1}, TRUE)
Expected output:
| Result | |
|---|---|
| C | 1 |
| A | 0 |
| B | 0 |
Example 4: Directed betweenness
Inputs:
| edges | directed | |
|---|---|---|
| A | B | true |
| B | C |
Excel formula:
=BETWEENNESS_CENT({"A","B";"B","C"}, TRUE)
Expected output:
| Result | |
|---|---|
| B | 0.5 |
| A | 0 |
| C | 0 |
Python Code
import networkx as nx
def betweenness_cent(edges, weighted=False, directed=False):
"""
Calculate the shortest-path betweenness centrality for nodes.
See: https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.centrality.betweenness_centrality.html
This example function is provided as-is without any representation of accuracy.
Args:
edges (list[list]): 2D array of edges [source, target, weight?].
weighted (bool, optional): If True, use edge weights for shortest path calculations. Default is False.
directed (bool, optional): If True, treat the graph as directed. Default is False.
Returns:
list[list]: 2D array of [node, score] pairs, sorted by score descending.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
edges = to2d(edges)
if not isinstance(edges, list) or not edges:
return "Error: edges must be a non-empty 2D list"
G = nx.DiGraph() if directed else nx.Graph()
if weighted:
G.add_weighted_edges_from([(str(row[0]), str(row[1]), float(row[2]) if len(row) >= 3 else 1.0) for row in edges if len(row) >= 2])
weight_arg = 'weight'
else:
G.add_edges_from([(str(row[0]), str(row[1])) for row in edges if len(row) >= 2])
weight_arg = None
if not G.nodes:
return "Error: No valid nodes or edges found in input"
scores = nx.betweenness_centrality(G, weight=weight_arg)
# Sort by score descending, then by node name
sorted_scores = sorted(scores.items(), key=lambda x: (-x[1], x[0]))
return [[node, float(score)] for node, score in sorted_scores]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
2D array of edges [source, target, weight?].
If True, use edge weights for shortest path calculations.
If True, treat the graph as directed.