How would you speed up the calculation of a custom centrality measure?

Hi - I am trying to find a way to speed up a calculation of straightness centrality, defined here: Centrality Analysis. Betweenness, Closeness, Straightness… | by AxU Platform | Medium

It’s effectively the euclidean_distance (stored as node attributes) / network_dist

I tried using Dask to speed it up but it is still slow; would anyone have any reccomendations to use graph-tools to speed it up?

# def euclidean_dist(x1, y1, x2, y2):
#     return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)

# def bravo(target, k, vertID_dict, network_dist):
#     euclidean_distance = euclidean_dist(vertID_dict[k][0], vertID_dict[k][1], vertID_dict[target][0], vertID_dict[target][1])
#     return euclidean_distance / network_dist

# for k in tqdm(ego_graphs_2000):
#     ego_graph = ego_graphs_2000[k]["graph"]
#     straightness = 0
#     sp = gt.shortest_distance(ego_graph, k, target=gt.shortest_distance weights=ego_graph.edge_properties["mm_len"])

#     if len(sp.get_array()) > 0 and len(G) > 1:
#         for target, value in enumerate(sp):
#             if k != target:
#                 network_dist = sp_scattered[target]
#                 straightness += bravo(target, k, vertID_dict, network_dist)
#         straightness_df[k] = straightness * (1.0 / (len(vertID_dict.keys()) - 1.0))
#     else:
#         straightness_df[k] = 0

Doesn’t the square root taking consume considerable time? I know for non-floating point unit computing, it can have an effect, but I don’t know how much it really takes with math.sqrt, it might be a good idea to test it. If you could do with only using distancesquareds instead of the distances (thus, not having to take squares), you could consider using them instead

Unfortunately, it’s not possible to provide a solution to your problem or an answer to your question with the information provided.

To enable us to understand the situation, you need to provide a minimal working example that shows the problem.

This is very important! If you provide us only the part of the code that you believe causes the problem, then it is not possible to understand the context that may have contributed to it.

You need to provide us a complete example that runs and reproduces the problem you are observing.