For each node (pink nodes) in an undirected/unweighted graph, I want to compute the closest distance to a given set of connected nodes (red nodes). It is essential that the computation is fast, also on large graphs (~4000 nodes) and for a large set of connected nodes. See the illustration below. What is the best algorithm/graph library for me to do this?
I tried to do something like that with NetworkX - shortest_path_length already, but it is too slow. Especially for a large set of connected red nodes.
import networkx as nx
all_distances = []
for node in red_nodes:
distances = nx.shortest_path_length(graph, source=node) # compute distances to all nodes in the graph from the source node
all_distances.append(distances)
shortest_distances = filter_for_shortest_distance(all_distances)
Here is an example on how to access a graph that I am working with. The red nodes could be any subset of connected nodes in the graph.
# Import navis
import navis
# Load one of the example neurons
sk = navis.example_neurons(n=1, kind='skeleton')
# Inspect the neuron graph
graph = sk.get_graph_nx()
