Skip to content

Commit f308527

Browse files
committed
Make approximate_current_flow_betweenness_centrality match other centrality algorithms
1 parent 1aff23f commit f308527

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

networkx/algorithms/centrality/current_flow_betweenness.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ def approximate_current_flow_betweenness_centrality(
113113
raise nx.NetworkXError("Graph not connected.")
114114

115115
n = G.number_of_nodes()
116-
if n < 3 and normalized:
117-
raise nx.NetworkXError(
118-
"Graph has fewer than 3 nodes. Normalization factor [(n-1)(n-2)] would be zero. "
119-
"To avoid a divide-by-zero, add more nodes or set normalized=False."
120-
)
116+
117+
# For small graphs (n < 3), betweenness centrality is always 0 for all nodes
118+
# since no node can be "between" any pair of other nodes
119+
if n < 3:
120+
return dict.fromkeys(G, 0.0)
121121

122122
if epsilon <= 0:
123123
raise nx.NetworkXError(f"Epsilon must be positive. Got epsilon={epsilon}.")
@@ -129,7 +129,7 @@ def approximate_current_flow_betweenness_centrality(
129129

130130
nb = (n - 1.0) * (n - 2.0) # normalization factor
131131
cstar = n * (n - 1) / nb
132-
k = sample_weight * int(np.ceil((cstar / epsilon) ** 2 * np.log(n)))
132+
k = int(sample_weight * np.ceil((cstar / epsilon) ** 2 * np.log(n)))
133133
if k > kmax:
134134
msg = f"Number random pairs k>kmax ({k}>{kmax}) "
135135
raise nx.NetworkXError(msg, "Increase kmax or epsilon")

0 commit comments

Comments
 (0)