
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