python networkx iteratively calculating shortest paths. It is allowed to use previously found paths

Viewed 38

I am looking to calculate multiple shortest paths from a set of node-pairs in a graph by using networkx. However, for some node-pairs, using (a part) of some previously found paths is allowed and preferred.

I already have the code for the weighted graph, a list of node-pairs and am iteratively using nx.astar_path to find the paths between node-pairs.

for node_pair in node_pairs:
    path = nx.astar_path(Graph,node_pair[0],node_pair[1],heuristic=heur,weight='weight')
    paths.append(path) 

Should I change the weight of the used edges in previously found paths in the graph depending on the node-pair? Or is there a better method to let the algorithm prefer previously found paths?

Unfortunately, the code below is not possible because not all paths are allowed to use (parts of) previously found paths:

for node_pair in node_pairs:
    path = nx.astar_path(Graph,node_pair[0],node_pair[1],heuristic=heur,weight='weight')
    paths.append(path) 
    for i in range(len(path)-1):
        Graph[path[i]][path[i+1]]['weight'] = 0

Regards

0 Answers
Related