'How can I calculate the number of geodesics (shortest paths) going through a vertex or an edge in networkx?

The betweeness function in igraph of R defines the edge betweeness as the the number of geodesics (shortest paths) going through.

However, the edge_betweenness_centrality in networkx of python defines it in a traditional way, which seems to require a heavier computational load.

My question is, how can I calculate that of R under networkx to save time?


For example, I created a circular graph containing 2000 nodes and 4000 edges both in R and python. The time spent for computation is presented as follow.

R

library(tidygraph)
library(igraph)

G <- play_smallworld(n_dim = 1, dim_size = 2000,
                     order = 2,
                     p_rewire = 0)

start <- Sys.time()
edge_betweenness(G)
end <- Sys.time()

print(end - start)
Time difference of 0.1190271 secs

Python

import networkx as nx
import time

G = nx.watts_strogatz_graph(n = 2000,
    k = 4,
    p = 0)

start = time.time()
nx.edge_betweenness_centrality(G)
end = time.time()

print('Runing time: %s Seconds' %(end-start))
Runing time: 12.19289755821228 Seconds


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source