简体   繁体   中英

I need to find the shortest path (distance) between a source node and a target node. Given that certain nodes MUST be included

I need to get from the blue circle to the red circle. The path must include the black circle, ( even though it might not be optimal).

儿童乘巴士运输

i have included distances from node to node. and by using the 'dijkstra_path' i get: 在此处输入图片说明

which is correct.

But... what can i do to make sure 'kountoumas' is included or even a list of other nodes.

and then run the algorithm or another one.

thank you

Calculate the distance from the blue circle to the black circle. Then, calculate the distance from the black circle to the red circle. Then, print everything as if it was a single path. This has the advantage of working even for lists of "intermediary" circles.

It even works if they have a specific order (as you said in the comments)!

在此处输入图片说明

As per my understanding of your last comment, you want to list all possible paths passing through the intermediate nodes to be able to choose the shortest one. So, for the graph in the figure, here is the code for listing all possible paths from 1 to 2 as first and last nodes respectively, with intermediate nodes 3 and 4. I added some comments to try to make it as clear as possible.

start = 1 # starting node for the path (fixed)
end = 2 # last node in the path (fixed)
intermediate = [4,3] # Intermediate nodes that the path must include
#permutations of intermediate nodes to find shortest path
p =  list(it.permutations(intermediate))
print "Possible orders of intermediate nodes", p, '\n'
hops_tmp = 0
path_tmp = [] # stores path for each permutation
sub_path_tmp = [] # stores sub path from one node to another
for j in xrange(len(p)): # loop for all permutations possibilities
    # path from starting node to the first intermediate node
    sub_path_tmp = nx.dijkstra_path(G,start,p[j][0]) 
    for k in xrange(len(sub_path_tmp)): # update path with sub_path
        path_tmp.append(sub_path_tmp[k])
    #loop to find path from intermediate to another upto the last node
    for i in xrange(len(intermediate)):
        # if last intermediate node calculate path to last node
        if i == len(intermediate) - 1:
            sub_path_tmp =  nx.dijkstra_path(G,p[j][i],end)
        else: # otherwise calculate path to the next intermediate node
            sub_path_tmp =  nx.dijkstra_path(G,p[j][i],p[j][i+1]) 
        for k in xrange(len(sub_path_tmp)-1): # update path with sub_path
            path_tmp.append(sub_path_tmp[k+1])
    hops_tmp = len(path_tmp) -1
    print path_tmp
    print hops_tmp , '\n'
    # Reset path and hops for the next permutation
    hops_tmp = 0
    path_tmp = []

And the result was as follows:

 Possible orders of intermediate nodes [(4, 3), (3, 4)] 

 [1, 8, 4, 8, 1, 3, 7, 5, 9, 2]

 9

 [1, 3, 1, 8, 4, 5, 9, 2]

 7 

PS 1- you can add other intermediate nodes if you wish and it should work

2- extracting the shortest path should be easy but I did not include it just to focus on the core of the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM