1

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).

Children Trasportation by Bus

i have included distances from node to node. and by using the 'dijkstra_path' i get: enter image description here

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

George Pamfilis
  • 1,397
  • 2
  • 19
  • 37
  • if possible can the nodes i wish to be included be in a certain order when finding the shortest path. – George Pamfilis Sep 25 '15 at 10:32
  • I'm not quite sure, but how is doing this: Calculate shortest distance from A->B and then from B->C. – AKJ88 Sep 25 '15 at 10:32
  • This is a variant of the TSP: http://stackoverflow.com/questions/20501634/traveling-salesman-without-return-and-with-given-start-and-end-cities – Phidelux Sep 25 '15 at 10:47
  • possible duplicate of [Finding the shortest path in a graph between 2 nodes that goes through a subset of nodes](http://stackoverflow.com/questions/7678789/finding-the-shortest-path-in-a-graph-between-2-nodes-that-goes-through-a-subset) – Phidelux Sep 25 '15 at 10:58
  • Does it need to be a path, or can it revisit some nodes? – Joel Sep 25 '15 at 16:25
  • it needs to be a path so i can design the routes. it can revisit some nodes. for example some nodes might have 5 children to 10 children that have to be picked up with the bus or the taxi (greek island way of transportation). so i NEED to go through certain nodes. but with limited cost (distance wise). – George Pamfilis Sep 27 '15 at 14:21
  • is there a way to list the paths where an existing networkx algorithm went through and find a path that includes those nodes and is the shortest?? maybe that could work. – George Pamfilis Sep 27 '15 at 14:26
  • Would you clarify what did you mean in your last comment ? – Abdallah Sobehy Sep 29 '15 at 20:26
  • Let me clarify my question: Are cycles allowed? (note - if you don't do "@joel" I won't get a notice that you've answered my comment). I'd also like to know if it has to do the intermediate nodes in a particular order. – Joel Sep 30 '15 at 09:59
  • To clarify...@AbdallahSobehy. list all the possible paths from blue to red. filter out those that do not contain the black. from the paths that are left pick the one with the shortest distance. – George Pamfilis Oct 08 '15 at 17:34

2 Answers2

2

enter image description here

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 

P.S 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

Community
  • 1
  • 1
Abdallah Sobehy
  • 2,881
  • 1
  • 15
  • 28
  • this should do it. i will test it out a couple a times and ill get back to you!! btw does it work for a directed graph although most of the roads are two-way. thanks – George Pamfilis Oct 08 '15 at 17:37
  • How did it work out after testing ? For directed graph with two ways roads I am afraid might not be solvable by any means as you might have infinite paths. Check this example: 1<->2<->3, you want to find all paths from 1 to 3 passing through 2. Though, it looks simple but you have: [1,2,3], [1,2,1,2,3]... which is equivalent to [1,[2,1]*,2,3] so infinite paths – Abdallah Sobehy Oct 08 '15 at 20:31
1

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)!

3442
  • 8,248
  • 2
  • 19
  • 41