简体   繁体   English

我需要找到源节点和目标节点之间的最短路径(距离)。 鉴于必须包含某些节点

[英]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: 通过使用'dijkstra_path'我得到: 在此处输入图片说明

which is correct. 哪个是对的。

But... what can i do to make sure 'kountoumas' is included or even a list of other nodes. 但是...我该怎么做才能确保包括“ kountoumas”甚至其他节点的列表。

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. 因此,对于图中的图形,这是用于列出从1到2的所有可能路径分别作为第一个节点和最后一个节点以及中间节点3和4的代码。我添加了一些注释,试图使其尽可能清楚。

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 PS 1-如果需要,您可以添加其他中间节点,它应该可以工作

2- extracting the shortest path should be easy but I did not include it just to focus on the core of the problem 2-提取最短路径应该很容易,但我并没有将其包括在内只是为了关注问题的核心

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 R / igraph,有没有办法在考虑到唯一节点属性的计数的情况下找到节点之间的最短路径? - Using R / igraph, is there a way to find a shortest path between nodes taking the count of unique node attributes into account? 在有向加权图中,有效地找到两个节点 A 和 B 之间穿过节点 X 的最短路径的成本 - In a directed, weighted graph, efficiently find the cost of the shortest path between two nodes A and B that traverses through a node X 是否有可能找到一个起始节点和多个目的节点之间的最短路径? 使用 Networkx 或 OSMnx - is it possible to find the shortest path between an origin node and multiple destination nodes? Using Networkx or OSMnx GraphViz,找到两个节点之间的最短路径 - GraphViz, find the shortest path between two nodes 在Python中显示按与给定节点的距离排序的节点 - Displaying nodes sorted by distance to a given node in Python 在定向加权图中查找最短路径,该路径访问每个节点,对重新访问节点和边缘没有限制 - Find shortest path in directed, weighted graph that visits every node with no restrictions on revisiting nodes and edges 如何在不排序列表的情况下找到随机列表中两个数字之间的最短距离。 我的代码如下,我需要另一种快速的方式 - how find the shortest distance between two numbers in a random list without sorting the list. my code is given below , i need another FAST way 如何在给定孤立节点与其非相邻节点之间的边缘距离的情况下预测列车系统的图网络? - How to predict graph network given edge distance between isolated nodes and their non-adjacent node for a train system? OSMNX Shortest path Nodes - 获取节点经过的时间 - OSMNX Shortest path Nodes - Get node traveled time 寻找最短距离,同时访问每个节点恰好一次 - Find shortest distance, while visiting every node exactly once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM