I'm trying to compute betweenness centrality for a few nodes in a very large graph. I've been using NetworkX. The problem is that it won't let me choose which nodes I need the centrality for. I tried modifying the source code, but it's giving me errors. Here's what I've done:
betweenness = dict.fromkeys(G, 0.0) # b[v]=0 for v in G
if k is None:
nodes = G
#nodes = nx.Graph()
#for indx in range(0, G.size()):
#if indx in subset:
#nodes.add_node(allNodes[indx])
else:
random.seed(seed)
nodes = random.sample(G.nodes(), k)
for s in nodes:
n = nodes.nodes().index(s)
if n in subset:
# single source shortest paths
if weight is None: # use BFS
S, P, sigma = _single_source_shortest_path_basic(G, s)
else: # use Dijkstra's algorithm
S, P, sigma = _single_source_dijkstra_path_basic(G, s, weight)
# accumulation
if endpoints:
betweenness = _accumulate_endpoints(betweenness, S, P, sigma, s)
else:
betweenness = _accumulate_basic(betweenness, S, P, sigma, s)
# rescaling
betweenness = _rescale(betweenness, len(interCommNodes),
normalized=normalized,
directed=G.is_directed(),
k=k)
return betweenness
Subset contains all the node indices in the original graph. Though "index" appears to be an invalid function and I cannot find a way to obtain it.
Can someone take a look at this?
Thanks!